i18n

Utilities for dealing with internationalization (i18n).

When requiring laxar, it is available as laxar.i18n.

Contents

Module Members

Types

Module Members

localize()

Shortcut to localizeRelaxed.

localizeStrict( languageTag, i18nValue, optionalFallback )

Localize the given internationalized object using the given languageTag.

Parameters
Property Type Description
languageTag String the languageTag to lookup a localization with. Maybe undefined, if the value is not i18n (app does not use i18n)
i18nValue * a possibly internationalized value:
- when passing a primitive value, it is returned as-is
- when passing an object, the languageTag is used as a key within that object
optionalFallback * a value to use if no localization is available for the given language tag
Returns
Type Description
* the localized value if found, undefined otherwise

localizeRelaxed( languageTag, i18nValue, optionalFallback )

For controls (such as a date-picker), we cannot anticipate all required language tags, as they may be app-specific. The relaxed localize behaves like localize if an exact localization is available. If not, the language tag is successively generalized by stripping off the rightmost sub-tags until a localization is found. Eventually, a fallback ('en') is used.

Parameters
Property Type Description
languageTag String the languageTag to lookup a localization with. Maybe undefined, if the value is not i18n (app does not use i18n)
i18nValue * a possibly internationalized value:
- when passing a primitive value, it is returned as-is
- when passing an object, the languageTag is used to look up a localization within that object
optionalFallback * a value to use if no localization is available for the given language tag
Returns
Type Description
* the localized value if found, the fallback undefined otherwise

localizer( languageTag, optionalFallback )

Encapsulate a given languageTag in a partially applied localize function.

Parameters
Property Type Description
languageTag String the languageTag to lookup localizations with
optionalFallback * a value to use by the localizer function whenever no localization is available for the language tag
Returns
Type Description
Localizer A single-arg localize-Function, which always uses the given language-tag. It also has a .format
-method, which can be used as a shortcut to string.format( localize( x ), args )

languageTagFromI18n( i18n, optionalFallbackLanguageTag )

Retrieve the language tag of the current locale from an i18n model object, such as used on the scope.

Parameters
Property Type Description
i18n [object Object] an internationalization model, with reference to the currently active locale and a map from locales to language tags
optionalFallbackLanguageTag * a language tag to use if no tags are found on the given object
Returns
Type Description
String the localized value if found, undefined otherwise

Types

Localizer

Localizer#format( i18nValue, optionalIndexedReplacements, optionalNamedReplacements )

Shortcut to string.format, for simple chaining to the localizer.

These are equal:

  • string.format( i18n.localizer( tag )( i18nValue ), numericArgs, namedArgs )
  • i18n.localizer( tag ).format( i18nValue, numericArgs, namedArgs ).
Parameters
Property Type Description
i18nValue String the value to localize and then format
optionalIndexedReplacements Array replacements for any numeric placeholders in the localized value
optionalNamedReplacements Object replacements for any named placeholders in the localized value
Returns
Type Description
String the formatted string, taking i18n into account