I am using following packages for multi-languages solutions with my Email sending system.
Basically, I followed the instruction to implement from here, and the language detection is worked. Now my question is how to detect the language user is using? My idea is to detect language from the user’s browser header and once the language is English, the content of Email should be changed into English.
I registered a helper function via handlebars as follows and create a route (/) to detect language in the coming request from client side. However, I have no idea does it worked or can someone kindly provide me a better solution? Thanks a lot!
handlebars.registerHelper("I18n", function (i18n_key) {
if (app.locals.language !== i18next.language) {
i18next.changeLanguage(app.locals.language);
}
const result = i18next.t(i18n_key);
return new handlebars.SafeString(result);
});
app.use("/", (req, res, next) => {
// const lng = req.headers["accept-language"];
// const lang = req.language;
// const lngs = req.languages;
// console.log(lng);
// console.log(lang);
// console.log(lngs);
console.dir(res.app.locals.language);
console.dir(req.language);
res.app.locals.language = req.language;
console.dir(res.app.locals.language);
console.log(req.i18n.t("hello"));
res.end(req.i18n.t("coworkerSignUp.title"));
next();
});
i18next-http-middleware provides language detection: https://github.com/i18next/i18next-http-middleware#language-detection
There's also a blog post here explaining how this could be implemented: https://locize.com/blog/how-does-server-side-internationalization-look-like/#email
with this code example: https://github.com/i18next/i18next-fs-backend/blob/master/example/fastify/app.js#L14-L19