Spy*_*ros 82 ruby-on-rails internationalization pluralize plural rails-i18n
我希望能够在rails中翻译i18n中的复数字符串.一个字符串可以是:
You have 2 kids
Run Code Online (Sandbox Code Playgroud)
要么
You have 1 kid
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用复数辅助方法,但我希望将其嵌入到i18n翻译中,以便我在将来的任何时候都不必弄乱我的观点.我读到它:count
在某种程度上用于复数的翻译,但我找不到任何关于它如何实现的真实资源.
请注意,我知道我可以在翻译字符串中传递变量.我也试过类似的东西:
<%= t 'misc.kids', :kids_num => pluralize(1, 'kid') %>
Run Code Online (Sandbox Code Playgroud)
哪个工作正常,但有一个相同想法的根本问题.我需要'kid'
在pluralize helper中指定字符串.我不想这样做,因为它将导致将来查看问题.相反,我想保留翻译中的所有内容,而不是视图中的任何内容.
我怎样才能做到这一点 ?
Zab*_*bba 169
试试这个:
en.yml
:
en:
misc:
kids:
zero: no kids
one: 1 kid
other: %{count} kids
Run Code Online (Sandbox Code Playgroud)
在一个视图中:
You have <%= t('misc.kids', :count => 4) %>
Run Code Online (Sandbox Code Playgroud)
更新了具有多个复数的语言的答案(使用Rails 3.0.7测试):
档案 config/initializers/pluralization.rb
:
require "i18n/backend/pluralization"
I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization)
Run Code Online (Sandbox Code Playgroud)
档案 config/locales/plurals.rb
:
{:ru =>
{ :i18n =>
{ :plural =>
{ :keys => [:one, :few, :other],
:rule => lambda { |n|
if n == 1
:one
else
if [2, 3, 4].include?(n % 10) &&
![12, 13, 14].include?(n % 100) &&
![22, 23, 24].include?(n % 100)
:few
else
:other
end
end
}
}
}
}
}
#More rules in this file: https://github.com/svenfuchs/i18n/blob/master/test/test_data/locales/plurals.rb
#(copy the file into `config/locales`)
Run Code Online (Sandbox Code Playgroud)
档案 config/locales/en.yml
:
en:
kids:
zero: en_zero
one: en_one
other: en_other
Run Code Online (Sandbox Code Playgroud)
档案 config/locales/ru.yml
:
ru:
kids:
zero: ru_zero
one: ru_one
few: ru_few
other: ru_other
Run Code Online (Sandbox Code Playgroud)
测试:
$ rails c
>> I18n.translate :kids, :count => 1
=> "en_one"
>> I18n.translate :kids, :count => 3
=> "en_other"
>> I18n.locale = :ru
=> :ru
>> I18n.translate :kids, :count => 1
=> "ru_one"
>> I18n.translate :kids, :count => 3
=> "ru_few" #works! yay!
>> I18n.translate :kids, :count => 5
=> "ru_other" #works! yay!
Run Code Online (Sandbox Code Playgroud)
sas*_*rov 35
我希望讲俄语的Ruby on Rails程序员能找到这个.只是想分享我自己非常精确的俄罗斯复数公式.它基于Unicode规范.这里只是config/locales/plurals.rb
文件的内容,其他一切都应该和上面的答案一样.
{:ru =>
{ :i18n =>
{ :plural =>
{ :keys => [:zero, :one, :few, :many],
:rule => lambda { |n|
if n == 0
:zero
elsif
( ( n % 10 ) == 1 ) && ( ( n % 100 != 11 ) )
# 1, 21, 31, 41, 51, 61...
:one
elsif
( [2, 3, 4].include?(n % 10) \
&& ![12, 13, 14].include?(n % 100) )
# 2-4, 22-24, 32-34...
:few
elsif ( (n % 10) == 0 || \
![5, 6, 7, 8, 9].include?(n % 10) || \
![11, 12, 13, 14].include?(n % 100) )
# 0, 5-20, 25-30, 35-40...
:many
end
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
母语人士可能会喜欢111
和121
.在这里测试结果:
谢谢你的初步答复!
sor*_*rin 11
首先,请记住复数形式的数量取决于语言,英语有两个,罗马尼亚语有3个,阿拉伯语有6个!
如果您希望能够正确使用复数表格,则必须使用gettext
.
对于Ruby和rails,您应该查看http://www.yotabanana.com/hiki/ruby-gettext-howto-rails.html
Rails 3通过CLDR考虑和计数插值变量来稳健地处理这个问题.请参阅http://guides.rubyonrails.org/i18n.html#pluralization
# in view
t('actors', :count => @movie.actors.size)
# locales file, i.e. config/locales/en.yml
en:
actors:
one: Actor
other: Actors
Run Code Online (Sandbox Code Playgroud)
这只是工作的开箱
.yml :
en:
kid:
one: '1 kid'
other: '%{count} kids'
Run Code Online (Sandbox Code Playgroud)
用法(当然,您可以在视图文件中跳过 I18n):
> I18n.t :kid, count: 1
=> "1 kid"
> I18n.t :kid, count: 3
=> "3 kids"
Run Code Online (Sandbox Code Playgroud)
安装rails-18n gem 并将翻译添加到您的.yml
文件中,如示例所示:
ru.yml :
ru:
kid:
zero: '??? ?????'
one: '%{count} ???????'
few: '%{count} ???????'
many: '%{count} ?????'
other: '????'
Run Code Online (Sandbox Code Playgroud)
用法:
> I18n.t :kid, count: 0
=> "??? ?????"
> I18n.t :kid, count: 1
=> "1 ???????"
> I18n.t :kid, count: 3
=> "3 ???????"
> I18n.t :kid, count: 5
=> "5 ?????"
> I18n.t :kid, count: 21
=> "21 ???????"
> I18n.t :kid, count: 114
=> "114 ?????"
> I18n.t :kid, count: ''
=> "????"
Run Code Online (Sandbox Code Playgroud)
小智 5
实际上有一种替代繁琐的 i18n 方法的方法。该解决方案称为 Tr8n。
你上面的代码只是:
<%= tr("You have {num || kid}", num: 1) %>
Run Code Online (Sandbox Code Playgroud)
就是这样。无需从代码中提取密钥并将它们维护在资源包中,无需为每种语言实施复数规则。Tr8n 带有适用于所有语言的数字上下文规则。它还带有性别规则、列表规则和语言案例。
上述翻译键的完整定义实际上如下所示:
<%= tr("You have {num:number || one: kid, other: kids}", num: 1) %>
Run Code Online (Sandbox Code Playgroud)
但由于我们要节省空间和时间,num 会自动映射到数字规则,无需为规则值提供所有选项。Tr8n 带有复数词和变形词,可以即时为您完成工作。
您的俄语密钥的翻译只是:
"? ??? ???? {num || ???????, ???????, ?????}"
Run Code Online (Sandbox Code Playgroud)
顺便说一句,您的翻译在具有特定性别规则的语言中会不准确。例如,在希伯来语中,您实际上必须为示例指定至少 2 个翻译,因为“您”会根据查看用户的性别而有所不同。Tr8n 处理得很好。这是希伯来语翻译的音译:
"Yesh leha yeled ahad" with {context: {viewing_user: male, num: one}}
"Yesh leha {num} yeladim" with {context: {viewing_user: male, num: other}}
"Yesh lah yeled ahad" with {context: {viewing_user: female, num: one}}
"Yesh lah {num} yeladim" with {context: {viewing_user: female, num: other}}
Run Code Online (Sandbox Code Playgroud)
因此,在这种情况下,您的单个英文密钥需要 4 次翻译。所有翻译都是在上下文中完成的 - 您不必打断句子。Tr8n 有一种机制可以根据语言和上下文将一个键映射到多个翻译——所有这些都是即时完成的。
最后一件事。如果您必须将计数部分设为粗体怎么办?它只是:
<%= tr("You have [bold: {num || kid}]", num: 1, bold: "<strong>{$0}</strong>") %>
Run Code Online (Sandbox Code Playgroud)
以防万一您想稍后重新定义“粗体” - 这将非常容易 - 您不必浏览所有 YAML 文件并更改它们 - 您只需在一个地方进行。
要了解更多信息,请查看这里:
https://github.com/tr8n/tr8n_rails_clientsdk
披露:我是 Tr8n 框架及其所有库的开发者和维护者。