如何仅为某些语言环境启用 Rails i18n 回退,而不是全部? [英] How do I enable Rails i18n fallbacks only for some locales, not all?

查看:12
本文介绍了如何仅为某些语言环境启用 Rails i18n 回退,而不是全部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 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?

推荐答案

这在 Rails 6.1 之前很棘手,因为你需要 击败了 Railtie 初始化程序中的逻辑,它迫切希望回退到 default_locale.

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 版本尽可能简单和不惯用.
  • 为en-US"定制和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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆