1

我有以下 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
4

1 回答 1

3

TL;博士

# 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."
于 2015-06-20T09:29:06.997 回答