Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

express + i18n-node + handlebars and avoid concurrency issues

View i18n-express-hbs-app.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
// require modules
var express = require('express'),
i18n = require('i18n'),
hbs = require('hbs'),
app = module.exports = express();
 
i18n.configure({
locales: ['en', 'fr'],
cookie: 'locale',
directory: "" + __dirname + "/locales"
});
 
app.configure(function () {
// setup hbs
app.set('views', "" + __dirname + "/views");
app.set('view engine', 'hbs');
app.engine('hbs', hbs.__express);
 
// you'll need cookies
app.use(express.cookieParser());
 
// init i18n module for this loop
app.use(i18n.init);
 
});
 
// register hbs helpers in res.locals' context which provides this.locale
hbs.registerHelper('__', function () {
return i18n.__.apply(this, arguments);
});
hbs.registerHelper('__n', function () {
return i18n.__n.apply(this, arguments);
});
 
// serving homepage
app.get('/', function (req, res) {
res.render('index');
});
 
// startup
app.listen(3000);
View i18n-express-hbs-app.js
1 2 3 4 5
<span id="text">{{{__ "text to test"}}}</span>
<br>
<span id="onecat">{{{__n "%d cat" "%d cats" 1}}}</span>
<br>
<span id="twocats">{{{__n "%d cat" "%d cats" 2}}}</span>

Hi @mashpie
Are there any other examples for using handlebars with i18n-node, this is great for a first "hello world" example but what about real life examples? different grammar, hence different word locations in different locales etc... it would also help to see along with i18n configuration+route logic+view template - the generated json file for static text and working along with dynamic data localized text coming from the db.
This is what developers will end up wanting to do pretty quickly while localizing an app and the example is leaving you to guess all of these trivial parts...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.