diff options
author | Sebastian Spaeth <Sebastian@SSpaeth.de> | 2012-12-01 23:35:52 +0100 |
---|---|---|
committer | Sebastian Spaeth <Sebastian@SSpaeth.de> | 2012-12-01 23:40:10 +0100 |
commit | 7b9f9d1edb96aabab7e35818824b71efbdd4efb9 (patch) | |
tree | 5305ce15c5e15355e6bd072599459a661c986d39 | |
parent | 7989cd6e4961749a39147c9020dbae745b4f23de (diff) | |
download | mediagoblin-7b9f9d1edb96aabab7e35818824b71efbdd4efb9.tar.lz mediagoblin-7b9f9d1edb96aabab7e35818824b71efbdd4efb9.tar.xz mediagoblin-7b9f9d1edb96aabab7e35818824b71efbdd4efb9.zip |
Fix i18n in our browser
We only ever served english pages since the switch to werkzeug's requests.
Fix this by actually checking the accepted languages that our web browser
sends and using that or falling back to english.
This is not optimal, imaging our browser sends "klingon, de" as accepted
languages and we happen to not have a klingon translation ready (a deficiency
that should be corrected immediately anyway!!). We would then fall back
to english rather than sending the sensible and pleasant German language
which the user would understand. This will require more backend work though.
Removing the gettext.find() in mg_globals.py. It looked in the wrong directory
anyway (mediagoblin/translations) and as that does not exist, had always returned
None without anyone noticing.
Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
-rw-r--r-- | mediagoblin/app.py | 1 | ||||
-rw-r--r-- | mediagoblin/mg_globals.py | 7 | ||||
-rw-r--r-- | mediagoblin/tools/translate.py | 25 |
3 files changed, 15 insertions, 18 deletions
diff --git a/mediagoblin/app.py b/mediagoblin/app.py index 3a2d00f0..de421aca 100644 --- a/mediagoblin/app.py +++ b/mediagoblin/app.py @@ -134,7 +134,6 @@ class MediaGoblinApp(object): ## Compatibility webob -> werkzeug request.GET = request.args - request.accept_language = request.accept_languages request.accept = request.accept_mimetypes ## Routing / controller loading stuff diff --git a/mediagoblin/mg_globals.py b/mediagoblin/mg_globals.py index fffa1dda..356a944d 100644 --- a/mediagoblin/mg_globals.py +++ b/mediagoblin/mg_globals.py @@ -45,11 +45,8 @@ workbench_manager = None # A thread-local scope thread_scope = threading.local() -# gettext -thread_scope.translations = gettext.find( - 'mediagoblin', - pkg_resources.resource_filename( - 'mediagoblin', 'translations'), ['en']) +# gettext (this will be populated on demand with gettext.Translations) +thread_scope.translations = None # app and global config objects app_config = None diff --git a/mediagoblin/tools/translate.py b/mediagoblin/tools/translate.py index 01cabe6a..ce670451 100644 --- a/mediagoblin/tools/translate.py +++ b/mediagoblin/tools/translate.py @@ -32,7 +32,7 @@ TRANSLATIONS_PATH = pkg_resources.resource_filename( def locale_to_lower_upper(locale): """ - Take a locale, regardless of style, and format it like "en-US" + Take a locale, regardless of style, and format it like "en_US" """ if '-' in locale: lang, country = locale.split('-', 1) @@ -60,17 +60,23 @@ def get_locale_from_request(request): Figure out what target language is most appropriate based on the request """ - request_form = request.GET or request.form + request_form = request.args or request.form if request_form.has_key('lang'): - return locale_to_lower_upper(request_form['lang']) + # User explicitely demanded a language + target_lang = request_form['lang'] - if 'target_lang' in request.session: + elif 'target_lang' in request.session: + # TODO: Uh, ohh, this is never ever set anywhere? target_lang = request.session['target_lang'] - # Pull the first acceptable language or English else: - # TODO: Internationalization broken - target_lang = 'en' + # Pull the first acceptable language or English + # This picks your favorite browser lingo, falling back to 'en' + + # TODO: We need a list of available locales, and match with the list + # of accepted locales, and serve the best available locale rather than + # the most preferred, or fall back to 'en' immediately. + target_lang = request.accept_languages.best return locale_to_lower_upper(target_lang) @@ -97,11 +103,6 @@ def setup_gettext(locale): mg_globals.thread_scope.translations = this_gettext -# Force en to be setup before anything else so that -# mg_globals.translations is never None -setup_gettext('en') - - def pass_to_ugettext(*args, **kwargs): """ Pass a translation on to the appropriate ugettext method. |