Browse files

path and locale helpers

  • Loading branch information...
1 parent 64b4c16 commit df90ae4d236e061413ea8f3c379a32b02f5ce539 Guillaume Denis committed Sep 18, 2012
Showing with 20 additions and 3 deletions.
  1. +19 −2 lib/node-i18n.js
  2. +1 −1 package.json
View
21 lib/node-i18n.js
@@ -38,20 +38,37 @@ module.exports = function (options) {
return this
}
- this.helper = function(locale){
+ this.translateHelper = function(locale){
+ console.log(locale)
return function(key, variables, forcedLocale) {
+ console.log(locale)
return self.translate(forcedLocale || locale, key, variables)
}
}
+ this.pathHelper = function(locale){
+ console.log(locale)
+ return function(path, forcedLocale) {
+ locale = forcedLocale || locale
+ if(locale === self.options.defaults) return path
+ return '/' + locale + path
+ }
+ }
+
this.middleware = function(req, res, next){
matches = /^(\/[a-z]{2}\/?)$|^\/([a-z]{2}\/)?(.*)$/.exec(req.path)
locale = matches[1] || matches[2] ? matches[2].replace('/', '') : null
// remove incorrect explicit locale
if((locale && !(_.indexOf(self.options.enabled, locale) >= 0)) || locale === self.options.default) return res.redirect(matches[3] || '/')
- res.locals.__ = self.helper(locale || self.options.default)
+ locale = locale || self.options.default
+
+ // binding view helpers
+ res.locals.__ = self.translateHelper(locale)
+ res.locals.__p = self.pathHelper(locale)
+ res.locals.__l = locale
+
next()
}
View
2 package.json
@@ -3,7 +3,7 @@
"description": "i18n for node",
"main": "./index",
"author": "Larafale & silently",
- "version": "0.0.6",
+ "version": "0.0.7",
"repository": {
"type": "git",
"url": "http://github.com/larafale/node-i18n.git"

0 comments on commit df90ae4

Please sign in to comment.