i18n.__({ phrase: 'id', locale: 'en' }) ignoring i18n config
I have set my config with following code
i18n.configure({
locales: ['de', 'fr', 'it'],
defaultLocale: 'de'
// [...]
});
Now when compiling a jade template with
//en is used as an example here, it comes as POST parameter
i18n.setLocale(req, 'en');
jade.compileFile('filename.jade', {})({
__: function(translationId)
{
i18n.__.bind(req, arguments);
}
});
it works as expected (since en is not defined in the locales the defaultLocale is used).
After seeing the deprecation note on this method (i18n.js#L221) I tried to replace the call with
jade.compileFile('filename.jade', {})({
__: function(translationId)
{
i18n.__({ phrase: translationId, locale: 'en' });
}
});
but then the config is ignored and i18n tries to load from en.json which it doesn't find an thus generate.
The expected behaviour however is that it does instead check the defined locales and since en is not defined there the defaultLocale is used instead.
No, I'm afraid this is not expected behaviour. The defaultLocale get's forcibly overwritten by your locale parameter i18n.__({ phrase: translationId, locale: 'en' });. This is how passing locale parameters should work. Passing it should just give full control to your own implementation of guessing a users language.
You should try to make use of i18n.init https://github.com/mashpie/i18n-node#i18ninit as this binds i18n's api to locals of requets and response object and initially guesses that requests language.
Unfortunately I don't have a running example for usage with jade... yet. Last time was back in 2011 https://github.com/mashpie/i18n-node/issues/9
Maybe you need to bind jade helper in another way: https://github.com/mashpie/i18n-node#attaching-helpers-for-template-engines