Skip to content

Commit

Permalink
added client
Browse files Browse the repository at this point in the history
  • Loading branch information
larafale committed Oct 19, 2012
1 parent f7222e5 commit 2d77819
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 39 deletions.
96 changes: 96 additions & 0 deletions 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()
}
73 changes: 34 additions & 39 deletions lib/node-i18n.js
Expand Up @@ -15,16 +15,16 @@ 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(/{{.*?}}/, '')
}
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(/{{{.*?}}}/, '')
Expand All @@ -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
Expand All @@ -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
}
}

Expand All @@ -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()
}

Expand All @@ -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

}
Expand Down

0 comments on commit 2d77819

Please sign in to comment.