Rails: Internationalization of Javascript Strings?

前端 未结 10 1438
你的背包
你的背包 2020-12-07 10:45

So, we have an existing Rails 2.3.5 app that does not support Internationalization at all. Now, I\'m well familiar with Rails I18n stuff, but we have a LOT of output strings

10条回答
  •  我在风中等你
    2020-12-07 10:56

    Why not something simple like:

    
    

    Then in JS you can do things like:

    I18n["en-US"]["alpha"]["bravo"];
    

    I've wrapped mine in an application helper.

    def current_translations
      @translations ||= I18n.backend.send(:translations)
      @translations[I18n.locale].with_indifferent_access
    end
    

    Then my call in my application.html.erb looks like this:

    
    

    This allows you to avoid having to know the current locale in JavaScript.

    I18n["alpha"]["bravo"];
    

    Or

    I18n.alpha.bravo;
    

提交回复
热议问题