I'm using i18next for the internationalization. For a default language, I have set up to detect the language from the URL query or from the accept-language header. (there are two languages, "?lng=en" and "?lng=zh-hant") Now I'm wondering how to append either of the two language queries to the default URL http://example.com/ so that it becomes http://example.com/?lng=en, after detecting the language from the accept-language header. I want the language query present in the URL always, and I want it to be set as the preferred language from accept-language header.
I've tried to parse the accept-language and then redirect using app.use() in Express, but I was unsuccessful. I couldn't find something relevant to my situation and I'm quite new to Javascript.
In short, I want to append "lng" variable from loadPath() to the URL as a query.
const i18next = require('i18next');
const Backend = require('i18next-fs-backend');
const middleware = require('i18next-http-middleware');
i18next.use(Backend)
.use(middleware.LanguageDetector)
.init({
detection: detection_options,
fallbackLng: 'en',
backend: {
loadPath(lng, ns) {
if (lng === 'zh' || lng === 'zh-HK' || lng === 'zh-TW') {
return path.join(__dirname, 'locales/zh-hant.json');
} else if (lng === 'en-US') {
return path.join(__dirname, 'locales/en.json');
}
return path.join(__dirname, 'locales/{{lng}}.json');
}
}
})
app.use(middleware.handle(i18next));