Internationalization Plugin for the Sandbox API
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. | Internationalization Plugin for the Sandbox API |
Functions | |
i18n(sandbox) | Define methods in the ‘i18n’ property of given sandbox. |
sandbox. | Get the list of available languages. |
sandbox. | Get the language currently selected for the application. |
sandbox. | Select the language of the application, shared by all modules. |
sandbox. | Define or replace properties for given language. |
sandbox. | Get the value of the property identified by given key. |
sandbox. | Get a string computed by replacing data values in the most specific value found for given key, used as a string template. |
sandbox. | Replace parameters and trim nodes based on html ‘lang’ attribute. |
Get the language currently selected for the application.
string, the value of the ‘lang’ attribute of the root HTML element, or when it is missing or the empty string ‘’, the value of the browser language found in navigator.language or navigator.browserLanguage.
Select the language of the application, shared by all modules.
The language code of selected language is stored in the ‘lang’ attribute of the root HTML element. It is used as a default when the language code is omitted in calls to i18n methods where language code is optional: i18n.get(), i18n.getString(), i18n.filterHtml().
languageCode | string, the language code of the selected language |
RFC5646 | Tags for Identifying Languages http://tools.ietf.org/html/rfc5646 |
Define or replace properties for given language.
Language properties are inherited by all languages whose language code starts with the given language code:
languageCode | string, the language code identifying the language, as defined in RFC5646 “Tags for Identifying Languages” |
languageProperties | object, a JSON-like structure with language properties, which may be several levels deep and contain values of any type including functions. |
RFC5646 | Tags for Identifying Languages http://tools.ietf.org/html/rfc5646 |
Get the value of the property identified by given key.
key | string or array, the key identifiying the property: |
languageCode | string, optional, language code for lookup in a specific language. Defaults to the language selected for the whole application, as returned in getSelectedLanguage(). |
Get a string computed by replacing data values in the most specific value found for given key, used as a string template.
When a function is found for the given key instead of a string template, it is called with the key, data and language code, replaced with their default values when omitted, and its return value is used as string template. In case the call to the function template fails, null is returned instead.
Function templates may be used in place of string values in language properties to handle pluralization, for example:
function(key,data,languageCode){ return data.number <= 1 ? "goose" : "geese"; }
The parameters to replace are surrounded by ‘#’ characters, e.g. ‘#param-to-replace#’. No space can appear in the name; only characters in the range [a-zA-Z0-9_\-\.] are allowed.
Replacement values are provided as properties of the data object, with the same name as the parameter:
{ 'param-to-replace': 'value' }
Dotted parameter names, e.g. ‘#section.subsection.name#’, are replaced with values nested within sections and subsections of the data object:
{ section: { subsection: { name: 'value' } } }
In case a property is not found in the given data object, getString() is called recursively to get the string value of the property for parameter replacement.
1. the key is looked up in language properties of selected language. A string is expected. If no value is found, null is returned. If a function is found, its return value is used instead; if the function fails, null is returned.
2. any parameter found in the string value is looked up, first in the given data, then in language properties of selected language, by calling getString() recursively. A string is expected for parameter replacement.
3. the resulting string, with parameters replaced, is returned.
key | string or array, the key identifiying the property: |
data | object, optional, replacement values for parameters, which may be nested within sections and subsections. Defaults to an empty object, leaving all parameters unreplaced. |
languageCode | string, optional, language code for lookup in a specific language. Defaults to the language selected for the whole application, as returned by getSelectedLanguage(). |
Replace parameters and trim nodes based on html ‘lang’ attribute.
The given HTML node is modified in place. You should clone it beforehand if you wish to preserve the original version.
The HTML node is filtered according to the languageCode argument, or if it is omitted, the language code of the application as returned by getSelectedLanguage(). Multiple translations may be included and only relevant translations will be kept, based on ‘lang’ attribute:
<div lang=''> <span lang='de'>Hallo #user.firstName#!</span> <span lang='en'>Hi #user.firstName#!</span> <span lang='fr'>Salut #user.firstName# !</span> <span lang='jp'>こんにちは#user.lastName#!</span> </div>
Filtering the HTML from the above example for the language ‘en-GB’ would result in:
<div lang=''> <span lang='en'>Hi #user.firstName#!</span> </div>
The ‘lang’ attribute is inherited from ancestors, including ancestors of the given HTML node, unless it has a ‘lang’ attribute itself. The root element of the HTML node will be removed from its parent as well if its language does not match the language code used for filtering. Elements within the scope of the empty language ‘’ or in the scope of no language attribute are preserved by the filtering.
Parameters of the form #param# found in text and attribute nodes are replaced in the same way as using i18n.getString():
{ user: { firstName: 'Jane', lastName: 'Doe' } }
After parameter replacement, the HTML node of the above example would end up as:
<div lang=''> <span lang='en'>Hi Jane!</span> </div>
htmlNode | DOM node, the node to apply the i18n filters to |
data | object, optional, replacement values for parameters found in attributes and text of the HTML node. Defaults to an empty object |
languageCode | string, optional, language code for lookup in a specific language. Defaults to the language selected for the whole application, as returned in getSelectedLanguage(). |
Specifying the language of content: the lang attribute