Reactive i18n and l10n isomorphic service for Meteor
File based, fast, lightweight (325 lines with comments) and reactive internationalization isomorphic driver for Meteor with support of placeholders.
Demo
Install:
meteor add ostrio:i18n
Object-based structure
i18nConfig = {
settings: {
defaultLocale: "en",
ru: {
code: "ru",
isoCode: "ru-RU",
name: "Русский"
},
en: {
code: "en",
isoCode: "en-US",
name: "English"
}
},
ru:{
property: "value",
property2: {
nestedProp: "value"
}
},
en:{
property: "value",
property2: {
nestedProp: "value"
}
}
...
};
this.i18n = new I18N({driver: 'Object', i18n: i18nConfig});
Mongo-based Files and Folders structure
private/
└─ i18n/ //--> Driver's dir
|
├── en/ //--> Localization folder with name of country two-letter code
| ├── file.json
| └── subFolder/
| └── anotherFile.json
|
├── de/ //--> Localization folder with name of country two-letter code
| ├── file.json
| └── subFolder/
| └── anotherFile.json
|
└── i18n.json //--> Config file
Initialization
this.i18n = new I18N(config);
Configuration object:
config.driver
{String: Mongo|Object} - Driver type. Use Mongo
for file-based and Object
object-based
config.i18n
{Object} - Internalization object
config.path
{String} - Path to i18n
folder, default: /assets/app/i18n
(which points to: private/i18n
in dev environment)
config.returnKey
{Boolean} - Return key if l10n value not found, default: true
config.collectionName
{String} - i18n Collection name, default: internalization
config.helperName
{String} - Template helper name, default: i18n
config.helperSettingsName
{String} - Settings helper name, default: i18nSettings
config.allowPublishAll
{Boolean} - Allow publish full i18n set to client, default: true
Isomorphic usage
get([locale,] key, [replacements...])
locale
{String} - [Optional] Two-letter locale string, used to force locale, if not set current locale is used
key
{String} - l10n key like: folder.file.object.key
replacements..
{String|[String]|Object} - [Optional] Replacements for placeholders in l10n string
i18n.get('file.obj.key');
i18n.get(locale, param);
i18n.get('en', 'file.obj.key');
i18n.get(param, replacements);
i18n.get('file.obj.key', 'User Name');
i18n.get(locale, param, replacements);
i18n.get('en', 'file.obj.key', 'User Name');
setLocale(locale)
locale
{String} - Two-letter locale code
i18n.setLocale(locale);
Get current localization at any environment
i18n.currentLocale.get();
Get current default locale
i18n.defaultLocale;
Get configuration object
i18n.langugeSet();
Get specific key from configuration object
key
{String} - One of the keys: current
, all
, other
, locales
, currentISO
, currentName
, currentPath
i18n.getSetting('current');
Client specific usage
Client's browser locale
i18n.userLocale;
subscribeToAll(callback)
callback
{Function} - Callback function triggered right after subscription is ready
Send full i18n and l10n set to client, might be used within iron-router
and fastrender
packages, example:
i18n = new I18N();
Router.route('index', {
path: '/',
fastRender: true,
waitOn: function() {
return i18n.subscribeToAll();
}
});
Template helpers
i18n
- accepts locale
, key
and replacements
:
You may change name of the helper via config object: config.helperName
<p>{{i18n 'sample.hello'}}</p>
<p>{{{i18n 'sample.html'}}}</p>
<p>{{i18n 'sample.fullName'}}</p>
<p>{{i18n 'sample.fullName' 'Michael' 'A.' 'Macht'}}</p>
<p>{{i18n 'en' 'sample.fullName' 'Michael' 'A.' 'Macht'}}</p>
<p>{{i18n 'de' 'sample.fullName' first='Michael' middle='A.' last='Macht'}}</p>
<p>{{i18n 'sample.fullName' first='Michael' middle='A.' last='Macht'}}</p>
<p>{{i18n 'sample.fullName' first='Michael' middle='A.' third='Macht'}}</p>
i18nSettings
- accepts configuration object key, one of current
, all
, other
, locales
You may change name of the helper via config object: config.helperSettingsName
{{#each i18nSettings 'all'}}
...
{{/each}}
Template language switcher example
<template name="langSwitch">
{{#each i18nSettings 'all'}}
{{#if compare code '==' currentLocale}}
<span title="Current language">{{name}}</span>
{{else}}
<a href="#" data-code="{{code}}" class="switch-language">{{name}}</a>
{{/if}}
{{/each}}
</template>
Template.langSwitch.helpers({
currentLocale: function(){
return i18n.currentLocale.get()
}
});
Template.langSwitch.events({
'click .switch-language': function(e, template) {
e.preventDefault();
i18n.setLocale(e.currentTarget.dataset.code);
return false;
}
});
Template helpers compare
, ==
, Session
and many more comes from: ostrio:templatehelpers package