Ruby-on-rails 如何在rails中设置默认语言环境

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24909128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:47:50  来源:igfitidea点击:

How to set default locale in rails

ruby-on-railsruby-on-rails-4internationalization

提问by raphael_turtle

How do I set the default locale in rails application.rb so I don't have to add unit every time I call the following?

如何在 rails application.rb 中设置默认语言环境,以便每次调用以下内容时都不必添加单位?

number_to_currency(@course.price, unit: "£") 

Rails guides says my locale is :en-GBfor the UK, but the following obviously creates an error in application.rb

Rails 指南说我的语言环境:en-GB适用于英国,但以下内容显然会在 application.rb 中产生错误

config.i18n.default_locale = :en-GB

回答by Alistair Holt

The rails-i18ngem contains a lot of locale configurations for Rails and en-GBis one of them. en-GB isn't included in Rails itself.

rails-i18n宝石含有大量的语言环境配置为Rails和EN-GB就是其中之一。en-GB 不包含在 Rails 本身中。

  1. Add the rails-i18ngem
  2. Add config.i18n.default_locale = :'en-GB'to application.rb
  1. 添加rails-i18n宝石
  2. 添加config.i18n.default_locale = :'en-GB'application.rb

回答by Marek Lipka

Try this:

尝试这个:

config.i18n.default_locale = :'en-GB'

回答by Ulysse BN

I had a similar issue where I had to set the locale, locally... There is a great workaround in Rails, many string based methods can be given a localeoption. See the chunk of code below:

我有一个类似的问题,我必须在本地设置语言环境...... Rails 中有一个很好的解决方法,可以为许多基于字符串的方法提供一个locale选项。请参阅下面的代码块:

number_to_currency(@course.price, unit: "£", locale: :"en-GB")

I know this could be argued as not an answer, but it would have been really helpful for me to find it here. I hope it will be for others!

我知道这可能被认为不是答案,但在这里找到它对我来说真的很有帮助。我希望它会适用于其他人!

回答by Snips

I avoided added another gem dependency, by adding my desired format for currency to the existing en (English) locale.

通过将我想要的货币格式添加到现有的 en(英语)语言环境中,我避免添加另一个 gem 依赖项。

Edit the file en.yml found in config/locales, and add the following number/currency format definition (you will see an existing hello: definition),

编辑在 config/locales 中找到的文件 en.yml,并添加以下数字/货币格式定义(您将看到一个现有的 hello: 定义),

en:
  hello: "Hello world"

  number:
    currency:
      format:
        delimiter: ','
        format: '%u%n'
        precision: 2
        separator: '.'
        strip_insignificant_zeros: false
        unit: '£'

...gives £1,000.00

...给 £1,000.00

As an alternative,

作为备选,

  number:
    currency:
      format:
        delimiter: ''
        format: '%n %u'
        precision: 0
        separator: '.'
        strip_insignificant_zeros: false
        unit: 'GBP'

...gives 1000 GBP

...给出 1000 英镑

See herefor more formatting options.

有关更多格式选项,请参见此处

回答by José Carlos Morín

You should set the next configuration in: application.rb

您应该在:application.rb 中设置下一个配置

config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')] config.i18n.default_locale = :de

config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')] config.i18n.default_locale = :de

回答by cthulhu

You need to install the rails-i18n gem first ( https://github.com/svenfuchs/rails-i18n) or create 'config/locales/en-GB.yml' file with 'en-GB.number.currency.format.unit: £' entry

您需要先安装 rails-i18n gem ( https://github.com/svenfuchs/rails-i18n) 或使用“en-GB.number.currency.format”创建“config/locales/en-GB.yml”文件.unit: £' 条目