Skip to content

Commit

Permalink
git st
Browse files Browse the repository at this point in the history
  • Loading branch information
larafale committed Sep 18, 2012
1 parent ed0bfc5 commit 6f00889
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 15 deletions.
84 changes: 80 additions & 4 deletions 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`
26 changes: 15 additions & 11 deletions lib/node-i18n.js
Expand Up @@ -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(/{{.*?}}/, '')
}
Expand Down Expand Up @@ -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)
Expand All @@ -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()
}
Expand All @@ -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])
})
}
Expand All @@ -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.