Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added client

  • Loading branch information...
commit 2d77819efdd4608764c3a8b872e4c4882f806955 1 parent f7222e5
@larafale authored
Showing with 130 additions and 39 deletions.
  1. +96 −0 lib/i18n-client.js
  2. +34 −39 lib/node-i18n.js
View
96 lib/i18n-client.js
@@ -0,0 +1,96 @@
+var i18nClient = function(options) {
+
+ var options = _.extend({
+ 'default' : 'en'
+ , 'locale' : 'en'
+ , 'enabled' : ['en', 'fr', 'es']
+ }, options)
+
+ var findLabels = function(word) {
+ var labels = []
+ while (match = /({{.*?}})/.exec(word)) {
+ labels.push(match[1].replace(/{{/, '').replace(/}}/, ''))
+ word = word.replace(/{{.*?}}/, '')
+ }
+ return labels
+ }
+
+ var findHooks = function(word) {
+ var hooks = []
+ while (match = /({{{.*?}}})/.exec(word)) {
+ hooks.push((match[1].replace(/{{{/, '').replace(/}}}/, '')).split('|'))
+ word = word.replace(/{{{.*?}}}/, '')
+ }
+ return hooks
+ }
+
+ var i18n = function(){
+
+ var self = this
+
+ this.options = options
+ this.words = {}
+ this.files = []
+
+ this.load = function(){
+
+ $.getJSON('/i18n.json?locale='+this.options.locale, function(data){
+ _.each(data.results, function(result) {
+ resultLocale = _.keys(result)[0]
+ self.words[resultLocale] = _.extend(self.words[resultLocale] || {}, result[resultLocale])
+ })
+ })
+
+ return this
+ }
+
+
+ this.translate = function(locale, key, variables){
+
+ var locale = locale
+ , variables = variables
+ , keys = key.split(".")
+
+ if(typeof(variables) === 'string') {
+ locale = variables
+ variables = {}
+ }
+
+ var word = this.words[locale]
+
+ //no dictionnary found
+ if(!word && !(word = this.words[self.options['default']])) return key
+
+ //finding key in dictionnary
+ for(var i=0; i<keys.length; i++){
+ if(word[keys[i]]) word = word[keys[i]]
+ else return key
+ }
+
+ if(_.isEmpty(variables)) return word
+
+ //replace hooks
+ _.each(findHooks(word), function(hook) {
+ text = variables[hook[0]] ? variables[hook[0]].replace(new RegExp("\\$" + hook[0]), hook[1]) : ''
+ word = word.replace(new RegExp("{{{"+hook[0]+"\\|.*?}}}"), text)
+ })
+
+ //replace labels
+ _.each(findLabels(word), function(label) { word = word.replace(new RegExp("{{" + label + "}}"), variables[label]) })
+
+ return word
+
+ }
+
+
+ this.__ = function(key, variables, locale) {
+ if(typeof(variables) === 'string') locale = variables
+ return self.translate(local || self.options.locale, key, variables)
+ }
+
+ return this
+
+ }
+
+ return new i18n().load()
+}
View
73 lib/node-i18n.js
@@ -15,7 +15,7 @@ module.exports = function (options) {
options.dir = process.cwd() + '/' + options.dir
var findLabels = function(word) {
- var labels = []
+ var labels = []
while (match = /({{.*?}})/.exec(word)) {
labels.push(match[1].replace(/{{/, '').replace(/}}/, ''))
word = word.replace(/{{.*?}}/, '')
@@ -23,8 +23,8 @@ module.exports = function (options) {
return labels
}
- var findHooks = function(word) {
- var hooks = []
+ var findHooks = function(word) {
+ var hooks = []
while (match = /({{{.*?}}})/.exec(word)) {
hooks.push((match[1].replace(/{{{/, '').replace(/}}}/, '')).split('|'))
word = word.replace(/{{{.*?}}}/, '')
@@ -42,8 +42,8 @@ module.exports = function (options) {
this.load = function(){
_.each(this.files, function(file){
- fileContents = require(self.options.dir + '/' + file)
- fileLocale = _.keys(fileContents)[0]
+ fileContents = require(self.options.dir + '/' + file)
+ fileLocale = _.keys(fileContents)[0]
self.words[fileLocale] = _.extend(self.words[fileLocale] || {}, fileContents[fileLocale])
})
return this
@@ -57,10 +57,10 @@ module.exports = function (options) {
this.pathHelper = function(locale){
return function(path, forcedLocale) {
- var l = forcedLocale || locale
+ var local_ = forcedLocale || locale
if(/^\/[a-z]{2}$|^\/[a-z]{2}\/(.*)$/.test(path)) path = path.substring(3)
- if(l === self.options.default) return path
- return '/' + l + path
+ if(local_ === self.options.default) return path
+ return '/' + local_ + path
}
}
@@ -78,9 +78,9 @@ module.exports = function (options) {
locale = locale || self.options.default
// binding view helpers
- res.locals[self.options['helper_translate']] = self.translateHelper(locale)
- res.locals[self.options['helper_path']] = self.pathHelper(locale)
- res.locals[self.options['helper_locale']] = locale
+ res.locals[self.options['helper_translate']]= self.translateHelper(locale)
+ res.locals[self.options['helper_path']] = self.pathHelper(locale)
+ res.locals[self.options['helper_locale']] = locale
next()
}
@@ -89,49 +89,44 @@ module.exports = function (options) {
if(locale && !(_.indexOf(self.options.enabled, locale) >= 0)) locale = self.options.default
// binding view helpers
- res.locals[self.options['helper_translate']] = self.translateHelper(locale)
- res.locals[self.options['helper_locale']] = locale
+ res.locals[self.options['helper_translate']]= self.translateHelper(locale)
+ res.locals[self.options['helper_locale']] = locale
next()
}
this.translate = function(locale, key, variables){
- var locale = locale
- var variables = variables
+ var locale = locale
+ , variables = variables
+ , keys = key.split(".")
+
if(typeof(variables) === 'string') {
- locale = variables
+ locale = variables
variables = {}
}
- word = this.words[locale]
+ var word = this.words[locale]
- if(!word){
- if(!this.words[self.options['default']]) return key //no definition found for any locale
- else word = this.words[self.options['default']]
- }
+ if(!word && !(word = this.words[self.options['default']])) return key
- keys = key.split(".")
+ //finding key in dictionnary
for(var i=0; i<keys.length; i++){
- tmp = word[keys[i]]
- if(tmp) word = tmp
- else return key //no definition found for given key
+ if(word[keys[i]]) word = word[keys[i]]
+ else return key
}
- if(!_.isEmpty(variables)) {
- //replace hooks
- _.each(findHooks(word), function(hook) {
- text = variables[hook[0]] ? variables[hook[0]].replace(new RegExp("\\$" + hook[0]), hook[1]) : ''
- word = word.replace(new RegExp("{{{"+hook[0]+"\\|.*?}}}"), text)
- })
-
- //replace labels
- _.each(findLabels(word), function(label) {
- finder = new RegExp("{{" + label + "}}")
- word = word.replace(finder, variables[label])
- })
- }
-
+ if(_.isEmpty(variables)) return word
+
+ //replace hooks
+ _.each(findHooks(word), function(hook) {
+ text = variables[hook[0]] ? variables[hook[0]].replace(new RegExp("\\$" + hook[0]), hook[1]) : ''
+ word = word.replace(new RegExp("{{{"+hook[0]+"\\|.*?}}}"), text)
+ })
+
+ //replace labels
+ _.each(findLabels(word), function(label) { word = word.replace(new RegExp("{{" + label + "}}"), variables[label]) })
+
return word
}
Please sign in to comment.
Something went wrong with that request. Please try again.