如何使用 i18n & 配置语言环境别名导轨? [英] How to configure locale aliases using i18n & rails?
问题描述
我正在开发一个 Rails 应用程序 (3.2.13),该应用程序被翻译成多种语言,包括挪威语(3 种可用风格之一).在公共页面上,该应用程序使用浏览器的语言设置来设置区域设置.
I am working on a rails app (3.2.13) that is being translated into several languages, including (one of the 3 available flavours of) Norwegian. On public pages, the app uses the browser's language settings to set locale.
大多数浏览器提供 3 个单独的挪威短代码:no
、nb
&nn
.我们的翻译在 nb
中,但我认为最好是 no
&nn
也默认为 nb
.这样,如果用户的浏览器语言首选项设置为 no
然后是 en
,应用程序将尝试首先提供 nb
挪威语,而不是跳过直接上英语.
Most browsers offer 3 separate Norwegian shortcodes: no
, nb
& nn
. The translations we have are in nb
, but I think it would be best if no
& nn
also defaulted to nb
. That way, if a user's browser language preferences were set to no
then en
, the app would try to supply nb
Norwegian first, instead of skipping straight to English.
是否可以为 i18n gem 配置一个语言别名"列表,像这样?
Is it possible to configure a list of "language aliases" for the i18n gem, something like this?
config.i18n.available_locales = [:sv, :en, :nb, :da, :fi]
config.i18n.aliased_locales = [:nb <= :no, :nb <= :nn]
推荐答案
简短回答
看看回退
在 initializers
中创建一个文件,如 i18n_fallbacks.rb
Create a file in initializers
like i18n_fallbacks.rb
config.i18n.fallbacks = {:no => [:nb], :nn => [:nb]}
相关事项
您甚至可以设置多个回退,它们将按照指定的顺序进行:
You can even set multiple fallbacks, and they will be taken in the same order as specified:
例如:
config.i18n.default_locale = :de
config.i18n.fallbacks = {:de => [:en,:es]}
de.yml
:de:
greeting: Hallo
en.yml
:en:
foo: bar
es.yml
:es:
bar: baz
您将获得以下信息:
I18n.t :greeting # found in de.yml, no fallback
# => 'Hallo'
I18n.t :foo # not in :de, try in :en and found
# => "bar"
I18n.t :bar # not in :de, try in :en and in :es
# => "baz"
I81n.t :other # not found anywhere, note the message delivers not found for the current locale:
# => "translation missing: de.other"
这篇关于如何使用 i18n & 配置语言环境别名导轨?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!