加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 自定义Rails I18n Locale Pluralization帮助

发布时间:2020-12-17 03:12:21 所属栏目:百科 来源:网络整理
导读:我正在尝试在I18n amp;中实现特定于语言环境的复数规则. Rails,但我没有运气.这是我正在做的事情: # in config/initializers/locale.rbI18n::Backend::Simple.send(:include,I18n::Backend::Pluralization){ # Force Use of :few key :ru = {:i18n = {:plur
我正在尝试在I18n& amp;中实现特定于语言环境的复数规则. 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"

你可以看到,我试图强制:几个键(它应该返回“少数用户”),只是为了看看这个dang复数器是否会工作…但没有骰子:(

这是我正在运行的环境:

> Rails 2.3.8
> i18n 0.5.0宝石

有任何想法吗?

解决方法

尝试复制你的问题,并有同样的问题.将复数规则移动到语言环境文件中,并且运行正常.

将语言环境文件切换到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"

可能会尝试一下,看看它是否有帮助

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读