i18n localization
See original GitHub issueHi! First of all I have to say that I discovered AH a few days ago and I am impressed. Thanks for this piece of code!
I am testing it and found an issue that maybe it is a bug, maybe it is something I am doing wrong…
I am using an initializer to create the static method (lookup) to localize client requests as explained in documentation, but even when I check that the returned value is the one I need (I use locale module to parse accept-language header, and, as it sometimes returns the system default locale, I use underscore to discard that if it is not in the “provided languages”), AH always use the default locale set.
At the end I opt to “force” api.i18n to the desired locale using the setLocale function provided. That way, it works fine. A bug?
Thanks in advance,
PD: I pasted below my initializer code… maybe it is useful for somebody… Miguel
'use strict';
var locale = require("locale");
var _ = require("underscore");
module.exports = {
initialize: function(api, next){
api.customLocalization = {
lookup: function(connection){
var clocale = 'en';
if(connection.type === 'web'){
var supported = new locale.Locales(api.config.i18n.locales);
var locales = new locale.Locales(connection.rawConnection.req.headers["accept-language"]);
var ulocale = locales.best(supported).toString();
if(_.indexOf(api.config.i18n.locales, ulocale) !== -1){
clocale = ulocale;
}
}
/* IF NOT, IT DOES NOT LOCALIZE */
/* UPDATE: DO NOT DO THIS */
api.i18n.setLocale(clocale);
/* --- */
return clocale;
}
};
next();
},
start: function(api, next){
next();
},
stop: function(api, next){
next();
}
};
Issue Analytics
- State:
- Created 7 years ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
I don’t think I follow what you are trying to do here. The line
api.i18n.setLocale(clocale)
seems to set the system locale, but if you are trying to respond to a client in the proper language, you actually need to set the connection’s locale. ’To set the connection’s locale, you should tell the config (https://github.com/evantahler/actionhero/blob/master/config/i18n.js#L18-L21) which method to use. This is the method which probably uses
locale
to check the accept-headers. This method just returns a string locale, ie:en-US
. Then, in your actions you can usedata.connection.loacalize()
to generate strings in the proper langugeOk then! 😃