Answer the question
In order to leave comments, you need to log in
What is the best way to organize multilingualism in express?
What is available:
- a middleware in express, which determines the language on each request and writes it to the req._lang variable, after which req is passed further down the chain
- the Translate class with the #setLocale(lang) method (sets the default translation language to lang) and #_(lang, phrase, args) method (translates phrase string with args arguments into lang language (if null - default))
- templating engine
Obvious option
var t = new Translate({..});
var app = express();
app.get('/url', function(req, res){
var nick, name;
...
t.setLocale(req._lang);
res.render('template.html', {
title: t._('Hello, %s', nick),
description: t._('This is a long description, my dear %s, ...', name)
});
});
// template.html
<h1><%= title %></h1>
<div><%= description %></div>
app.get('/url', function(req, res){
var nick, name;
...
res.render('template.html', {
nick: nick,
name: name
});
});
// template.html
<h1><% _('Hello, %s', nick) %></h1>
<div><% _('This is a long description, my dear %s, ...', name) %></div>
var t = new Translate({..});
var app = express();
// мидлвар
app.use(function(req, res, next){
req._ = function(phrase, args){
return t._(req._lang, phrase, args);
}
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question