i18n-for-browser
Modern translation module for web.
Install
npm i i18n-for-browser# or yarn add i18n-for-browser
CDN
i18n-for-browser
is also available on unpkg.com as UMD, which exposes global object i18n
.
API
Module exposes next API:
;;
Description of this methods you can find in Documentation.
Shirt description:
i18n
Global config. Instanse of Config
.
Usage example
; /** * Set global config. */i18n; console; // Uses global config. /** * Create config fork with some overrides. */const i18nFork = i18n;/** * Bind new config to method. */const __pi = i18nFork; /** * Now you able to use plural intervals. */console;
__()
Translates a single phrase and adds it to locales if unknown.
Usage example
/** * Basic usage *//** * As template string */__`cat`/** * Supports sprintf formatting *//** * Sprintf formatting with template string */__` cats`/** * Sprintf formatting with few arguments *//** * Mustache templates are supported with `mustacheProcessor` *//** * First argument as object with specified locale */
__mf()
Supports the advanced MessageFormat as provided by excellent messageformat module. You should definetly head over to messageformat.github.io for a guide to MessageFormat. i18n-for-browser
takes care of new MessageFormat('en').compile(msg);
with the current msg loaded from it's json files and cache that complied fn in memory. So in short you might use it similar to __()
plus extra object to accomblish MessageFormat's formating.
Usage example
/** * Basic usage, also works as raw `__` method *//** * Basic replacement *//** * Also work with sprintf formatting */
__n()
Plurals translation of a single phrase. Singular and plural forms will get added to locales if unknown. Returns translated parsed and substituted string based on count
parameter.
Usage example
/** * Basic usage *//** * As template string */__` cats`/** * Can work without translation in config *//** * First argument as object with specified locale */
__m()
Returns a map of translations for a given phrase in each language.
Usage example
/** * Basic usage */
Express middleware helper
To provide translations to client from your express app you can use this helper.
;// or const i18nExpressHelper = ;// ...// Before this `i18n` should already initialized.app;// ...
views/layout.ejs <%- initI18nForBrowser() %> <%- body %>