Ruby-on-rails 如何使用 rails i18n 回退功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4223806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to use rails i18n fallback features
提问by antpaw
I have this i18n problem
我有这个 i18n 问题
activerecord:
notices:
messages:
success:
create: "Something was created"
models:
user:
success:
create: "Thanks for registration"
I18n.t("activerecord.notices.models.user.success.create")
# => "Thanks for registration"
I18n.t("activerecord.notices.models.book.success.create")
# => "translation missing: de, activerecord, notices, models, book, success, create"
I don't know why the book model doesn't get the fallback massage. I have set config.i18n.fallbacks = true. I'm using Rails 3
我不知道为什么书本模型没有得到回退按摩。我已经设置了config.i18n.fallbacks = true。我正在使用 Rails 3
回答by raskhadafi
I set in config/application.rb usually
我通常在 config/application.rb 中设置
config.i18n.fallbacks =[:de, :fr, :en]
So you can declare the order of the fallback.
所以你可以声明回退的顺序。
But keep attention in some environments/*.rb the configuration is overwritten.
但是要注意在某些环境中/*.rb 会覆盖配置。
回答by egze
回答by user456733
Have you enabled fallbacks for your backend? Assuming it's Simple(based on yml in example):
您是否为后端启用了回退?假设它很简单(基于示例中的 yml):
put this in an initializer:
把它放在一个初始化程序中:
require "i18n/backend/fallbacks"
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
回答by Alain Beauvois
In rails 3+, this is set in the config/environments files :
在 rails 3+ 中,这是在 config/environments 文件中设置的:
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation can not be found)
config.i18n.fallbacks = true
回答by pastullo
I believe the best way to handle a missing string, is to display a default locale, rather than an error message.
我相信处理丢失字符串的最佳方法是显示默认语言环境,而不是错误消息。
Add this line in application.rb to fallback to the english locale.
在 application.rb 中添加此行以回退到英语语言环境。
config.i18n.fallbacks = [:en]
In case you want to specify locale-specific fallbacks, you can use the following:
如果要指定特定于语言环境的回退,可以使用以下内容:
config.i18n.fallbacks = {:de => [:de,:en], :en => [:en,:de]}
Also, note that you can enable and disable fallbacks based on your environment.
So while on development it might make sense to have an error displayed, you can instead enable fallbacks in your environments/production.rbwith the following:
另请注意,您可以根据您的环境启用和禁用回退。因此,虽然在开发过程中显示错误可能是有意义的,但您可以environments/production.rb使用以下内容启用回退:
config.i18n.fallbacks = true
回答by LeFranck
There is a misunderstanding with the I18n Fallbackfeature.
对I18n Fallback功能存在误解。
This feature causes that when there is a missing translation exception(in this case, it happens when I18nfails to find the value associated with the "activerecord.notices.models.book.success.create"key in the locale files of your current language) I18nwill lookup in the predefined list of fallbacks languagesthe value of the key that generated the missing translation exception, if it's found I18nwill returned that value, but if it's not found in any of those other locale files I18nwill return the missing translation exception.
此功能导致当缺少翻译异常时(在这种情况下,当I18n无法"activerecord.notices.models.book.success.create"在您当前语言的语言环境文件中找到与键关联的值时会发生这种情况)I18n将在预定义的回退语言列表中查找该值在生成丢失翻译异常的键中,如果找到,I18n将返回该值,但如果在任何其他语言环境文件中未找到,I18n将返回丢失翻译异常。
So when you defined config.i18n.fallbacks = true, that doesn't mean that when a missing translation exceptionoccurs, in this case:
因此,当您定义 时config.i18n.fallbacks = true,这并不意味着发生丢失翻译异常时,在这种情况下:
I18n.t("activerecord.models.book.success.create")
# => "translation missing: de, activerecord, notices, models, book, success, create"
I18nwill lookup a similar key in your locale files to return his value, could be:
I18n将在您的语言环境文件中查找类似的键以返回其值,可能是:
I18n.t("activerecord.models.user.success.create")
# => "Thanks for registration"
What will happens it's that I18nwill lookup in yours defaults fallback languagesfor the specific language where the missing translation exceptionhas occurred.
会发生什么是I18n将在您的默认后备语言中查找发生丢失翻译异常的特定语言。
A good example of usage will be:
一个很好的用法示例是:
# using :"en-US" as a default locale:
I18n.default_locale = :"en-US"
I18n.fallbacks[:es] # => [:es, :"en-US", :en]
Locales files:
语言环境文件:
es:
activerecord:
notices:
messages:
success:
create: "Algo fue creado"
models:
user:
success:
create: "Gracias por registrarte"
en-US:
activerecord:
...
models:
books:
success:
create: "The model was created"
Call in English site:
拨打英文网站:
I18n.t("activerecord.models.books.success.create")
# => "The model was created"
Call in Spanish site:
致电西班牙网站:
#with config.i18n.fallbacks = false
I18n.t("activerecord.models.books.success.create")
# => "translation missing: es, activerecord, models, book, success, create"
#with config.i18n.fallbacks = true
I18n.t("activerecord.models.books.success.create")
# => "The model was created"
For more information check: https://github.com/ruby-i18n/i18n
更多信息请查看:https: //github.com/ruby-i18n/i18n

