Closed jonathandion closed 3 years ago
hm, as far as I can see... you've implemented i18n in a way like in it's test:
did you try using app.get
instead of router.get
? I thought router
could refer to an isolated instance, see http://expressjs.com/en/4x/api.html#router:
A router object is an isolated instance of middleware and routes. You can think of it as a “mini-application,” capable only of performing middleware and routing functions. Every Express application has a built-in app router.
or initialize i18n within your router?
Same issue here. I'm using the most simple script:
const i18n = require('i18n');
i18n.configure({
locales: ['en_UK', 'nl_NL', 'de_DE', 'es_ES'],
defaultLocale: 'en_EN',
updateFiles: false,
directory: './locales'
});
i18n.setLocale('nl_NL');
console.log(i18n.getLocale());
This logs en_EN to the console instead of nl_NL. Using Node v8.10.0 and i18n v0.8.3
in your app.js
if you want to set locale globally for your all path and restfull api
i18n.configure({
locales: ['fa'],
directory: path.resolve('./locales')
});
app.use(i18n.init);
app.use((req, res, next) => {
res.setLocale('fa');
next();
});
if you want to set it just in one method, just do this:
router.get('/', async (req, res) => {
res.setLocale('fa');
// your code is here
}
is it clear enough?
should be :)
as written here https://github.com/mashpie/i18n-node#i18nsetlocale
and https://github.com/mashpie/i18n-node/blob/master/test/i18n.setLocale.js + https://github.com/mashpie/i18n-node/blob/master/test/i18n.setLocaleDefaultLanguage.js
and https://github.com/mashpie/i18n-node/blob/master/examples/express4-setLocale/index.js
Hi,
I try to set the locale dynamically using the
setLocale
function but when accessing the updated locale withgetLocale
, I always getting the same output why?I try
i18n.setLocale(req, 'fr');
req.setLocale(req, 'fr');
My code in order
My middleware before the routing:
Thanks.