Name: i18n-dialect
Owner: 2GIS
Description: null
Created: 2017-08-03 13:58:25.0
Updated: 2018-05-15 03:39:37.0
Pushed: 2018-05-15 03:39:46.0
Homepage: null
Size: 88
Language: TypeScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
dialect
is a library to support runtime i18n functions.
dialect
exports a controller class called TranslationController
, which should be instantiated in your application. Controller's constructor has following configurational parameters:
require
when running on server side, or asynchronous file load with fetch
or other asynchronous request library.%N
substitution is failed; may be undefined.The TranslationController
object also has a setLocale
method to update internal dictionary - once this is done, the changes are applied to all subsequent calls of translation functions. Notice that setLocale
method is asynchronous, so updating your user interface should not be done right after it's call: you should wait until everything is loaded and parsed.
dialect
exports four simple translation functions:
_tGen
for simple translations_ptGen
for contextual translations_ntGen
for plural translations_nptGen
for contextual plural translationsThese functions are function generators (higher order functions), and before using them in your project you should parameterize them with TranslationController
instance, like this:
rt { TranslationController, _tGen, _ptGen, _ntGen, _nptGen } from 'i18n-dialect';
controller = new TranslationController(...);
rt const _t = _tGen(controller);
rt const _pt = _ptGen(controller);
rt const _nt = _ntGen(controller);
rt const _npt = _nptGen(controller);
In some other file:
rt { _t, _pt, _nt, _npt } from '...';
ow you can use _t, _pt, etc.
Make sure that names of your created functions are strictly equal to _t
, _nt
, _pt
and _npt
, and you use these functions with these names only in your code; otherwise your translations will not be parsed successfully with i18n-stex
parser.
dialect
also exports a single class called TranslationProvider
, which simplifies interaction with library for a bit. You should instantiate it once and then just pass it into your other files and components. Example:
rt { TranslationController, TranslationProvider } from 'i18n-dialect';
controller = new TranslationController(...);
rt const i18n = new TranslationProvider(controller);
In some other file:
rt { i18n } from '...';
ow you can use i18n._t, i18n._pt, etc.
i18n-dialect uses github-flow to accept & merge fixes and improvements. Basic process is:
npm run test
and make sure nothing is brokennpm run build
to build pure-js distribution files.Pull requests with failing tests will not be accepted. Also, if you add or modify packages to package.json
, make sure you use yarn
and update yarn.lock
.