Skip to content
Browse files

git st

  • Loading branch information...
1 parent ed0bfc5 commit 6f00889a2bce0fefb5aece2bc023cee05aa4117d @larafale committed Sep 18, 2012
Showing with 95 additions and 15 deletions.
  1. +80 −4 README.md
  2. +15 −11 lib/node-i18n.js
View
84 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
+app.use(i18n.middleware)
+
+//call before app.use(app.router)
+
+```
+
+`dir` start on the same level as your express application file
+<br/>
+
+#### 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`
+<br/>
+Now `/products`, `/en/products` and `/fr/products` will match the route
+<br/>
+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`
View
26 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()
}

0 comments on commit 6f00889

Please sign in to comment.
Something went wrong with that request. Please try again.