Mic*_*unn 7 ruby-on-rails internationalization rails-i18n
我正在为列出产品评论摘要的商店构建视图。我正在按分数对评论进行分组,我希望在每组顶部有一个字符串,形式为“10 条 5 星评论”。我知道,如果我只想将“评论”复数化,我可以这样做en.rb:
en: {
reviews_header: {
one: "1 review",
other: "%{count} reviews"
}
}
Run Code Online (Sandbox Code Playgroud)
是否有一种reviews_header哈希格式可以让我指定“评论”和“明星”的计数,以便在必要时将它们都复数化?在伪代码中,我想象的是:
en: {
reviews_header: {
counts: [ :review_count, :star_count ],
review_count: {
one: {
star_count: {
one: "1 review with 1 star",
other: "1 review with %{star_count} stars"
}
},
other: {
star_count: {
one: "%{review_count} reviews with 1 star",
other: "%{review_count} reviews with %{star_count} stars"
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我会用t(:reviews_header, review_count: 10, star_count: 5).
我现在正在做的是将字符串更改为“10 条 5 星评论”的形式,这解决了“星级”复数的问题,但这在其他语言中不起作用。
这里有一个嵌套复数的情况。虽然我对 Ruby 的了解还很初级,但我找不到任何文档可以通过 Ruby 内置的 i18n 功能为这种嵌套复数情况提供解决方案。然而,在支持ICU Library的编程语言中,可以从MessageFormat中受益。
使用这个Ruby 库进行 MessageFormat 解析和格式化,可以手工制作一个嵌套的 MessageFormat 来覆盖该字符串的所有变体,从而覆盖任何语言中嵌套复数规则的复杂性。请记住,对于大多数语言来说,您不需要大部分规则,但有少数语言(例如阿拉伯语和俄语)需要许多这样的情况;阿拉伯语有 2 的特殊情况,俄语有 1 的特殊情况(1、21、31、1001,但没有 11)。可以在此处找到 Unicode CLDR 项目的图表,其中列出了所有语言的复数规则。
通常,我会培训翻译人员使用这个在线工具(来自同一开发人员message-format-rb)并根据其语言的需要翻译 MessageFormat。
这是一个完整的、最大的 MessageFormat,后面是 Ruby 代码片段:
{review_count, plural,
=0 {
{star_count, plural,
other {no reviews}}
}
zero {
{star_count, plural,
zero {{review_count} reviews with {star_count} stars}
one {{review_count} review with {star_count} star}
two {{review_count} reviews with {star_count} stars}
few {{review_count} reviews with {star_count} stars}
other {{review_count} reviews with {star_count} stars}}
}
one {
{star_count, plural,
zero {{review_count} review with {star_count} stars}
one {{review_count} review with {star_count} star}
two {{review_count} review with {star_count} stars}
few {{review_count} review with {star_count} stars}
other {{review_count} review with {star_count} stars}}
}
two {
{star_count, plural,
zero {{review_count} reviews with {star_count} stars}
one {{review_count} review with {star_count} star}
two {{review_count} reviews with {star_count} stars}
few {{review_count} reviews with {star_count} stars}
other {{review_count} reviews with {star_count} stars}}
}
few {
{star_count, plural,
zero {{review_count} reviews with {star_count} stars}
one {{review_count} review with {star_count} star}
two {{review_count} reviews with {star_count} stars}
few {{review_count} reviews with {star_count} stars}
other {{review_count} reviews with {star_count} stars}}
}
other {
{star_count, plural,
zero {{review_count} reviews with {star_count} stars}
one {{review_count} review with {star_count} star}
two {{review_count} reviews with {star_count} stars}
few {{review_count} reviews with {star_count} stars}
other {{review_count} reviews with {star_count} stars}}
}
}
Run Code Online (Sandbox Code Playgroud)
以及 Ruby 片段:
require 'message_format'
require 'test/unit/assertions'
include Test::Unit::Assertions
icumf = "{review_count, plural, =0 {{star_count, plural,other {no reviews}}} zero { {star_count, plural, zero {{review_count} reviews with {star_count} stars} one {{review_count} review with {star_count} star} two {{review_count} reviews with {star_count} stars} few {{review_count} reviews with {star_count} stars} other {{review_count} reviews with {star_count} stars}}}one {{star_count, plural, zero {{review_count} review with {star_count} stars} one {{review_count} review with {star_count} star} two {{review_count} review with {star_count} stars} few {{review_count} review with {star_count} stars} other {{review_count} review with {star_count} stars}}} two {{star_count, plural, zero {{review_count} reviews with {star_count} stars} one {{review_count} review with {star_count} star} two {{review_count} reviews with {star_count} stars} few {{review_count} reviews with {star_count} stars} other {{review_count} reviews with {star_count} stars}}} few {{star_count, plural,zero {{review_count} reviews with {star_count} stars} one {{review_count} review with {star_count} star} two {{review_count} reviews with {star_count} stars} few {{review_count} reviews with {star_count} stars} other {{review_count} reviews with {star_count} stars}}} other {{star_count, plural, zero {{review_count} reviews with {star_count} stars} one {{review_count} review with {star_count} star} two {{review_count} reviews with {star_count} stars} few {{review_count} reviews with {star_count} stars} other {{review_count} reviews with {star_count} stars}}}}"
# Set the locale to get the plural rules for that locale
message = MessageFormat.new(icumf, 'en')
assert_equal message.format({ :review_count => 0, :star_count => 0 }), 'no reviews'
assert_equal message.format({ :review_count => 0, :star_count => 100 }), 'no reviews'
assert_equal message.format({ :review_count => 1, :star_count => 2 }), '1 review with 2 stars'
assert_equal message.format({ :review_count => 2, :star_count => 5 }), '2 reviews with 5 stars'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
546 次 |
| 最近记录: |