简体   繁体   English

如何为Rails 4设置语言环境

[英]How to setup locales for Rails 4

i have a rather specific setup regarding rails internationalization. 我对Rails国际化有一个相当具体的设置。 I'm using rails-i18n gem, but that shouldn't matter. 我正在使用rails-i18n gem,但这无关紧要。 It worked perfectly with Rails 3. Here is my config from config/application.rb 它与Rails 3完美配合。这是config/application.rb

config.i18n.default_locale = :en
config.i18n.locale = :hr

Let me explain: 让我解释:

  • locale is set to :hr (Croatian) because I mostly make localized applications in Croatian language 语言环境设置为:hr (克罗地亚语),因为我主要使用克罗地亚语进行本地化的应用程序
  • default locale is set to :en because I'm often using gems like rails-admin which have English translations included. 默认语言环境设置为:en因为我经常使用像rails-admin这样的宝石,其中包含英文翻译。 It plays nicely in production where missing (Croatian) translations fallback to English. 它在生产中表现出色,因为缺少的(克罗地亚语)翻译会退回到英语。 That's fine, all admins understand English :) 很好,所有管理员都懂英语:)

And the question is: how to make it work with Rails 4? 问题是:如何使其与Rails 4一起使用?

It seems that Rails 4 ignores config.i18n.locale , and it always use :en locale. 看来Rails 4会忽略config.i18n.locale ,并且始终使用:en区域设置。

So far, I've been using before_action to set I18n.locale = :hr but that doesn't work in Rails console or Rack middleware... 到目前为止,我一直在使用before_action设置I18n.locale = :hr但这在Rails控制台或Rack中间件中不起作用...

Thanks in advance, 提前致谢,

Danijel 海伦芬

I found a simple solution and I'm posting it here... 我找到了一个简单的解决方案,并将其发布在这里...

Insert into config/application.rb 插入config/application.rb

  config.i18n.default_locale = :hr
  config.i18n.available_locales = [:hr, :en]
  config.i18n.fallbacks = [:en]

Remove or comment the following line from config/environments/production.rb config/environments/production.rb删除或注释以下行

  # config.i18n.fallbacks = true

or change it to: 或将其更改为:

  config.i18n.fallbacks = [:en]

You can add the code below: 您可以在下面添加代码:

class ApplicationController
    ...

    before_filter :set_locale

    ...

    private

    def set_locale
        I18n.locale = :hr
    end 

end 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM