我正在为列出产品评论摘要的商店构建 View 。我正在按分数对评论进行分组,我希望在每组顶部有一个字符串,形式为“10 条 5 星评论”。我知道如果我只想将“review”复数化,我可以在 en.rb
中做到这一点:
en: {
reviews_header: {
one: "1 review",
other: "%{count} reviews"
}
}
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"
}
}
}
}
}
我会用
t(:reviews_header, review_count: 10, star_count: 5)
得到字符串.我现在正在做的是将字符串更改为“10 条 5 星评论”的形式,这解决了“星级”复数的问题,但这在其他语言中不起作用。
最佳答案
这里有一个嵌套复数的情况。虽然我对 Ruby 的熟悉程度很初级,但我找不到任何文档可以通过 Ruby 的内置 i18n 功能为这种嵌套复数情况提供解决方案。但是,在支持 ICU Library 的编程语言中,有可能受益于 MessageFormat .
使用 this library对于 Ruby 的 MessageFormat 解析和格式化,可以手工制作一个嵌套的 MessageFormat 来覆盖这个字符串的所有变体,以覆盖任何语言中嵌套复数规则的复杂性。请记住,对于大多数语言,您不需要这些规则中的大部分,但很少有像阿拉伯语和俄语这样的语言需要许多这些情况;阿拉伯语有两个的特殊情况,俄语有一个特殊的情况(1, 21, 31, 1001,但不是 11)。可以找到来自 Unicode CLDR 项目的图表,其中列出了所有语言的复数规则 here .
通常,我训练翻译人员使用 this online tool (来自 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}}
}
}
和 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'
关于ruby-on-rails - rails 5 : Can you pluralize two words in a string with two different counts?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45199525/