【问题标题】:I18n locale disregarding fallbacksI18n 语言环境不考虑后备
【发布时间】:2015-02-04 17:54:06
【问题描述】:

我问了previous question regarding locale-setting。我正在尝试将各种挪威语言的后备设置为挪威 Bokmal (:nb)。期望的行为是,如果浏览器传递 nn 或 no 作为语言环境请求,则 I18n.locale 将设置为 :nn 或 :no,然后在没有这些语言环境的翻译时,将向浏览器提供 :nb .

根据我上一个问题的答案,我的应用程序初始化程序中有这一行:

  config.i18n.default_locale    = :en
  config.i18n.fallbacks = {:nn => [:nb], :no => [:nb]}

在 Rails 控制台中,这给了我以下结果:

> I18n.fallbacks
  => {:en=>[:en]} 

> I18n.fallbacks[:nn]
  => [:nn, :nb, :en] 

> I18n.fallbacks[:no]
  => [:no, :nb, :en] 

使用语言列表中只有 nn 和 no 的浏览器,这不起作用 - 它会退回到 :en 的默认语言环境。这是请求标头:

Accept-Language: "nn,no;q=0.5"

如果我将 :nb 添加到浏览器语言堆栈,我会正确地获得挪威语内容。

在这个过程中我有什么遗漏吗?

【问题讨论】:

    标签: ruby-on-rails rails-i18n


    【解决方案1】:

    您需要根据浏览器设置设置I18n.locale

    def set_locale
      I18n.locale = extract_locale_from_accept_language_header
    end
    
    private
    
    def extract_locale_from_accept_language_header
      request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
    end
    

    取自:http://guides.rubyonrails.org/i18n.html#setting-the-locale-from-the-client-supplied-information

    【讨论】:

      猜你喜欢
      • 2019-04-23
      • 1970-01-01
      • 2015-08-05
      • 1970-01-01
      • 1970-01-01
      • 2018-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多