aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJon Moss <maclover7@users.noreply.github.com>2017-02-26 17:33:13 -0500
committerGitHub <noreply@github.com>2017-02-26 17:33:13 -0500
commitb10f601e9b8e8b0a5f40e2f839bfde5e7220a44b (patch)
tree933412bdeccf2ab562c190b4bb834736f71b589e
parent7888f4fae0b0d0b63fde1645feb61c588bb4c010 (diff)
parentacb4b4607655b4fc9f0e3ea70b9452fefe41ca6e (diff)
downloadrails-b10f601e9b8e8b0a5f40e2f839bfde5e7220a44b.tar.gz
rails-b10f601e9b8e8b0a5f40e2f839bfde5e7220a44b.tar.bz2
rails-b10f601e9b8e8b0a5f40e2f839bfde5e7220a44b.zip
Merge pull request #28190 from annejohnson/update-i18n-guide-with-zero-opt
Update i18n guide to cover :zero key support in pluralization [ci skip]
-rw-r--r--guides/source/i18n.md10
1 files changed, 8 insertions, 2 deletions
diff --git a/guides/source/i18n.md b/guides/source/i18n.md
index ed8cf8a344..6c8706bc13 100644
--- a/guides/source/i18n.md
+++ b/guides/source/i18n.md
@@ -707,6 +707,7 @@ The `:count` interpolation variable has a special role in that it both is interp
```ruby
I18n.backend.store_translations :en, inbox: {
+ zero: 'no messages', # optional
one: 'one message',
other: '%{count} messages'
}
@@ -715,15 +716,20 @@ I18n.translate :inbox, count: 2
I18n.translate :inbox, count: 1
# => 'one message'
+
+I18n.translate :inbox, count: 0
+# => 'no messages'
```
The algorithm for pluralizations in `:en` is as simple as:
```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.