2017-07-19 61 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代碼段以下

相關問題