Skip to content

Commit acb4b46

Browse files
author
Anne Johnson
committed
Update i18n guide to cover :zero key support in pluralization [ci skip]
1 parent 7888f4f commit acb4b46

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

guides/source/i18n.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,7 @@ The `:count` interpolation variable has a special role in that it both is interp
707707

708708
```ruby
709709
I18n.backend.store_translations :en, inbox: {
710+
zero: 'no messages', # optional
710711
one: 'one message',
711712
other: '%{count} messages'
712713
}
@@ -715,15 +716,20 @@ I18n.translate :inbox, count: 2
715716
716717
I18n.translate :inbox, count: 1
717718
# => 'one message'
719+
720+
I18n.translate :inbox, count: 0
721+
# => 'no messages'
718722
```
719723

720724
The algorithm for pluralizations in `:en` is as simple as:
721725

722726
```ruby
723-
entry[count == 1 ? 0 : 1]
727+
lookup_key = :zero if count == 0 && entry.has_key?(:zero)
728+
lookup_key ||= count == 1 ? :one : :other
729+
entry[lookup_key]
724730
```
725731

726-
I.e. the translation denoted as `:one` is regarded as singular, the other is used as plural (including the count being zero).
732+
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`.
727733

728734
If the lookup for the key does not return a Hash suitable for pluralization, an `I18n::InvalidPluralizationData` exception is raised.
729735

0 commit comments

Comments
 (0)