S
S
Stanislav2019-05-09 20:26:36
Node.js
Stanislav, 2019-05-09 20:26:36

How to more correctly determine the selected language by the user on the site?

Hello.
From time to time I encounter a problem when the language seems to be selected by default RU, but EN is displayed on the site, some kind of failure occurs or something like that.
The language check is very primitive and is determined directly by the URL using the middleware.
If the user goes to the site in the /en/ directory, then all urls are formed with this directory and, of course, the language pack is determined in the middleware.
That's how I did it

module.exports = (request, response, next) => {
    locale = request.originalUrl.replace(/.*\/(en|ru)\/.*/, '$1'), noLang = /\//.test(locale), noLang && (locale = "ru"), (request.CONST = response.locals.CONST = require(`../languages/${locale}`)),  next()
}

I don’t even know why, but sometimes it happens that the wrong language pack is connected.
Maybe do something differently?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Chernyshev, 2019-05-15
@IvanBlacky

As an option, first check on the client what language is used in the browser, for example, using this library , put it in the cookie and check the value of the cookie on the server already. Well, of course, make a language switcher in the web face.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question