y18n-browser
The bare-bones internationalization library.
Examples
simple string translation:
var __ = __ console
output:
my awesome string foo
pluralization support:
var __n = __n console
output:
2 fishes foo
JSON Language Files
The JSON language files can be stored anywhere as long as you can include them
when you instantiate a new Y18N.
File names correspond to locales, e.g., en.json
, pirate.json
.
When strings are observed for the first time they will be added to the JSON file corresponding to the current locale.
Usage
It is recommended to create a helper file/module to handle constructing a new Y18N instance like so:
y18n.js
Create an instance of y18n with the config provided, options include:
sources
: an object mapping to existing json source fileswriteLocalUpdates
: should newly observed strings be written to local storage, defaultfalse
.locale
: what locale should be used.fallbackToLanguage
: should fallback to a language-only file (e.g.en.json
) be allowed if a file matching the locale does not exist (e.g.en_US.json
), defaulttrue
.
Methods
import y18n from './my/instance/y18n'
Now you can use your configured instance anywhere.
y18n.__(str, arg, arg, arg)
Print a localized string, %s
will be replaced with arg
s.
y18n.__n(singularString, pluralString, count, arg, arg, arg)
Print a localized string with appropriate pluralization. If %d
is provided
in the string, the count
will replace this placeholder.
y18n.setLocale(str)
Set the current locale being used.
y18n.getLocale()
What locale is currently being used?
y18n.updateLocale(obj)
Update the current locale with the key value pairs in obj
.
Example locale json file
License
ISC