这里有一个嵌套复数的例子。虽然我对Ruby的熟悉程度是基本的,但是我找不到任何通过Ruby的内置i18n特性为这个嵌套复数情况提供解决方案的文档。但是,在支持ICU Library的编程语言中,可能受益于MessageFormat。
使用this library为Ruby for MessageFormat解析和格式化,可以手动嵌套MessageFormat来覆盖此字符串的所有变体,以涵盖任何语言中嵌套复数规则的复杂性。请记住,大多数语言不需要大多数这些规则,但阿拉伯语和俄语等语言很少需要这些情况;阿拉伯语有两种特殊情况,俄语有特殊情况(1,21,31,1001,但不是11)。来自Unicode CLDR项目的图表列出了所有语言的复数规则可以找到here。
我通常会训练翻译人员使用this online tool(来自message-format-rb
的同一开发人员),并根据其语言的需要翻译MessageFormat。
{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'
下面是一个完整的,最大的MessageFormat由一个Ruby代码段以下