changeset 99:906941e7cb6b

i18n: use babel to determine supported languages for accept_languages best match, add debug logging
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Tue, 08 Mar 2011 11:26:18 +0100
parents 29f2e129dd36
children 1857720a1372
files MoinMoin/i18n/__init__.py
diffstat 1 files changed, 11 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/i18n/__init__.py	Mon Mar 07 22:21:10 2011 +0100
+++ b/MoinMoin/i18n/__init__.py	Tue Mar 08 11:26:18 2011 +0100
@@ -14,8 +14,9 @@
 """
 
 
+from babel import Locale
+
 from flask import current_app, request, flaskg
-
 from flaskext.babel import Babel, gettext, ngettext, lazy_gettext
 
 _ = gettext
@@ -41,13 +42,20 @@
     if u and u.locale is not None:
         # locale is given in user profile, use it
         locale = u.locale
+        logging.debug("user locale = %r" % locale)
     else:
         # try to guess the language from the user accept
         # header the browser transmits. The best match wins.
-        supported_languages = ['de', 'fr', 'en'] # XXX
-        locale = request.accept_languages.best_match(supported_languages)
+        logging.debug("request.accept_languages = %r" % request.accept_languages)
+        supported_locales = [Locale('en')] + current_app.babel_instance.list_translations()
+        logging.debug("supported_locales = %r" % supported_locales)
+        supported_languages = [str(l) for l in supported_locales]
+        logging.debug("supported_languages = %r" % supported_languages)
+        locale = request.accept_languages.best_match(supported_languages, 'en')
+        logging.debug("best match locale = %r" % locale)
     if not locale:
         locale = current_app.cfg.locale_default
+        logging.debug("default locale = %r" % locale)
     return locale