Last active
September 26, 2018 16:31
-
-
Save mashpie/5247373 to your computer and use it in GitHub Desktop.
express + i18n-node + mustache (via consolidate.js) and avoid concurrency issues
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>mustache-i18n-test</title> | |
<body> | |
{{#__}}Hello{{/__}} {{name}}<br> | |
{{result}} | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Where is this
arguments
variable coming from?https://gist.github.com/mashpie/5247373#file-i18n-express-mustache-app-js-L32