我有以下I18n-Setup
config.i18n.available_locales = [:de, :en]
config.i18n.fallbacks = true
config.i18n.enforce_available_locales = true
config.i18n.default_locale = :de
我有一个基于网址的区域设置切换:
# 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.rb
和production.rb
在I18n设置方面没有区别。
现在我遇到了以下问题:
EN
DE
后退英文标题我不清楚为什么这在生产环境中不起作用。
更新:在我的生产环境的生产控制台中,后备似乎也有效:
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
答案 0 :(得分:2)
<强> 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."