ClassI18nInternal

Internal support for selecting messages to render, with placeholder interpolation and locale-aware number formatting and pluralisation

Constructors

  • Internal

    Parameters

    • translations: {
          [key: string]: string | TranslationPluralForms;
      } = {}

      Key-value pairs of the translation strings to use.

    • Optionalconfig: {
          locale: string;
      } = {}

      Configuration options for the function.

      • locale: string

        An overriding locale for the PluralRules functionality.

    Returns I18n

Properties

locale: string
translations: {
    [key: string]: string | TranslationPluralForms;
}
pluralRules: {
    [key: string]: ((count: number) => PluralRule);
} = ...

Different pluralisation rule sets

Returns the appropriate suffix for the plural form associated with n. Possible suffixes: 'zero', 'one', 'two', 'few', 'many', 'other' (the actual meaning of each differs per locale). 'other' should always exist, even in languages without plurals, such as Chinese. https://cldr.unicode.org/index/cldr-spec/plural-rules

The count must be a positive integer. Negative numbers and decimals aren't accounted for

pluralRulesMap: {
    [key: string]: string[];
} = ...

Map of plural rules to languages where those rules apply.

Note: These groups are named for the most dominant or recognisable language that uses each system. The groupings do not imply that the languages are related to one another. Many languages have evolved the same systems independently of one another.

Code to support more languages can be found in the i18n spike: https://github.com/alphagov/govuk-frontend/blob/spike-i18n-support/src/govuk/i18n.mjs

Languages currently supported:

Arabic: Arabic (ar) Chinese: Burmese (my), Chinese (zh), Indonesian (id), Japanese (ja), Javanese (jv), Korean (ko), Malay (ms), Thai (th), Vietnamese (vi) French: Armenian (hy), Bangla (bn), French (fr), Gujarati (gu), Hindi (hi), Persian Farsi (fa), Punjabi (pa), Zulu (zu) German: Afrikaans (af), Albanian (sq), Azerbaijani (az), Basque (eu), Bulgarian (bg), Catalan (ca), Danish (da), Dutch (nl), English (en), Estonian (et), Finnish (fi), Georgian (ka), German (de), Greek (el), Hungarian (hu), Luxembourgish (lb), Norwegian (no), Somali (so), Swahili (sw), Swedish (sv), Tamil (ta), Telugu (te), Turkish (tr), Urdu (ur) Irish: Irish Gaelic (ga) Russian: Russian (ru), Ukrainian (uk) Scottish: Scottish Gaelic (gd) Spanish: European Portuguese (pt-PT), Italian (it), Spanish (es) Welsh: Welsh (cy)

Methods

  • Internal

    Work out which pluralisation rules to use for the current locale

    The locale may include a regional indicator (such as en-GB), but we don't usually care about this part, as pluralisation rules are usually the same regardless of region. There are exceptions, however, (e.g. Portuguese) so this searches by both the full and shortened locale codes, just to be sure.

    Returns string

    The name of the pluralisation rule to use (a key for one of the functions in this.pluralRules)

  • Internal

    Get the appropriate suffix for the plural form.

    Uses Intl.PluralRules (or our own fallback implementation) to get the 'preferred' form to use for the given count.

    Checks that a translation has been provided for that plural form – if it hasn't, it'll fall back to the 'other' plural form (unless that doesn't exist either, in which case an error will be thrown)

    Parameters

    • lookupKey: string

      The lookup key of the string to use.

    • count: number

      Number used to determine which pluralisation to use.

    Returns PluralRule

    The suffix associated with the correct pluralisation for this locale.

    Plural form .other required when preferred plural form is missing

  • Internal

    Check to see if the browser supports Intl.PluralRules

    It requires all conditions to be met in order to be supported:

    • The implementation of Intl supports PluralRules (NOT true in Safari 10–12)
    • The browser/OS has plural rules for the current locale (browser dependent)

    https://browsersl.ist/#q=supports+es6-module+and+not+supports+intl-pluralrules

    Returns boolean

    Returns true if all conditions are met. Returns false otherwise.

  • Internal

    Takes a translation string with placeholders, and replaces the placeholders with the provided data

    Parameters

    • translationString: string

      The translation string

    • options: {
          [key: string]: unknown;
      }

      Any options passed with the translation string, e.g: for string interpolation.

      • [key: string]: unknown

    Returns string

    The translation string to output, with ${} placeholders replaced

  • Internal

    Get the plural form using our fallback implementation

    This is split out into a separate function to make it easier to test the fallback behaviour in an environment where Intl.PluralRules exists.

    Parameters

    • count: number

      Number used to determine which pluralisation to use.

    Returns PluralRule

    The pluralisation form for count in this locale.

  • Internal

    The most used function - takes the key for a given piece of UI text and returns the appropriate string.

    Parameters

    • lookupKey: string

      The lookup key of the string to use.

    • Optionaloptions: {
          [key: string]: unknown;
      }

      Any options passed with the translation string, e.g: for string interpolation.

      • [key: string]: unknown

    Returns string

    The appropriate translation string.

    Lookup key required

    Options required for ${} placeholders