如何仅对某些语言环境而不是对所有语言环境启用Rails i18n后备? [英] How do I enable Rails i18n fallbacks only for some locales, not all?
问题描述
我正在Rails 4应用程序中使用i18n后备,以减少"de"和"de-AT"语言环境的重复翻译(将来可能会添加更多这样的对).为了使之成为可能,我在 application.rb
中启用了后备广告:
I am using i18n fallbacks in a Rails 4 application in order to reduce duplicate translations for "de" and "de-AT" locales (possibly more pairs like this to add in the future). In order to make this possible, I've enabled fallbacks in my application.rb
:
config.i18n.fallbacks = true
config.i18n.fallbacks = {'de-AT' => 'de'}
但是我不希望其他语言环境在生产和开发过程中都不使用英语(这是默认语言环境).
But I don't want other locales to fall back to English (which is the default locale), neither in production nor in development.
有可能吗?如果可以,我该如何实现呢?
Is that possible, and if yes, how do I achieve that?
推荐答案
This is tricky in Rails up to 6.1 because you need to beat the logic in the Railtie initializer which desperately wants to fallback to the default_locale
.
要将默认的后备区域设置设置为 nil
,您需要使用以下代码:
To set the default fallback locale to nil
you need to use the following code:
config.i18n.default_locale = :en
config.i18n.fallbacks.defaults = [[]] # If you just write [], then default_locale is used
config.i18n.fallbacks.map = {
ca: :es, # Catalan fallback to Spanish
de: "en-UK", # German fallback to UK English first, then automatically to :en
}
注#1:默认情况下,所有国家/地区变体( en-US
)都会先回退到语言(例如 en
),并且您只需要指定一些内容即可后退的方式有所不同.
Note #1: By default all country variants (en-US
) will fallback to languages first (e.g en
) and you only need to specify something if you want to fallback differently.
注意#2:要检查设置是否正确,请执行以下操作:
Note #2: To check if you set it up correctly:
...> rails console
2.7.2 :001 > I18n.fallbacks["de-AT"] # Check what happens with Austrian German?
=> [:"de-AT", :de, :"en-UK", :en]
2.7.2 :002 > I18n.fallbacks[:ca] # Catalan falls back to Spanish
=> [:es]
2.7.2 :003 > I18n.fallbacks[:fr] # French should have no fallback!
=> [:fr]
注意#3:映射将递归扩展.
Note #3: Mappings will be recursively expanded.
config.i18n.fallbacks.map = {
es: :fr, # Spanish to French
fr: :en, # French to English
}
rails console
2.7.2 :001 > I18n.fallbacks[:es]
=> [:es, :fr, :en]
但是您不需要注意周期和/或重复项.这些被过滤掉了.
But you don't need to watch for cycles and/or duplicates. These are filtered out.
注#4:映射哈希还可以将语言代码映射到数组,该数组按从左到右的顺序进行处理.
Note #4: The mapping hash can map language codes also to arrays, which are processed in order from left to right.
config.i18n.fallbacks.map = {
ca: [:es, :fr, :en], # Catalan falls back to Spanish then French, then English
}
注释5:我还没有找到一个好的规范语言映射模型,该模型需要适当"的语言映射.后备决策基于每个国家/地区当前的语言技能并将其编码为Rails.
Note #5: I haven't found a good canonical language mapping model, which takes "appropriate" fallback decisions based on language current language skills per country and encodes it in Rails.
注释#6:我强烈建议:
Note #6: I would strongly suggest:
- 将:en用作所有内容的唯一且主要的后备.
- 保持:en版本尽可能简单和惯用.
- 自定义"zh-CN"和"en-UK"在选定的地方.
- 如果您要翻译,请进行全面的翻译.部分翻译UI非常令人恼火.
注释#7:从Rails 6.1.3开始,默认语言环境不再默认作为回退(
Note #7: Starting in Rails 6.1.3 the default locale is no longer included by default as a fallback (see this commit)
这篇关于如何仅对某些语言环境而不是对所有语言环境启用Rails i18n后备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!