【问题标题】:Custom Rails I18n Locale Pluralization Help自定义 Rails I18n 语言环境复数化帮助
【发布时间】:2010-12-05 02:09:58
【问题描述】:

我正在尝试在 I18n 和 Rails 中实现特定于语言环境的复数规则,但我没有运气。这就是我正在做的事情:

# in config/initializers/locale.rb
I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization)
{
   # Force Use of :few key 
   :ru =>  {:i18n => {:plural => {:rule => lambda { |n| :few}}}}
}

# in config/locales/ru.yml
ru:
  user: 
    one: One User 
    few: Few Users
    many: Many Users
    other: Other Users

# Testing
script/console
>> I18n.locale = :ru ; I18n.t("user", :count => 20)
=> "Other Users"

如您所见,我正在尝试强制使用 :few 键(它应该返回“Few Users”),只是为了看看这个该死的复数器是否可以工作......但没有骰子:(

这是我正在运行的环境:

  • Rails 2.3.8
  • i18n 0.5.0 宝石

有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails ruby internationalization


    【解决方案1】:

    尝试复制您的问题,但遇到了同样的问题。将复数规则移动到语言环境文件中,并且工作正常。

    将语言环境文件切换为 Ruby 样式,因为由于某种原因,常规 YAML 似乎不喜欢我的 lambda。

    # config/initializers/locale.rb
    I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization)
    
    # config/locales/ru.rb
    {
      :ru => {
        :user => {
          :one   => "One User",
          :few   => "Few Users",
          :many  => "Many Users",
          :other => "Other Users"
        },
        :i18n => {
          :plural => {
            :rule => lambda { |n| :few }
          }
        }
      }
    }
    
    # Testing
    $ script/console 
      Loading development environment (Rails 2.3.8)
      >> I18n.locale = :ru; I18n.t("user", :count => 20) #=> "Few Users"
    

    可以试试看是否有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多