Base Internationalization (i18n) Module
This module provides the basis for the adaptation of a web application to different languages in an international context.
This module collects utility methods related to the language of the browser, the language of DOM elements, and comparison of language codes.
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 definition and lookup of language properties associated with language codes is managed in lb.base.i18n.data.
Eric Bréchemier © 2011-2013, Some Rights Reserved Legal-Box SAS © 2010-2011, All Rights Reserved
BSD License http://creativecommons.org/licenses/BSD/
2013-09-10
lb. | Base Internationalization (i18n) Module |
Functions | |
getBrowserLanguage(): string | Get the browser’s language. |
getLanguage([htmlElement]): string | Get the language of given HTML element. |
setLanguage(languageCode[,htmlElement]) | Set the language of given HTML element. |
languageCompare(languageCode1, languageCode2): integer | A comparator function suitable for use in array.sort(). |
equals(languageCode1, languageCode2): boolean | Check whether two language codes are considered equal. |
contains(languageCode1, languageCode2): boolean | Check whether second language code inherits from first language code. |
Get the browser’s language.
string, the language code of the browser’s language, as retrieved in navigator.language or navigator.browserLanguage.
window.navigator.language | MDC Doc Center https://developer.mozilla.org |
navigator Object | MSDN http://msdn.microsoft.com |
Get the language of given HTML element.
The language is computed by looking at the value of the ‘lang’ attribute of the node itself, then looking for a value inherited from the closest ancestor defining a ‘lang’ attribute. The value ‘’ (empty string) is returned either when no language matched or when a ‘lang’ attribute is found set to the explicit value ‘’.
In this implementation, only the ‘lang’ attribute is considered. A future version may take the ‘xml:lang’ attribute into account as well.
This method can be called without argument to return the language of the document element.
htmlElement | DOM Node, optional, defaults to the root HTML element, a DOM element. |
string, the value of the first ‘lang’ attribute found on the node or its closest ancestor element, or the empty string ‘’ by default.
Set the language of given HTML element.
The method can be called with a single argument to set the language of the document element.
In current implementation, the language is set to the ‘lang’ attribute of given node only. It may also be set to the ‘xml:lang’ attribute in a future version.
languageCode | string, the language code identifying the language, as defined in RFC5646 “Tags for Identifying Languages” |
htmlElement | DOM Element, optional, defaults to root HTML element, the DOM element to set the language to. |
Nothing happens in case the language code is not a string or the given html node is not an element.
A comparator function suitable for use in array.sort().
Languages are compared in a case-insensitive way. They are then sorted in lexical order. This ensures that in each family, language codes are sorted from least specific (shortest) to most specific (longest).
languageCode1 | string, the first language code for the comparison, as defined in RFC5646 “Tags for Identifying Languages” |
languageCode2 | string, the second language code for the comparison, as defined in RFC5646 “Tags for Identifying Languages” |
The result is undefined in case one or both of given language codes is not a string.
Check whether two language codes are considered equal.
Language codes are compared in a case-insensitive way.
languageCode1 | string, the first language code for the comparison, as defined in RFC5646 “Tags for Identifying Languages” |
languageCode2 | string, the second language code for the comparison, as defined in RFC5646 “Tags for Identifying Languages” |
The result is undefined in case one or both language codes is not a string.
Check whether second language code inherits from first language code.
Language codes are compared in a case-insensitive way. A language code is considered as heir of another when it is found as an hyphen-separated substring at the start of the other language code.
languageCode1 | string, the first language code for the comparison, as defined in RFC5646 “Tags for Identifying Languages” |
languageCode2 | string, the second language code for the comparison, as defined in RFC5646 “Tags for Identifying Languages” |
The result is undefined in case one or both language codes is not a string.