Make yml translations available in Javascript files

I am using jQuery in my current Rails project and I would like to be able to use translations from my yml files in Javascript.

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

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

It is also possible that Rails 2.3.5 has some default option. I don’t know, and I may not need to use the plugin at all?

+3
source share
1

:

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 %>;

application.html.haml:

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

js, . . , js, , javascript ( ).

+5

Source: https://habr.com/ru/post/1730761/


All Articles