question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[req|res].setLocale() doesn't change template locale

See original GitHub issue

app.use(i18n.init) sets res.locals.locale so that the template helpers can get the current language. However, calling res.setLocale() doesn’t update this property, so the final page language doesn’t get changed.

// First Request:
app.use(i18n.init); // locale=en, res.locals.locale=en
app.use(function(req, res, next) {
  res.setLocale('ja'); // locale=ja, res.locals.locale=en
  res.render('some-template'); // template i18n helpers use locals.locale, 
                               // so page is still in english
});

// Second Request:
app.use(i18n.init); // locale = ja, res.locals.locale=ja
// ...

I’m not sure what the right fix is here (or even if you consider this bug), but I thought I’d open this up to start a discussion.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
mauron85commented, Jul 31, 2014

I had same/similar issue. I solved it by manually setting res.locals.language to same locale as in req.setLocale fnc.

app.use(function (req, res, next) {
    var locale = 'en';
    req.setLocale(locale);
    res.locals.language = locale;
});

Hope it helps.

1reaction
FredKSchottcommented, Jul 24, 2014

Yea sure, it’s small enough to include here:

var http = require('http'),
    express = require('express'),
    i18n = require('i18n');

i18n.configure({
    locales: ['ja', 'en'],
    cookie: 'lang',
    directory: 'locales/'
});

var app = express();

app.use(i18n.init);

app.use(function(req, res, next) {
    res.setLocale('ja');
    next();
});

app.use(function(req, res, next) {
    console.log(res.getLocale()); // ja
    console.log(res.locale); // en
    console.log(req.locale); // ja
    console.log(res.locals); // { locale: en, /* ... */ }
    next();
});

var server = http.createServer(app);
server.listen(3000);

This is slightly different behavior than my application (not sure where my undefineds are coming from) but it looks like that same hbs template helper problem would be occurring here as well

Read more comments on GitHub >

github_iconTop Results From Across the Web

setlocale() doesn't read from bindtextdomain() dir C++
The setlocale() function checks the environment, when you call it with an empty string as the second argument. That is then called the...
Read more >
Cannot set LC_CTYPE to default locale: No such file or directory
You have two ways to remedy this: either you change, what is requested or you add the requested locale to the remote system...
Read more >
perllocale - Perl locale handling (internationalization and ...
If setlocale() fails for some reason (for example, an attempt to set to a locale unknown to the system), the locale for the...
Read more >
Documentation: 15: 24.1. Locale Support - PostgreSQL
You can use different settings for different databases, but once a database is created, you cannot change them for that database anymore. LC_COLLATE...
Read more >
How to change system locale on RHEL7?
Configuring locales that are set at login requires some knowledge of different configuration files. What changes you will need to make ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found