mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Update i18n guide to cover :zero key support in pluralization [ci skip]
This commit is contained in:
parent
7888f4fae0
commit
acb4b46076
1 changed files with 8 additions and 2 deletions
|
@ -707,6 +707,7 @@ The `:count` interpolation variable has a special role in that it both is interp
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
I18n.backend.store_translations :en, inbox: {
|
I18n.backend.store_translations :en, inbox: {
|
||||||
|
zero: 'no messages', # optional
|
||||||
one: 'one message',
|
one: 'one message',
|
||||||
other: '%{count} messages'
|
other: '%{count} messages'
|
||||||
}
|
}
|
||||||
|
@ -715,15 +716,20 @@ I18n.translate :inbox, count: 2
|
||||||
|
|
||||||
I18n.translate :inbox, count: 1
|
I18n.translate :inbox, count: 1
|
||||||
# => 'one message'
|
# => 'one message'
|
||||||
|
|
||||||
|
I18n.translate :inbox, count: 0
|
||||||
|
# => 'no messages'
|
||||||
```
|
```
|
||||||
|
|
||||||
The algorithm for pluralizations in `:en` is as simple as:
|
The algorithm for pluralizations in `:en` is as simple as:
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
entry[count == 1 ? 0 : 1]
|
lookup_key = :zero if count == 0 && entry.has_key?(:zero)
|
||||||
|
lookup_key ||= count == 1 ? :one : :other
|
||||||
|
entry[lookup_key]
|
||||||
```
|
```
|
||||||
|
|
||||||
I.e. the translation denoted as `:one` is regarded as singular, the other is used as plural (including the count being zero).
|
The translation denoted as `:one` is regarded as singular, and the `:other` is used as plural. If the count is zero, and a `:zero` entry is present, then it will be used instead of `:other`.
|
||||||
|
|
||||||
If the lookup for the key does not return a Hash suitable for pluralization, an `I18n::InvalidPluralizationData` exception is raised.
|
If the lookup for the key does not return a Hash suitable for pluralization, an `I18n::InvalidPluralizationData` exception is raised.
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue