Class

Locale

Locale(language, regionopt, variantopt, scriptopt)

Create a new locale instance. Locales are specified either with a specifier string that follows the BCP-47 convention (roughly: "language-region-script-variant") or with 4 parameters that specify the language, region, variant, and script individually.

The language is given as an ISO 639-1 two-letter, lower-case language code. You can find a full list of these codes at http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes

The region is given as an ISO 3166-1 two-letter, upper-case region code. You can find a full list of these codes at http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2.

The variant is any string that does not contain a dash which further differentiates locales from each other.

The script is given as the ISO 15924 four-letter script code. In some locales, text may be validly written in more than one script. For example, Serbian is often written in both Latin and Cyrillic, though not usually mixed together. You can find a full list of these codes at http://en.wikipedia.org/wiki/ISO_15924#List_of_codes.

As an example in ilib, the script can be used in the date formatter. Dates formatted in Serbian could have day-of-week names or month names written in the Latin or Cyrillic script. Often one script is default such that sr-SR-Latn is the same as sr-SR so the script code "Latn" can be left off of the locale spec.

Each part is optional, and an empty string in the specifier before or after a dash or as a parameter to the constructor denotes an unspecified value. In this case, many of the ilib functions will treat the locale as generic. For example the locale "en-" is equivalent to "en" and to "en--" and denotes a locale of "English" with an unspecified region and variant, which typically matches any region or variant.

Without any arguments to the constructor, this function returns the locale of the host Javascript engine.

Constructor

# new Locale(language, regionopt, variantopt, scriptopt)

Parameters:
Name Type Attributes Description
language string | Locale

the ISO 639 2-letter code for the language, or a full locale spec in BCP-47 format, or another Locale instance to copy from

region string <optional>

the ISO 3166 2-letter code for the region

variant string <optional>

the name of the variant of this locale, if any

script string <optional>

the ISO 15924 code of the script for this locale, if any

View Source Locale.js, line 69

Methods

# equals() → {boolean}

Return true if the the other locale is exactly equal to the current one.

View Source Locale.js, line 1042

whether or not the other locale is equal to the current one

boolean

# getLangSpec() → {string}

Return the language locale specifier. This includes the language and the script if it is available. This can be used to see whether the written language of two locales match each other regardless of the region or variant.

View Source Locale.js, line 1020

the language locale specifier

string

# getLanguage() → {string|undefined}

Return the ISO 639 language code for this locale.

View Source Locale.js, line 959

the language code for this locale

string | undefined

# getLanguageAlpha3() → {string|undefined}

Return the language of this locale as an ISO-639-alpha3 language code

View Source Locale.js, line 967

the alpha3 language code of this locale

string | undefined

# getRegion() → {string|undefined}

Return the ISO 3166 region code for this locale.

View Source Locale.js, line 975

the region code of this locale

string | undefined

# getRegionAlpha3() → {string|undefined}

Return the region of this locale as an ISO-3166-alpha3 region code

View Source Locale.js, line 983

the alpha3 region code of this locale

string | undefined

# getScript() → {string|undefined}

Return the ISO 15924 script code for this locale

View Source Locale.js, line 991

the script code of this locale

string | undefined

# getSpec() → {string}

Return the whole locale specifier as a string.

View Source Locale.js, line 1007

the locale specifier

string

# getVariant() → {string|undefined}

Return the variant code for this locale

View Source Locale.js, line 999

the variant code of this locale, if any

string | undefined

# isPseudo() → {boolean}

Return true if the current locale is the special pseudo locale.

View Source Locale.js, line 1053

true if the current locale is the special pseudo locale

boolean

# isValid() → {boolean}

Return true if the current locale uses a valid ISO codes for each component of the locale that exists.

View Source Locale.js, line 1063

true if the current locale has all valid components, and false otherwise.

boolean

# toString() → {string}

Express this locale object as a string. Currently, this simply calls the getSpec function to represent the locale as its specifier.

View Source Locale.js, line 1034

the locale specifier

string

# static getAvailableLocales(sync, onLoad) → {Array.<string>}

Return the list of available locales that this iLib file supports. If this copy of ilib is pre-assembled with locale data, then the list locales may be much smaller than the list of all available locales in the iLib repository. The assembly tool will automatically fill in the list for an assembled copy of iLib. If this copy is being used with dynamically loaded data, then you can load any locale that iLib supports. You can form a locale with any combination of a language and region tags that exist in the locale data directory. Language tags are in the root of the locale data dir, and region tags can be found underneath the "und" directory. (The region tags are separated into a different dir because the region names conflict with language names on file systems that are case-insensitive.) If you have culled the locale data directory to limit the size of your app, then this function should return only those files that actually exist according to the ilibmanifest.json file in the root of that locale data dir. Make sure your ilibmanifest.json file is up-to-date with respect to the list of files that exist in the locale data dir.

Parameters:
Name Type Description
sync boolean

if false, load the list of available files from disk asynchronously, otherwise load them synchronously. (Default: true/synchronously)

onLoad function

a callback function to call if asynchronous load was requested and the list of files have been loaded.

View Source Locale.js, line 1106

this is an array of locale specs for which this iLib file has locale data for

Array.<string>

# static languageAlpha1ToAlpha3(alpha1) → {string|undefined}

Return the ISO-639 alpha3 equivalent language code for the given ISO 639 alpha1 language code. If the given alpha1 code is not found, this function returns its argument unchanged.

Parameters:
Name Type Description
alpha1 string | undefined

the alpha1 code to map

View Source Locale.js, line 922

the alpha3 equivalent of the given alpha1 code, or the alpha1 parameter if the alpha1 value is not found

string | undefined

# static regionAlpha2ToAlpha3(alpha2) → {string|undefined}

Return the ISO-3166 alpha3 equivalent region code for the given ISO 3166 alpha2 region code. If the given alpha2 code is not found, this function returns its argument unchanged.

Parameters:
Name Type Description
alpha2 string | undefined

the alpha2 code to map

View Source Locale.js, line 909

the alpha3 equivalent of the given alpha2 code, or the alpha2 parameter if the alpha2 value is not found

string | undefined