Lightweight, reactive, isomorphic, extendable i18n package for meteor using MongoDB
server
i18n.addLanguage('en', 'English')
i18n.addLanguage('de', 'Deutsch')
i18n.addLanguage('it', 'Italiano')
i18n.add({
welcome: {
salutation: {
en: 'Hello Dolly',
de: 'Hallo Dolly'
}
}
})
i18n.add({
welcome: {
salutation: 'Ciao Dolly'
}
}, 'it')
i18n.setLanguage('en')
i18n.getLanguage()
// -> en
i18n.setLanguage('it')
i18n.get('welcome.salutation')
// -> 'Ciao Dolly'
i18n.get('welcome.salutation', 'de')
// -> 'Hallo Dolly'
client
By default the client can't add translations. You can simply set up allow/deny rules on the database.
i18n.addLanguage('de', 'Deutsch')
i18n.addLanguage('en', 'English')
i18n.loadAll(function () {
i18n.setLanguage('it')
i18n.get('welcome.salutation')
// -> 'Ciao Dolly'
i18n.get('welcome.salutation', 'de')
})
<p>{{ i18nget "welcome.salutation" }}</p>
The name of the default-helper is kind of clunky. So I'd add the following helper:
Template.registerHelper('_', i18n.get)
halunka:i18n
adds the contents of files with the .i18n.json
extension to the database. The filenames cannot contain dots.
Example:
{
"salutations": {
dolly: {
"en": "Hello Dolly",
"de": "Hallo Dolly",
"it": "Ciao Dolly"
},
sam: {
"en": "Hello Sam",
"de": "Hallo Sam",
"it": "Ciao Sam"
}
}
}
Adds a translation to the DB. This function is only availible on the server, mainly because you can't upsert
from the client. Also it seems like an edge-case. If you want to use it on the client you can do something like this:
if(Meteor.isServer) {
Meteor.methods({
'i18n:add': i18n.add
})
} else {
i18n.add = function (data, lang) {
Meteor.call('i18n:add', data, lang)
}
}
Returns a translation for either the passed language or the current one.
Returns an object with all translations with a key.
Adds a new language.
Sets the current language.
Returns the current language. Falls back to the default language and the first language added
Returns an array of all added languages in the following format:
{
key: key,
name: val
}
Loads translations for a specific language on the client and the calls the callback
.
Loads all translation on the client and calls the callback function.
The Mongo DB.
An interface to i18n.get
.
An interface to i18n.listLang
.
- document string templating