JAVASCRIPT
Internationalization Utilities
All samples are using Ink.requireModules
, please read how to use it at Ink.requireModules section
You can use this module to internationalize your applications. It roughly emulates GNU gettext's API.
Method name | Description |
---|---|
new I18n(dict, [lang], [testMode]) | Constructor |
.alias() | Create an alias. |
.append(dict) | Adds translation strings for the helper to use. |
.appendGlobal(dict, lang) | Adds a dictionary to be used in all I18n instances for the corresponding language. |
.getKey(key) | Gest a key from the current dictionary |
.lang([lang]) | Gets or sets the language. |
.langGlobal([lang]) | Gets or sets the current default language of I18n instances. |
.ntext(strSin, strPlur, count) | Translates and pluralizes text. |
.ordinal(num, [options]) | Gets the ordinal suffix of a number. |
.reset() | Resets I18n global state (global dictionaries, and default language for instances) |
.testMode([newTestMode]) | Sets or unsets test mode. |
.text(str, [namedParms], [args], [arg2]) | Translates a string. |
dict
Object mapping language codes (in the form of `pt_PT`, `pt_BR`, `fr`, `en_US`, etc.) to their `dictionaries`lang
'pt_PT'language code of the target languagetestMode
falseSets the test mode (see `testMode()`) on construction.Some API functions
Code
Create an alias.
Returns an alias to this I18n instance. It contains the I18n methods documented here, but is also a function. If you call it, it just calls text()
. This is commonly assigned to "_".
Adds translation strings for the helper to use.
dict
Object containing language objects identified by their language codeAdds a dictionary to be used in all I18n instances for the corresponding language.
dict
Dictionary to be addedlang
Language fo the dictionary being addedGest a key from the current dictionary
key
Key you wish to get from the dictionary.Gets or sets the language.
If there are more dictionaries available in cache, they will be loaded.
lang
Language code to set this instance to. Omit this argument if you want to get the language code instead.Gets or sets the current default language of I18n instances.
lang
the new language for all I18n instances. Omit this argument if you wish to *get* the current default language instead.Translates and pluralizes text.
Given a singular string, a plural string and a number, translates either the singular or plural string.
strSin
Word to use when count is 1strPlur
Word to use otherwisecount
Number which defines which word to useargs...
Extra arguments, to be passed to `text()`Gets the ordinal suffix of a number.
This works by using transforms (in the form of Objects or Functions) passed into the function or found in the special key _ordinals
in the active language dictionary.
num
Input numberoptions
{}Dictionaries for translating. Each of these options' fallback is found in the current language's dictionary. The lookup order is the following: `exceptions`, `byLastDigit`, `default`. Each of these may be either an `Object` or a `Function`. If it's a function, it is called (with `number` and `digit` for any function except for byLastDigit, which is called with the `lastDigit` of the number in question), and if the function returns a string, that is used. If it's an object, the property is looked up using `obj[prop]`. If what is found is a string, it is used directly.options.byLastDigit
{}If the language requires the last digit to be considered, mappings of last digits to ordinal suffixes can be created here.options.exceptions
{}Map unique, special cases to their ordinal suffixes.Resets I18n global state (global dictionaries, and default language for instances)
Sets or unsets test mode.
In test mode, unknown strings are wrapped in [ ... ]
. This is useful for debugging your application and to make sure all your translation keys are in place.
newTestMode
Flag to set the test mode state. Omit this argument to *get* the current testMode instead.Translates a string.
Given a translation key, return a translated string, with replaced parameters.
When a translated string is not available, the original string is returned unchanged.
str
Key to look for in i18n dictionary (which is returned verbatim if unknown)namedParms
Named replacements. Replaces {named} with values in this object.args
Replacement #1 (replaces first {} and all {1})arg2
Replacement #2 (replaces second {} and all {2})argn...
Replacement #n (replaces nth {} and all {n})