Rails I18n级联-如何使其正常工作 [英] Rails I18n Cascading - how to get it to work
问题描述
我阅读了此博客文章来自Sven Fuchs 关于I18n的级联可能性,但我无法使其发挥作用。
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.
有人关于如何在Rails 4 App中启动和运行I18n级联的任何提示或可行示例吗?
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.
问题是,默认情况下不包括级联模块,因此您必须自己包含它。
不幸的是,似乎没有预定义的配置选项(例如 config.i18n.fallbacks
,但是如果我错了,请纠正我),因此,您必须手动添加它:
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
此后,如果您通过级联:true $ c $
After that, if you pass the cascade: true
option into the translate-helper, it will work.
请考虑在<$ c $中包含以下几行c> 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'
在您看来,您可以这样使用它:
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"
它还与惰性查找:
# 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"
这篇关于Rails I18n级联-如何使其正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!