Base data storage of language properties for Internationalization (i18n)
This module provides the basis for the storage and retrieval of language properties for the adaptation of a web application to different languages in an international context.
This module provides a generic storage for language properties associated with language codes. Any type of language properties may be defined by an an application:
A language code is a string which identifies the language, region and other variations of the language as defined in RFC5646 “Tags for Identifying Languages”, for example:
The lookup of language properties is done from the most specific language to the least specific language, with longer language codes considered more specific than shorter language codes. Only language codes which are substrings of the language selected for the lookup are considered. For example, if the selected language is ‘en-GB’, ‘en-GB’ and ‘en’ are considered in this order, while ‘en-US’, ‘fr-FR’, ‘fr-CA’ and ‘fr’ are left out. Language codes are compared in a case-insensitive way.
The empty string ‘’ is the least specific language code possible, which will always be considered last in the lookup process. Common default properties can be associated with the empty language code ‘’, they will be shared by all languages.
Any custom property may be defined in language properties for the needs of your application, and associated with a language code by calling addLanguageProperties(). Calling reset() removes all language properties.
The list of language codes associated with language properties is returned by getLanguageCodes(). It is initially empty.
Eric Bréchemier © 2011, Some Rights Reserved Legal-Box SAS © 2010-2011, All Rights Reserved
BSD License http://creativecommons.org/licenses/BSD/
2011-08-14
lb. | Base data storage of language properties for Internationalization (i18n) |
Functions | |
getLanguageCodes(): array | Get the list of language codes associated with language properties. |
addLanguageProperties(languageCode,languageProperties) | Add or replace language properties associated with given language code. |
getDefaultLanguageCode(): string | Get the default language code for use in internationalization methods. |
get(key[,languageCode]): any | Get the value of the property identified by given key, in the most specific language available. |
reset() | Remove all language properties. |
Get the list of language codes associated with language properties.
array of strings, the list of unique language codes with associated language properties, sorted in case-insensitive lexical order.
Language codes are returned AS IS, but in case the same language code has been registered several times, comparing in a case-insensitive manner, duplicates are not included in the list. Language codes are not currently normalized to a lower case form in the resulting list; this may be done in a future implementation.
Add or replace language properties associated with given language code.
Language properties may be specified in multiple calls with the same language code. In case of duplicate properties, the properties defined last are considered more specific and take precedence over properties defined previously.
languageCode | string, the language code identifying the language, as defined in RFC5646 “Tags for Identifying Languages” |
languageProperties | object, a set of language properties |
Nothing happens in case the given language code is not a string.
Get the default language code for use in internationalization methods.
This method is intended to provide a default value to optional language code arguments of base internationalization methods.
string, the value of the ‘lang’ attribute of the root HTML element, or when it is missing or an empty string ‘’, the value of the browser language found in navigator.language or navigator.browserLanguage.
Get the value of the property identified by given key, in the most specific language available.
The key argument may be a string or an array of strings:
The last two forms are equivalent, both matching a property ‘propertyName’ nested in a property ‘subsection’ within a property ‘section’ at top level of language properties. The array notation allows to look up a property which would contain a dot in its name, without the substitution to a section and subsection: [‘no.substitution.done’].
key | string, the name of the looked up property such as ‘name’, or string, a dotted string such as ‘section.subsection.name’, or an array of strings to represent a path to a property such as [‘section’,’subsection’,’name’] nested within sections and subsections |
languageCode | string, optional, the language code used to filter relevant languages, defaults to the value of getDefaultLanguageCode() |