imf
v5.0.2
Published
A convenience wrapper for Yahoo's intl-messageformat localization library
Downloads
25
Readme
imf
This package is now deprecated in favor of intl-dom
A convenience wrapper for intl-messageformat.
Installation
npm install imf
Usage (Browser)
<script src="node_modules/intl-messageformat/dist/intl-messageformat-with-locales.min.js"></script>
<script src="node_modules/imf/dist/index.js"></script>
or:
import IMF from './node_modules/imf/dist/index-es6.js';
Usage (Node)
const IMF = require('imf');
Example
For the following JSON file structure...
{
"Localized value!": "Chinese translation of \"Localized value!\"",
"tablekey.Tablekey localized value!": "Chinese translation of \"tablekey.Tablekey localized value!\"",
"tablekey": {
"nestedMore": {
"Tablekey localized value2": "Put another Chinese translation of \"tablekey.Tablekey localized value!\" here"
}
}
}
OR
{
"Localized value!": "Chinese translation of \"Localized value!\"",
"tablekey": {
"Tablekey localized value!": "Chinese translation of \"tablekey.Tablekey localized value!\"",
"nestedMore": {
"Tablekey localized value2": "Put another Chinese translation of \"tablekey.Tablekey localized value!\" here"
}
}
}
OR
{
"Localized value!": "Chinese translation of \"Localized value!\"",
"tablekey.Tablekey localized value!": "Chinese translation of \"tablekey.Tablekey localized value!\"",
"tablekey.nestedMore.Tablekey localized value2": "Put another Chinese translation of \"tablekey.Tablekey localized value!\" here"
}
...etc., one could do:
IMF({
languages: ['zh-CN', 'en-US'],
callback: function (l, getFormatter /*, enUSLocale, esLocale, ptLocale, zhCNLocale*/) {
alert(l("Localized value!")); // Looks up "Localized value!" in Chinese file (at "locales/zh-CN.json") and in English (at "locales/en-US.json") if not present in Chinese
const tk = getFormatter('tablekey');
alert(tk("Tablekey localized value!")); // Equivalent to l("tablekey.Tablekey localized value!")
// Note that the following two sets are equivalent
const tk2 = getFormatter(['tablekey', 'nestedMore']);
alert(tk2("Tablekey localized value2"));
const tk3 = getFormatter('tablekey.nestedMore');
alert(tk3("Tablekey localized value2"));
}
});
API
Methods
Constructor(options) - A constructor (which can be used without
new
) to be supplied an options object with the following optional properties:languages
- Array of BCP 47 language tags in order of preference. Can also be a string for a single tag (e.g., "en-US"), or, if not supplied, will default tonavigator.language
oren-US
. Will be used as the second argument to theIntlMessageFormat
constructor.locales
- Array of locale objects (see locale JSON file structure above). Will set thelocales
property if present. If present,langs
should also be set.langs
- Array of BCP 47 language tags. Will be used to preset thelangs
property. Should only be provided iflocales
is manually provided.fallbackLanguages
- Allows a language or set of languages in the same format aslanguages
. Its message will be supplied to thefallback
callback argument of the formatter function (or used in a return value iffallback
is notfalse
) whenever no message is found forlanguages
.namespace
- Initial namespace for the formatter function supplied as the first argument tocallback
. Defaults to value ofdefaultNamespace
. Namespaces may be expressed as an array of namespace parts or as a string. Will be used in conjunction with a separator. See the example above for an illustration of the two allowable JSON file format structures. Note that namespace parts can be indefinitely nested whether as sub-objects or as additional separator-separated strings.defaultNamespace
- Used when a namespace is not provided togetFormatter
. Defaults to empty string.defaultSeparator
- Separator to be used with namespaces. Defaults to.
.basePath
- Base path for locale file resolution. Defaults tolocales/
.localeFileResolver
- Accepts a BCP 47 language tag as an argument, and should return a file path for obtaining the corresponding JSON locale file. Defaults to a function which usesbasePath
and then adds.json
to the supplied language tags for the file name within the base path.callback
- Invoked upon the IMF() object and supplied with the following arguments upon loading of locales:A formatter function based on
namespace
(ordefaultNamespace
or the empty string if none supplied)A generic formatter building function (see
getFormatter
method)Retrieved locale files as JSON objects; to get all locale files, use
this.locales
from within your callback.Retrieved fallback locale files as JSON objects; to get all fallback locale files, use
this.fallbackLocales
from within your callback.
getFormatter(ns, sep)
- A generic formatter building method; can be supplied an optional namespace and optional separator, defaulting otherwise todefaultNamespace
anddefaultSeparator
, respectively. The namespace can either be an array of namespace parts or a string with the separator already applied. The namespace (along with separator interpolated between and after) will be prepended to the key supplied by the user when the formatter function is called. Will be automatically invoked (and the result supplied tocallback
) ifcallback
orlanguages
is supplied upon instantiation. The returned formatter function can be called with the following arguments (supplied in order or with a single object with properties of the given name):key
- Lookup key for aIntlMessageFormat
-formatted message in the locales file (will be prepended as per the above by any supplied namespaces and separators). If novalues
orformats
objects are supplied, the literal value from the locales file will be returned without anyIntlMessageFormat
processing. If supplied as an array, the last item will be treated as the key whereas the prior portions will be treated as a namespace override. Likewise if a separator-separated string is supplied.values
- Optional values object (same as accepted byIntlMessageFormat.format()
)formats
- Optional formats object (same as accepted by third argument to theIntlMessageFormat
constructor).fallback
- Optional fallback function or boolean. If a function,fallback
will be called whenever a message cannot be found, and will be supplied a single object argument with properties,message
,langs
,namespace
,separator
,key
,values
, andformats
and whose return value will be used for the return value of the formatter function. Might be used to return a different structure to indicate a need for different directionality (e.g., if an Arabic message is not found, the fallback can be used to return a value which can be used to indicate the English one can be displayed left to right). If an otherwise non-false
value is supplied forfallback
(or the argument is omitted), any message forfallbackLocales
(if any) will be returned instead. If no message has been found and no fallbacks provided, an error will be thrown.
loadLocales(langs, cb, avoidSettingLocales)
- Loads the locale file objects. Will be automatically invoked ifcallback
orlanguages
is supplied upon instantiation. The second callback argument is optional and will be invoked with each locale object as an argument after the locales have loaded (with the locales also available as an array of objects on thelocales
property (and used by formatters) unlessavoidSettingLocales
is supplied).
Properties
langs
- Will automatically be set (as an array of BCP 47 language tags) upon calls toloadLocales
or if thelangs
option is supplied upon instantiation. Utilized in error messages when no message or fallback message can be found.locales
- Will automatically be set to an (empty) array. IfloadLocales
is run (including by the use oflanguages
orcallback
options arguments to the constructor), the retrieved locale JSON objects will be added to this array. This property can also be set as an array of (pre-loaded) locales at instantiation. If supplied after instantiation, these will replace any already set.fallbackLocales
- Will automatically be set to an array. Defaults to an empty array. IffallbackLanguages
is provided, the locale JSON objects loaded byloadLocales
will be added to this array. This property can also be set as an array of (pre-loaded) locales at instantiation. If supplied after instantiation, these will replace any already set. Unless thefallback
argument provided togetFormatter
is set tofalse
, these fallback locales will be checked whenevergetFormatter
does not find a message within thelocales
property.fallbackLanguages
Will automatically be set.
To-dos
- Add utility for auto-setting
document.title
,html.lang
, andhtml.dir
- Some code like this to import Yahoo's locale files (plural rules
and sublanguages) (or if necessary calling
IntlMessageFormat.__addLocaleData()
ourselves). Won't be necessary for browser in future if implemented
const text = (await fetch('path/to/Yahoo/IntlMessageFormat/dist/locale-data/XX.js')).text();
console.log(text);
- Support JSONP (allowing for multi-line template strings or other deviants from non-JSON syntax like single quotes).
- Add logic to recover if file like
en-US.json
is not present buten.json
is (might enhancegetJSON
errBack to capture thrown object with file property indicating the file causing the error).