I would like to use the new webapp2 features for localization, which also have local formatting for time and currency.
Django has a nice function called get_language_from_request that I used before I switched to webapp2 completely, and now I use i18n from webapp2, and I can switch between the localizations that I write with gettext and compile files called messages.mo which my application can read and display. Then I identified and prioritized the following ways to get a custom language: 1. HTTP GET, for example. hl = pt-br for the Brazilian Portuguese 2. HTTP SESSION variable I call i18n_language 3. Cookies that I have to set, but I don’t know exactly how 4. The HTTP header I could get, and here I don’t know exactly and I watch how djnango does this with the convenient get_language_from_request that I used, and now I have stopped importing django, and I still want this functionality for my now webapp2 based code.
def get_language_from_request(self, request): """ Analyzes the request to find what language the user wants the system to show. If the user requests a sublanguage where we have a main language, we send out the main language. """ if self.request.get('hl'): self.session['i18n_language'] = self.request.get('hl') return self.request.get('hl') if self.session: lang_code = self.session.get('i18n_language', None) if lang_code: logging.info('language found in session') return lang_code lang_code = Cookies(self).get(LANGUAGE_COOKIE_NAME) if lang_code: logging.info('language found in cookies') return lang_code accept = os.environ.get('HTTP_ACCEPT_LANGUAGE', '') for accept_lang, unused in self.parse_accept_lang_header(accept): logging.info('accept_lang:'+accept_lang) lang_code = accept_lang return lang_code
I see that django code is available, but I don’t know how much i18n from webapp2 does, for example, I have to take care of rollback for languages like pt-br, should go back to pt if not. mo for pt-br and similar for other dialects.
Actually setting the language that I can do with
i18n.get_i18n().set_locale(language)
I ask your help to give priority to different ways of getting a custom language, and I would also like to know your ideas on how to continue implementation. Or do you think that I can only do using the session variable, and not so carefully about the “full” solution, since in any case I mainly correct the language for geographic use, where my only actual translations used are now Brazilian Portuguese and English but I want it well prepared for switching to Spanish and Russian and other languages, so I would like to be able to switch to the user's language and at least save it in a webapp2 session and find out what you think ete also about using cookie and header to get the user's language.
The source code that I used for si from django looks like this and I can no longer use it because it is blocked for django.mo files and specific to django
def get_language_from_request(request): """ Analyzes the request to find what language the user wants the system to show. Only languages listed in settings.LANGUAGES are taken into account. If the user requests a sublanguage where we have a main language, we send out the main language. """ global _accepted from django.conf import settings globalpath = os.path.join(os.path.dirname(sys.modules[settings.__module__].__file__), 'locale') supported = dict(settings.LANGUAGES) if hasattr(request, 'session'): lang_code = request.session.get('django_language', None) if lang_code in supported and lang_code is not None and check_for_language(lang_code): return lang_code lang_code = request.COOKIES.get(settings.LANGUAGE_COOKIE_NAME) if lang_code and lang_code not in supported: lang_code = lang_code.split('-')[0]
Is it possible to do this for each request? And I think I should also set the header to self.response.headers['Content-Language'] = language
According to my expectation, I can take some functions directly from django if I decide to use http headers, but I don’t understand what it does, maybe you can explain this code for me from django:
def parse_accept_lang_header(lang_string): """ Parses the lang_string, which is the body of an HTTP Accept-Language header, and returns a list of (lang, q-value), ordered by 'q' values. Any format errors in lang_string results in an empty list being returned. """ result = [] pieces = accept_language_re.split(lang_string) if pieces[-1]: return [] for i in range(0, len(pieces) - 1, 3): first, lang, priority = pieces[i : i + 3] if first: return [] priority = priority and float(priority) or 1.0 result.append((lang, priority)) result.sort(lambda x, y: -cmp(x[1], y[1])) return result
thanks
Update
I found that I could not use sessions in the initialize function of the request handler, possibly because the session object had not yet been created. So I put the code to get the language from the session, which performs the rendering function of BaseHandler, and it seems to work. It would also be nice to consider the headers or cookie value.