S
S
s_katala2018-01-29 21:09:37
Node.js
s_katala, 2018-01-29 21:09:37

What does req.setLocale is not a function error mean?

I'm trying to make multilanguage on i18n
I get an error if I switch to domains.dev/en or ru
error

TypeError: req.setLocale is not a function
    at init (B:/OSPanel/domains/domains.dev/frontend/server/locales/services/i18n_urls.js:24:13)

in app.js (taken from here Github )
services.i18nUrls.configure(app, i18n, {
  locales: ['ru', 'en'],
  defaultLocale: 'ru',
  cookie: process.env.npm_package_name + '_i18n_cookie',
  indent: ' '
});

services
module.exports = {
  configure: function(app, i18n, config) {
    app.locals.i18n = config;
    i18n.configure(config);
  },
  init: function(req, res, next) {
    var rxLocale = /^\/(\w\w)/i;
    if (rxLocale.test(req.url)){
      var locale = rxLocale.exec(req.url)[1];
      if (req.app.locals.i18n.locales.indexOf(locale) >= 0)
        req.setLocale(locale);
    }
    //else // no need to set the already default
    next();
  },
  url: function(app, url) {
    var locales = app.locals.i18n.locales;
    var urls = [];
    for (var i = 0; i < locales.length; i++)
      urls[i] = '/' + locales[i] + url;
    urls[i] = url;
    return urls;
  }
};

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
levchak0910, 2018-01-29
@s_katala

Look towards i18n-2 . It might be more convenient for you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question