Skip to content

Instantly share code, notes, and snippets.

@llaine
Created August 25, 2014 08:25
Show Gist options
  • Save llaine/575f9ac746fda5136073 to your computer and use it in GitHub Desktop.
Save llaine/575f9ac746fda5136073 to your computer and use it in GitHub Desktop.
Meteor translate module
<template name="helloWorld">
{{ translate 'HELLO' }}
</template>
/**
*
* author : Lainé Louis
*
*
* Translate module.
* Using for i18n.
*
* The module doesn't use navigator.language but Sessions.
*/
/**
*
* Will contain all messages.
*
* @type {*}
*/
var messages = {
'HELLO': {
'en' : 'Hello World ! ',
'fr' : 'Bonjour le monde ! '
}
};
/**
* Set the current locale
* @param locale
*/
setLocale = function(locale){
Session.set('locale', locale);
Deps.flush();
};
/**
* Get the current locale
* @returns {*}
*/
getLocale = function(){
if(Session.get('locale') === undefined){
Session.setDefault('locale', 'en');
}
return Session.get('locale');
};
/**
* This function return a message corresponding to the key passed in param.
* @param key
* @returns {*}
*/
function getMessage(key){
var locale = getLocale();
if(messages !== undefined && messages[key] !== undefined && messages[key][locale] !== undefined){
return messages[key][locale]
}else{
return key;
}
}
/**
* TRANSLATE template for handlebar.
* Translate in the Session locale the key message.
*/
Handlebars.registerHelper('translate', function(key){
return getMessage(key);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment