From 6f00889a2bce0fefb5aece2bc023cee05aa4117d Mon Sep 17 00:00:00 2001 From: Louis Grellet Date: Tue, 18 Sep 2012 12:05:39 +0200 Subject: [PATCH] git st --- README.md | 84 +++++++++++++++++++++++++++++++++++++++++++++--- lib/node-i18n.js | 26 ++++++++------- 2 files changed, 95 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index d3a069d..ac89266 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,85 @@ node-i18n ========= -i18n for node with deep level objects +i18n for node with expressJS -module developers -================= +``` +var i18n = require('node-i18n')({ + default : 'en' + , enabled : ['en', 'fr'] + , 'dir' : './assets/private/i18n' + , 'helper_translate' : '__' +}) -type 'npm test' to launch vows \ No newline at end of file +app.use(i18n.middleware) + +//call before app.use(app.router) + +``` + +`dir` start on the same level as your express application file +
+ +#### Express route + +let's say you have a route `/products` where you want to implement i18n +Before implementing i18n your route would have look like so : + +``` +app.get('/products', function(req, res){ + res.end() +}) +``` +After + +``` +app.get(i18n.route('products'), function(req, res){ + res.end() +}) +``` +Note that the argument that `i18n.route()` takes is a `RegExp`, and we ommitted the `/` before `products` +
+Now `/products`, `/en/products` and `/fr/products` will match the route +
+As `en` is set to be the default, when making a request to `/en/products` you will be redirected to `/products` + +#### exemple of en.json + +``` +{ "en": { + + "baseline" : "Welcome to my site" + + , "header": { + "menu": { + "h1" : "Hello {{person}} !" + , "h2" : "What's up" + } + } + + , "footer": { + "who": "Who are we ?" + } + +}} +``` + +#### Jade template + +``` +p __('baseline') + +div.header + h1= __('header.menu.h1', 'Batman') + h2= __('header.menu.h2') + +div.footer + a(href="#") __('footer.who') + +``` + + +## Tests + + +`npm test` \ No newline at end of file diff --git a/lib/node-i18n.js b/lib/node-i18n.js index f817ffb..f8de68a 100644 --- a/lib/node-i18n.js +++ b/lib/node-i18n.js @@ -5,19 +5,19 @@ module.exports = function (options) { var options = _.extend({ 'default' : 'en' - , 'dir' : './assets/private/i18n' , 'enabled' : ['en'] + , 'dir' : './assets/private/i18n' + , 'helper_translate' : '__' + , 'helper_path' : '__p' + , 'helper_locale' : '__l' }, options) options.dir = process.cwd() + '/' + options.dir var findLabels = function(word) { var labels = [] - var finder = /({{.*?}})/ - while (match = finder.exec(word)) { - var l = match[1]; - l = l.replace(/{{/, '').replace(/}}/, '') - labels.push(l) + while (match = /({{.*?}})/.exec(word)) { + labels.push(match[1].replace(/{{/, '').replace(/}}/, '')) // remove caught variable word = word.replace(/{{.*?}}/, '') } @@ -51,6 +51,10 @@ module.exports = function (options) { return '/' + locale + path } } + + this.route = function(route) { + return new RegExp("^(\/[a-z]{2})?\/" + (route ? route : "")) + } this.middleware = function(req, res, next){ matches = /^(\/[a-z]{2}\/?)$|^\/([a-z]{2}\/)?(.*)$/.exec(req.path) @@ -62,9 +66,9 @@ module.exports = function (options) { locale = locale || self.options.default // binding view helpers - res.locals.__ = self.translateHelper(locale) - res.locals.__p = self.pathHelper(locale) - res.locals.__l = 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,7 +93,7 @@ module.exports = function (options) { if(!_.isEmpty(variables)) { labels = findLabels(word) _.each(labels, function(l) { - var finder = new RegExp("{{" + l + "}}") + finder = new RegExp("{{" + l + "}}") word = word.replace(finder, variables[l]) }) } @@ -102,5 +106,5 @@ module.exports = function (options) { } - return new i18n() + return new i18n().load() } \ No newline at end of file