-
Notifications
You must be signed in to change notification settings - Fork 339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add pluralisation support to i18n functionality #2853
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is very, very cool.
Well commented, tests seem to make sense.
is there anything that we've used as a reference, copied or adapted that should be credited either in the commit messages or comments? (And do we need to check the licensing on anything?)
I think this also closes #2568 – worth associate this PR with it and moving it into Needs Review so nobody else tries to pick it up? |
@36degrees Good point. Have added it. |
toEqual runs deep equality checking, however as we're only comparing strings, we don't need this ability.
Expands the i18n functionality to add pluralisation support, via passing the special
count
option through thet()
method. Doing so will locate the appropriate plural form suffix for the current locale and append this to the lookup key.Suffixes are based on the Unicode CLDR specification and align with the output of the Intl.PluralRules implementation.
For example, calling the method with
testKey
will instead use the following lookup keys in the configured locales:Closes #2804.
Changes
config
parameter to the I18n constructor. Currently this only has one option,locale
. Unlike thelocale
option in earlier spikes, this one is only intended to be used internally and isn't configurable by users. Falls back to the document language if not specified.t
method to check for the presence of acount
option and, if present, callsgetPluralSuffix
and modifies the lookup key to point to the appropriate plural form.replacePlaceholders
method to check if the placeholder is of type 'number' and, if so, to format it according to the configured locale. The number will not be formatted ifIntl.NumberFormat
isn't supported.hasIntlPluralRulesSupport
: Checks for browser support forIntl.PluralRules
and related dependencies. Used bygetPluralSuffix
.hasIntlNumberFormatSupport
: Checks for browser support forInit.NumberFormat
and related dependencies. Used byreplacePlaceholders
.getPluralSuffix
: Gets the CLDR specification plural form type (zero
,one
,two
,few
,many
orother
) based on the given number and configured locale. Falls back to the hardcoded plural rules ifIntl.PluralRules
is not supported.pluralRules
: Provides a fallback set of functions for determining the correct plural suffix to use for a given number and language.pluralRulesMap
: Object mapping language codes to the appropriate function withinpluralRules
.translations
object.count
option is possible and uses the appropriate plural form for the value ofcount
.pluralRules
functions produce values consistent withIntl.PluralRules
.Supported fallback languages
Browsers that support
Intl.PluralRules
theoretically support any language the browser vendor chooses to support. For older browsers (primarily Internet Explorer), we provide a set of fallback functions for a limited set of languages.We don't have any definitive data on what languages are used by forms services on GOV.UK (the main expected use case for these changes). This list has been compiled by combining languages we know are used by GOV.UK services, some assumptions, and languages that, although not in known use, have plural rules identical to other supported languages and are 'quick wins' for support.
Known in use by GOV.UK services:
Languages that are assumed may be in use:
'Quick win' languages: