I try to set defaultLocale as other languages, say defaultLocale: 'de', but it is not working, unless I set the locale specially in controller as res.setLocale('de'), it will use the right locale.
I temporarily solve this by add res.setLocale(sails.config.i18n.defaultLocale); to i18n hook, as below:
routes: {
before: {
'/*': function (req, res, next) {
i18n.init(req, res, function() {
res.locals.i18n = res.i18n = res.__;
res.setLocale(sails.config.i18n.defaultLocale);
next();
});
}
}
},
Any better solution to this?
@lyfeyaj which version of sails are you on?
@mikermcneil, v0.9.11. I checked the dependency of node i18n version: "i18n": "0.4.1"
@mikermcneil Any better solution for this?
Is this issue present in sails@beta?
I can confirm that its not working in sails@beta. The translation file updates is not working either, I had to set that manually set this in the config file.
locales: ['en', 'es', 'fr', 'de'],
defaultLocale: 'en',
directory: './locales',
updateFiles: true
I think the translations are made based on the "Accept-Language" header. I will look into this further later on today
@kokujin thanks! I believe it's working as it should-- if it's not, definitely needs a fix, and even if it is, the docs on the subject could use some work. Appreciate your help.
@kokujin any update? And I update to version sails@beta, this problem is still there
Hi there @lyfeyaj, sorry it took so long. The problem are also in sails@beta as I mentioned above. I hope to have time next week to make a PR and fix the problem.
I think this is working as designed. defaultLocale is being passed properly down to the i18n module. It's just that the module only uses the defaultLocale value if it can't guess the locale from the Accept-Language header. If I set my defaultLocale to de, then request a Sails page with a bogus Accept-Language: xyz header, I see the German translation.
If you want to force the site into a certain default local _regardless of the language header_, and translate it on demand based on, say, a query param, you could put this at the top of your config/routes.js:
'/*': function(req, res, next) {
res.setLocale(req.param('lang') || sails.config.i18n.defaultLocale);
return next();
}
@sgress454 Thanks, that helps.
I'm sorry, I should have explained a little more about what I meant by
buggy:
On Tue, Apr 1, 2014 at 5:03 AM, lyfeyaj [email protected] wrote:
@sgress454 https://github.com/sgress454 Thanks, that helps.
—
Reply to this email directly or view it on GitHubhttps://github.com/balderdashy/sails/issues/1508#issuecomment-39166458
.
@lyfeyaj @kokujin Hey there - was your question answered by sgress454 or do you need more info? If this issue is resolved we'd like to close this up, thanks!
@lyfeyaj , my problem then was item 2, "The templates do not store new/unknown translation strings as it should.". I don't know if this has been resolved in the new release candidates
We'll take a closer look and update or close as needed. If you find the information in the meantime, please do the same~ Thanks!
@kokujin
The templates do not store new/unknown translation strings as it should
Automatically storing new translations is turned off by default in Sails. Turning it on via the config option is the right way to handle your issue.
Agreed, but I posted that this is not the case, setting the save option
does not work
On Jun 12, 2014 2:20 AM, "sgress454" [email protected] wrote:
@kokujin https://github.com/kokujin
The templates do not store new/unknown translation strings as it should
Automatically storing new translations is turned off by default in Sails.
Turning it on via the config option is the right way to handle your issue.—
Reply to this email directly or view it on GitHub
https://github.com/balderdashy/sails/issues/1508#issuecomment-45816786.
Here again!
In my case I am with sails v0.11.2
Seems it is also not loading default local anyway.
I had to solve it with policies again like.
Created: api/policies/lang.js with below codes
module.exports = function(req, res, next) {
res.setLocale('fr');
next();
}
Then updated: config/policies.js
'*': [..., 'lang'],
Thanks
Hi all,
How can i load the default route '/' to load with default locale in it?
ex: '/' loads http://mywebsite/login
how can i make it to be http://mywebsite/en/login
Thanks..
@jeswinjames24 I'm not sure if i18n-node supports url parsing but if you are interested, you can give sails-hook-gengojs a try. You can turn on url parsing in the options under:
header: { detect: {/*...*/} }. More info about gengo.js can be found at https://github.com/gengojs/gengojs.
Note: I am the author of gengo.js ;).
Most helpful comment
I temporarily solve this by add
res.setLocale(sails.config.i18n.defaultLocale);toi18n hook, as below:Any better solution to this?