Module: hcje/translation

Module for handling internationalisation.

Source:

Methods

(static) getPreferredLanguage() → {string}

Get preferred language from the navigator language property.

Source:
Returns:
Type
string

(static) i18n(strs, expressions) → {string}

Tag function that will translate a template literal; see [Tagged templates]{https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates}. See translate.

To use, just add the i18n tag in front of a template literal. E.g. i18n`translate me`.

Parameters:
Name Type Description
strs Array.<string>

Array of strings from the template literal.

expressions *

The results of the expressions in the template literal.

Source:
Returns:

Translated string

Type
string

(static) loadData(config) → {Promise}

Load the a file based on the language. Looks for a file called -XX.extension, where XX is the language code in lowercase. The function takes the base name and affixes the language when searchingr; so for the language fr and a base name of i18n.json, the function will search for i18n-fr.json first, then falling back to fallbackLanguageCode on error.

Parameters:
Name Type Description
config Object
Properties
Name Type Attributes Default Description
baseUrl string

Url without any language identification.

languageCode string <optional>

two character ISO-639 code. If not provided, fallsback to the user's preferred language.

fallbacklanguageCode string <optional>
'en'

Language used if languageCode fails.

asJson boolean

True if loading a json file.

filter Array.<string>

If provided, only language codes in the filter will be checked. This does not filter the fallback. This should be an array of language codes for which a translation exists.

Source:
Returns:

Fulfils to a string or object depending on whether the json flag is true.

Type
Promise

(static) loadTranslation(config, config) → {Promise}

Load the language. Looks for a file called i18n-XX.json in the base folder, where XX is the language code in lowercase. The function takes the base name and affixes the language when searching; so for the language fr and a base name of i18n.json, the function will search for i18n-fr.json first and fall back to fallbackLanguageCode on error.

Parameters:
Name Type Description
config Object
config string

baseUrl - The url without any specific language identification.

Properties
Name Type Description
languageCode string

Two character ISO-639 code.

fallbackLanguageCode string

Language to use if languageCode fails

filter Array.<string>

If provided, only language codes in the filter will be checked. This does not filter the fallback. This array should contain the language codes for which a translation exists.

Source:
Returns:

Fulfils to true if successful.

Type
Promise

(static) translate(key) → {string}

Translate a key word. If no translation available, the key word is returned unchanged. In order to help with debugging, the flag flag-translation can be added to query string of the url used to launch the app. If this exists in the search parameters, strings that are translated are enclosed within '[tx=' and ']'. If the string has been parsed but no translation was found, the result is enclosed within '[tx?' and ']'. This allows easy identification of strings that have not be parsed, strings that have been parsed but do not have a translation, and strings that have been parsed and successfully translated.

Parameters:
Name Type Description
key string

The text or key to translate.

Source:
Returns:
Type
string

(inner) addLanguageCodeToUrl(baseFile, languageCode) → {string}

Convert a url to a language version. Only applicable to json, txt or md files. Add -XX to the base name where XX is the ISO 639 language code. Only urls for json, txt or md files are modified.

For example, for the url 'https://henspace.com/myFile.md' and country code of 'en' the result would be 'https://henspace.com/myFile-en.md'.

Parameters:
Name Type Description
baseFile string

Url for the translation.

languageCode string

ISO 639 language code to use with url.

Source:
Returns:
Type
string