Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
express + i18n-node + mustache (via consolidate.js) and avoid concurrency issues
// require modules
var express = require('express'),
i18n = require('../../i18n'),
url = require('url'),
cons = require('consolidate'),
app = module.exports = express();
// minimal config
i18n.configure({
locales: ['en', 'de'],
cookie: 'yourcookiename',
directory: __dirname + '/locales'
});
app.configure(function () {
// setup mustache to parse .html files
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.engine('html', cons.mustache);
// you'll need cookies
app.use(express.cookieParser());
// init i18n module for this loop
app.use(i18n.init);
// register helper as a locals function wrapped as mustache expects
app.use(function (req, res, next) {
// mustache helper
res.locals.__ = function () {
return function (text, render) {
return i18n.__.apply(req, arguments);
};
};
next();
});
});
// serving homepage
app.get('/', function (req, res) {
res.render('index', {
'name': 'Marcus',
'result': res.__n('Result: %d cat', 'Result: %d cats', 3)
});
});
// startup
app.listen(3000);
<!DOCTYPE html>
<html>
<head>
<title>mustache-i18n-test</title>
<body>
{{#__}}Hello{{/__}} {{name}}<br>
{{result}}
</body>
</html>
@justin-john

This comment has been minimized.

Show comment Hide comment
@justin-john

justin-john May 2, 2013

I am getting an error "TypeError: Object #<ServerResponse> has no method '__n'" on 'result': res.__n('Result: %d cat', 'Result: %d cats', 3). How to correct this error?

I am getting an error "TypeError: Object #<ServerResponse> has no method '__n'" on 'result': res.__n('Result: %d cat', 'Result: %d cats', 3). How to correct this error?

@mashpie

This comment has been minimized.

Show comment Hide comment
@mashpie

mashpie Jun 2, 2013

Hey Justin,

sry for my late response. I need to setup notifications correctly. Could you checkout i18n from git and run $ mocha --reporter spec examples/express-mustache/test.jsfrom inside it's own directory? Can you tell me about your versions of mustache, express, i18n and node?

Owner

mashpie commented Jun 2, 2013

Hey Justin,

sry for my late response. I need to setup notifications correctly. Could you checkout i18n from git and run $ mocha --reporter spec examples/express-mustache/test.jsfrom inside it's own directory? Can you tell me about your versions of mustache, express, i18n and node?

@justin-john

This comment has been minimized.

Show comment Hide comment
@justin-john

justin-john Jul 1, 2013

Hi @mashpie,

Sorry for responding to your comment so late. The above error is corrected by changing __n method to __ method. Please mention @myname in your comment, so that I get notification about your comment.

I am using hogan v0.0.1, express v3.0.5, i18n v0.4.0 and node v0.8.

Hi @mashpie,

Sorry for responding to your comment so late. The above error is corrected by changing __n method to __ method. Please mention @myname in your comment, so that I get notification about your comment.

I am using hogan v0.0.1, express v3.0.5, i18n v0.4.0 and node v0.8.

@mashpie

This comment has been minimized.

Show comment Hide comment
@mashpie

mashpie Jul 6, 2013

Hey @justin-john,

didn't get notified either... anyway. Any errors in output of make examples and/or make test ?

Owner

mashpie commented Jul 6, 2013

Hey @justin-john,

didn't get notified either... anyway. Any errors in output of make examples and/or make test ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment