MOCKSTACKS
EN
Questions And Answers

More Tutorials









NodeJS Localization

using i18n module to maintains localization in node js app


Lightweight simple translation module with dynamic json storage. Supports plain vanilla node.js apps and should work with any framework (like express, restify and probably more) that exposes an app.use() method passing in res and req objects. Uses common __('...') syntax in app and templates. Stores language files in json files compatible to webtranslateit json format. Adds new strings on-the-fly when first used in your app. No extra parsing needed.

express + i18n-node + cookieParser and avoid concurrency issues

// usual requirements
var express = require('express'),
 i18n = require('i18n'),
 app = module.exports = express();
i18n.configure({
 // setup some locales - other locales default to en silently
 locales: ['en', 'ru', 'de'],
 // sets a custom cookie name to parse locale settings from
 cookie: 'yourcookiename',
 // where to store json files - defaults to './locales'
 directory: __dirname + '/locales'
});
app.configure(function () {
 // you will need to use cookieParser to expose cookies to req.cookies
 app.use(express.cookieParser());
 // i18n init parses req for language headers, cookies, etc.
 app.use(i18n.init);
});
// serving homepage
app.get('/', function (req, res) {
 res.send(res.__('Hello World'));
});
// starting server
if (!module.parent) {
 app.listen(3000);
}


Conclusion

In this page (written and validated by ) you learned about NodeJS Localization . What's Next? If you are interested in completing NodeJS tutorial, your next topic will be learning about: NodeJS Deploying application without downtime.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.