2

Im trying to set up i18n-node and Expressjs. My conf is this:

// i18n Configuration =========================================================

var i18nconf = {
   locales        : ["en", "es"],
   cookie         : "locale",
   defaultLocale : "en",
   directory      : path.join(__dirname, "lang"),
   indent : "  "
};

i18n.configure(i18nconf);
app.use(i18n.init);

There are 2 locales on lang/ folder en.json and es.json, I can switch between them no problem but express always load es.json as default, somehow is ignoring defaultLocale: 'en' in the conf.

any ideas?

Thanks in advance!

mdv
  • 925
  • 3
  • 14
  • 28

5 Answers5

2

This library is operating a bit funny to me. I added this middleware to my express server configuration and i18n started to behave as it should.

app.use(function (req, res, next) {
    var locale = 'en'
    req.setLocale(locale)
    res.locals.language = locale
    next()
})
mattyfew
  • 163
  • 3
  • 17
2

Check your header: accept-language.

According to the source code, defaultLocale will be used only when the following step goes through:

  1. queryParameter miss match
  2. cookie miss match
  3. headers['accept-language'] miss match
Aliaksandr Sushkevich
  • 11,550
  • 7
  • 37
  • 44
Ike Chang
  • 74
  • 1
  • 5
0

This module looks in a lot of places for a locale setting. Have you carefully checked and double-checked none of those places are indicating "es" currently? Perhaps a stale cookie from a previous test you did? Try clearing your cookies.

Peter Lyons
  • 142,938
  • 30
  • 279
  • 274
0

Here is a solution that works for me. I wanted to site to be in Chinese by default. I also have 2 language change buttons in the navbar (flag images) that go to /en & /zh routes, which set the cookie and redirect the user back to the page they came from.

Tested it with incognito window and by clearing the cookie and refreshing the site. First load in ZH and language change works by adding/changing the cookie value.

I also had to init i18n BEFORE using req.setLocale() in my middleware.

const express = require('express');
const hbs = require('express-hbs');
const i18n = require('i18n');
const app = express();
const cookieParser = require('cookie-parser');

const indexRoutes = require('./routes/indexRoutes');

app.engine(
  'hbs',
  hbs.express4({
    partialsDir: __dirname + '/views/partials/',
    helpers: {
      __: function () {
        return i18n.__.apply(this, arguments);
      },
      __n: function () {
        return i18n.__n.apply(this, arguments);
      },
    },
  })
);

app.set('view engine', 'hbs');

app.use(express.json());
app.use(cookieParser());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(__dirname + '/public'));

i18n.configure({
  locales: ['zh', 'en'],
  defaultLocale: 'zh',
  cookie: 'locale',
  directory: __dirname + '/locales',
  directoryPermissions: '755',
  autoReload: true,
  updateFiles: true,
  objectNotation: true,
  api: {
    __: '__', //now req.__ becomes req.__
    __n: '__n', //and req.__n can be called as req.__n
  },
});
app.use(i18n.init);

app.use((req, res, next) => {
  if (req.cookies.locale === undefined) {
    res.cookie('locale', 'zh', { maxAge: 900000, httpOnly: true });
    req.setLocale('zh');
  }

  next();
});

app.get('/zh', (req, res) => {
  res.cookie('locale', 'zh', { maxAge: 900000, httpOnly: true });
  res.redirect('back');
});

app.get('/en', (req, res) => {
  res.cookie('locale', 'en', { maxAge: 900000, httpOnly: true });
  res.redirect('back');
});

app.use('/', indexRoutes);

app.listen(process.env.PORT || 3000, () =>
  console.log(`Server up on ${process.env.PORT || 3000}`)
);
0

I's a very old question, but the replacement with defaults only happens when the 'retryInDefaultLocale' property is set.

var i18nconf = {
   locales        : ["en", "es"],
   cookie         : "locale",
   defaultLocale : "en",
   retryInDefaultLocale: true,
   directory      : path.join(__dirname, "lang"),
   indent : "  "
};