I've recently added Babel to my GAE/webapp2 site, and it works, but I'm confused about the proper way to get the locale.
In this answer, he gets the locale with self.request.get('locale', 'es_ES')
but this only seems to work if something like ?locale=fr_FR
is in the URL. I've never seen anyone set a locale in the URL. Is there any reason to get a locale from the URL?
Getting the locale from the headers seems to make more sense:
self.request.headers.get('Accept-Language').split(",")
One quirk is that browsers provide locale with a hyphen and lower case ("fr-fr") but Babel expects an underscore and mixed case (fr_FR). Where the browser provides a locale header of only "fr-fr", the following:
locale = self.request.headers.get('Accept-Language').split(",")[0]
i18n.get_i18n().set_locale(locale)
will end up using the default language rather than French because Babel is looking for "fr_FR". Should I take the locale from the header and convert the hyphen to underscore and lower case to mixed case? Seems that Babel should do that for me.
(I know that the header may have multiple locales and that I should check them all, but I'd still need to do the conversion for each one.)