views:

19

answers:

1

I'm using jQuery in my current Rails project and I'd like to have some way to use the translations from my yml files in Javascript.

I know that I can easily use them in my .js.erb templates. But what about the javascript files in /public/javascript?

It looks like Babilu (http://github.com/toretore/babilu) would do exactly what I want. I was just wondering if there are other plugins out there... It's not that I have something against Babilu, but I do like to have choice ;-)

Also it might be that there is some default way in Rails 2.3.5. I'm not aware of and I maybe don't have to use a plugin at all?

+1  A: 

controller:

class JavascriptsController < ApplicationController

  skip_before_filter :authorize
  respond_to :js
  layout false
  caches_page :locale # don't know if this is reset on server restart.

  def locale

    # http://edgeguides.rubyonrails.org/caching_with_rails.html
    @translations = I18n.translate('javascripts')

  end

end

view script:

Translations = <%= raw @translations.to_json %>;

and in application.html.haml:

= javascript_include_tag "locale.js?language=#{I18n.locale}" # JavascriptsController

Then add some simple js that looks up keys in the hash. Fairly easy to fix. Also, this way you won't be passing all translations to js, just the subset that is actually used in the javascript layer (shouldn't be too much).

sandstrom