Rails I18n Cascading - how to get it to work

和自甴很熟 提交于 2020-01-07 04:12:17

问题


I read in this blogpost from Sven Fuchs about the cascading possibility by I18n, but I am unable to get it to work.

I tried to put the code mentioned in the blogpost into the application controller and into an initializer, I also tried to pass the cascade option, like it is mentioned in the comments in the module itself, but nothing seems to work.

Does anybody have any hints or a working example about how to get I18n cascading in a Rails 4 App up and running?


回答1:


So, finally got this working.

The problem is, that the cascading module isn't included by default, so you have to include it yourself. Unfortunately, there doesn't seem to be a predefined config-option (like with config.i18n.fallbacks, but please correct me if I am wrong), so you have to include it manually:

# config/application.rb
module NameOfMyApp
  class Application < Rails::Application
    # some config code here
    I18n.backend.class.send(:include, I18n::Backend::Cascade)
    # more config code
  end
end

After that, if you pass the cascade: true option into the translate-helper, it will work.

An example

Consider that you have the following lines in your en.yml file:

# config/locales/en.yml
en:
  title: 'Default application title'
  subtitle: 'Default application subtitle'
  footertext: 'Default text for footer'

  accounts:
    title: 'Default accounts title'
    subtitle: 'Default accounts subtitle'

    index:
      title: 'Accounts index title'

In your views you use it like this:

# app/views/account/index.html.erb
<%= t('accounts.index.title', cascade: true) %> # => "Accounts index title"
<%= t('accounts.index.subtitle', cascade: true) %> # => "Default accounts subtitle"
<%= t('accounts.index.footertext', cascade: true) %> # => "Default text for footer"

It is also working with lazy lookups:

# app/views/account/index.html.erb
<%= t('.title', cascade: true) %> # => "Accounts index title"
<%= t('.subtitle', cascade: true) %> # => "Default accounts subtitle"
<%= t('.footertext', cascade: true) %> # => "Default text for footer"



回答2:


Some additional information to the accepted answer (that helped me quite well to get in the right direction).

If you also want the cascading lookup to be the default behaviour of the translate helper, one way to go is by overriding the translate method. Doing this you manipulate as little as possible to get this to work. Most probably this solution should be upwards compatible, too - as long as the translate helper isn't changed too much regarding method signature etc.

# e.g. in config/initializers/i18n.rb
# monkey-patch to let I18n.translate (and its alias 't') cascade for keys by default
ActionView::Base.class_eval do
  def translate(key, options = {})
    super(key, options.merge({ cascade: { skip_root: false } }))
  end
  alias t translate
end

This solution is as well the recommended way by Sven Fuchs and is tested with Rails 4.0.4.



来源:https://stackoverflow.com/questions/18510500/rails-i18n-cascading-how-to-get-it-to-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!