简体   繁体   中英

Rails I18n Cascading - how to get it to work

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?

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"

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM