Mercurial > libervia-web
comparison libervia/server/pages.py @ 1169:97e850e6fae9
pages (i18n): if locale is not specified, "accept-language" header is used to try to determine the best one
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 12 Apr 2019 14:08:02 +0200 |
parents | ea0caa7b1bcc |
children | 469d0de8da0e |
comparison
equal
deleted
inserted
replaced
1168:ea0caa7b1bcc | 1169:97e850e6fae9 |
---|---|
1409 login_url = self.getPageRedirectURL(request) | 1409 login_url = self.getPageRedirectURL(request) |
1410 self.HTTPRedirect(request, login_url) | 1410 self.HTTPRedirect(request, login_url) |
1411 | 1411 |
1412 return data | 1412 return data |
1413 | 1413 |
1414 def setBestLocale(self, request): | |
1415 """Guess the best locale when it is not specified explicitly by user | |
1416 | |
1417 This method will check "accept-language" header, and set locale to first | |
1418 matching value with available translations. | |
1419 """ | |
1420 accept_language = request.getHeader("accept-language") | |
1421 if not accept_language: | |
1422 return | |
1423 accepted = {a.strip() for a in accept_language.split(',')} | |
1424 available = [unicode(l) for l in self.host.renderer.translations] | |
1425 for lang in accepted: | |
1426 lang = lang.split(';')[0].strip().lower() | |
1427 if not lang: | |
1428 continue | |
1429 for a in available: | |
1430 if a.lower().startswith(lang): | |
1431 session_data = self.host.getSessionData(request, | |
1432 session_iface.ISATSession) | |
1433 session_data.locale = a | |
1434 return | |
1435 | |
1414 def renderPartial(self, request, template, template_data): | 1436 def renderPartial(self, request, template, template_data): |
1415 """Render a template to be inserted in dynamic page | 1437 """Render a template to be inserted in dynamic page |
1416 | 1438 |
1417 this is NOT the normal page rendering method, it is used only to update | 1439 this is NOT the normal page rendering method, it is used only to update |
1418 dynamic pages | 1440 dynamic pages |
1491 log.warning(_(u'illegal char found in locale ("/"), hack ' | 1513 log.warning(_(u'illegal char found in locale ("/"), hack ' |
1492 u'attempt? locale={locale}').format(locale=locale)) | 1514 u'attempt? locale={locale}').format(locale=locale)) |
1493 locale = None | 1515 locale = None |
1494 session_data.locale = locale | 1516 session_data.locale = locale |
1495 | 1517 |
1518 # if locale is not specified, we try to find one requested by browser | |
1519 if session_data.locale is None: | |
1520 self.setBestLocale(request) | |
1496 | 1521 |
1497 d = defer.Deferred() | 1522 d = defer.Deferred() |
1498 d.addCallback(self._checkAccess, request) | 1523 d.addCallback(self._checkAccess, request) |
1499 | 1524 |
1500 if self.redirect is not None: | 1525 if self.redirect is not None: |