Answer the question
In order to leave comments, you need to log in
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()
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question