【问题标题】:Globalize Fallback does not work in production environmentGlobalize Fallback 在生产环境中不起作用
【发布时间】:2015-06-20 17:29:06
【问题描述】:

我有以下 I18n-Setup

config.i18n.available_locales = [:de, :en]
config.i18n.fallbacks = true
config.i18n.enforce_available_locales = true
config.i18n.default_locale = :de

我有一个基于 URL 的语言环境切换:

# routes.rb
scope '(:locale)/', locale: /en|de/, defaults: {locale: 'de'} do
  resources :programmes
  root 'programmes#portal'
end

在我的 ApplicationController 中,我将语言环境设置为 before_filter

# application_controller.rb  
before_filter :set_locale

def set_locale
  I18n.locale =  params[:locale] || I18n.default_locale
end

在我的application.rb

config.i18n.available_locales = [:de, :en]
config.i18n.fallbacks = true
config.i18n.enforce_available_locales = true
config.i18n.default_locale = :de

我的两种语言都有备用方案 (/app/initializers/globalize.rb):

Globalize.fallbacks = {:en => [:en, :de], :de => [:de, :en]}

我的 development.rbproduction.rb 在 I18n 设置方面没有区别。

现在我遇到了以下问题:

  1. 我有一个只翻译成EN的记录
  2. 在我的开发环境中使用 locale = DE 回退到英文标题作品
  3. 但是,在生产环境中,这种回退方法不起作用。

我不清楚为什么这在生产环境中不起作用。

更新:在我的生产环境的生产控制台中,回退似乎也可以工作:

irb(main):005:0* I18n.locale
=> :de

irb(main):006:0> p=Programme.find(1289)
=> #<Programme id: 1289, created_at: "2014-07-08 09:58:21", ....>

irb(main):007:0> p.title
=> "English Title"

irb(main):008:0> I18n.locale = :en
=> :en

irb(main):009:0> p.title
=> "English Title"

irb(main):010:0> I18n.locale = :something-undefined
=> :something-undefined

irb(main):011:0> p.title    
=> nil

irb(main):012:0> I18n.default_locale
=> :de

【问题讨论】:

标签: ruby-on-rails ruby ruby-on-rails-4 globalize


【解决方案1】:

TL;DR

# config/application.rb
require 'i18n/backend/fallbacks'

回答

我最近在开发环境中遇到了类似的错误。

这是我的相关代码设置:

# Gemfile
gem 'globalize', '5.0.1'
gem 'rails', '4.2.2'
gem 'rails-i18n', '4.0.4'

和:

# config/application.rb
config.i18n.available_locales = [:it, :en]
config.i18n.enforce_available_locales = true
config.i18n.default_locale = :it
config.i18n.fallbacks = true

通过这个设置,我得到了这些结果:

2.2.2 :001 > Product.find_by_name('Fun Farm').translations.inspect
 => "#<ActiveRecord::Associations::CollectionProxy [#<Product::Translation id: 16, product_id: 16, locale: \"it\", created_at: \"2015-06-20 08:33:51\", updated_at: \"2015-06-20 08:33:51\", awards: \"Vincitore della <strong>Ludoteca Ideale</strong>, ...\", spot: \"Il party game per chi ha i riflessi pronti.\", synopsis: \"<p>\\n  Gli animali stanno scappando dalla fattoria ...\">]>"
2.2.2 :002 > Product.find_by_name('Fun Farm').spot
 => "Il party game per chi ha i riflessi pronti." 
2.2.2 :003 > I18n::locale = :en
 => :en
2.2.2 :004 > Product.find_by_name('Fun Farm').spot
 => nil

添加:

# config/application.rb
require 'i18n/backend/fallbacks'

有效:

2.2.2 :001 > Product.find_by_name('Fun Farm').translations.inspect
 => "#<ActiveRecord::Associations::CollectionProxy [#<Product::Translation id: 16, product_id: 16, locale: \"it\", created_at: \"2015-06-20 08:33:51\", updated_at: \"2015-06-20 08:33:51\", awards: \"Vincitore della <strong>Ludoteca Ideale</strong>, ...\", spot: \"Il party game per chi ha i riflessi pronti.\", synopsis: \"<p>\\n  Gli animali stanno scappando dalla fattoria ...\">]>"
2.2.2 :002 > Product.find_by_name('Fun Farm').spot
 => "Il party game per chi ha i riflessi pronti." 
2.2.2 :003 > I18n::locale = :en
 => :en
2.2.2 :004 > Product.find_by_name('Fun Farm').spot
 => "Il party game per chi ha i riflessi pronti."

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    相关资源
    最近更新 更多