- MaskJS Localization Util
- Localization Function
Resolve language from the current request(middleware)
Resolve language from navigator.language
or location.query ('/?language=en')
If the language is not supported, the default one is taken.
Atma-Formatter is used to format/interpolate strings.
Refer to the atma-formatter
.
~[L: ID [,...expressions]]
~[L: (expression) [,...expressions]]
-
Simple:
~[L:fooId]
-
Formatting:
Example:
header > '~[L:welcomeId, name]' // same as header > '~[L:"welcomeId", name]'
$L.extend('en', { welcome: 'Hello {0}!' }); mask.render(template, { name: 'Baz' });
-
get i18n ID from model: Example:
var menu = [ 'todo', 'task' ] ul { for (item of menu) { li > '~[L:("m_" + item)]' } }
$L.extend('en', { m_todo: 'My Todos', m_task: 'My Tasks' }); mask.render(template);
Browser @see examples
$L('titleHello');
Node
connect
.use($L.middleware({
support: [ 'en', 'de', 'ru' ],
path: '/public/localization/%%.json'
});
// Aftewards each `req` has `$L` function.
// Or use direct
$L.fromReq(req)('id');
Load this library with IncludeJS - after defining the list of supported languages and the path to translations, it will load also supported translations
include
.embed('/atma/localization.js?path=/public/i18n/%%.json&langs=de,it,fr')
.done(function(){
// appropriate translationis is loaded and ready to use
$L('welcome', 'Baz')
});
Use the middleware
function so that not all translations are loaded at once, but only with the first incomming request.
(c) MIT, Atma.js Project