Created
August 25, 2014 08:25
-
-
Save llaine/575f9ac746fda5136073 to your computer and use it in GitHub Desktop.
Meteor translate module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template name="helloWorld"> | |
{{ translate 'HELLO' }} | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* 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