所以,终于搞定了。
问题是,默认情况下不包含级联模块,因此您必须自己包含它。
不幸的是,似乎没有预定义的配置选项(如config.i18n.fallbacks,但如果我错了请纠正我),所以你必须手动包含它:
# 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
之后,如果您将 cascade: true 选项传递给 translate-helper,它将起作用。
一个例子
请考虑您的 en.yml 文件中有以下几行:
# 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'
在你看来,你是这样使用它的:
# 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"
它也适用于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"