2017-07-19 99 views
3

我正在为商店建立一个视图,列出产品评论的摘要。我按评分对评论进行分组,并且我想要在每组“10颗5颗星评论”的每个组的顶部添加一个字符串。我知道,如果我只是想变复数“审查”,我可以在en.rb做到这一点:Rails 5:你可以用两个不同的数字复数化一个字符串中的两个单词吗?

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五星评论”的形式,它解决了复数化“明星”的问题,但这不适用于其他语言。

回答

0

这里有一个嵌套复数的例子。虽然我对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 for Translators tool

下面是一个完整的,最大的MessageFormat由一个Ruby代码段以下

相关问题