From 826919c97c3fa61f46bef2e2a3708a16f956cfce Mon Sep 17 00:00:00 2001 From: Sebastian Spaeth Date: Sun, 2 Dec 2012 02:35:04 +0100 Subject: [PATCH] en_US is always available We were using "en" as fallback only when no preferred language matched. This is obviously bad. Always insert en_US as available locale, so we can match it with the accept_languages. Don't set available_locales as mg_global, per discussion with paroneaya, make it a global var in translate.py Signed-off-by: Sebastian Spaeth --- mediagoblin/init/__init__.py | 4 ++-- mediagoblin/mg_globals.py | 2 -- mediagoblin/tools/translate.py | 15 ++++++++------- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/mediagoblin/init/__init__.py b/mediagoblin/init/__init__.py index 88af377e..8d70c4ef 100644 --- a/mediagoblin/init/__init__.py +++ b/mediagoblin/init/__init__.py @@ -19,7 +19,7 @@ from beaker.util import parse_cache_config_options import jinja2 from mediagoblin.tools import staticdirect -from mediagoblin.tools.translate import get_available_locales +from mediagoblin.tools.translate import set_available_locales from mediagoblin.init.config import ( read_mediagoblin_config, generate_validation_report) from mediagoblin import mg_globals @@ -40,7 +40,7 @@ class ImproperlyConfigured(Error): def setup_locales(): """Checks which language translations are available and sets them""" - setup_globals(available_locales=get_available_locales()) + set_available_locales() def setup_global_and_app_config(config_path): diff --git a/mediagoblin/mg_globals.py b/mediagoblin/mg_globals.py index 646fbdbd..356a944d 100644 --- a/mediagoblin/mg_globals.py +++ b/mediagoblin/mg_globals.py @@ -45,8 +45,6 @@ workbench_manager = None # A thread-local scope thread_scope = threading.local() -# a list of translated locales -available_locales = None # gettext (this will be populated on demand with gettext.Translations) thread_scope.translations = None diff --git a/mediagoblin/tools/translate.py b/mediagoblin/tools/translate.py index 2d83316e..8c413056 100644 --- a/mediagoblin/tools/translate.py +++ b/mediagoblin/tools/translate.py @@ -27,18 +27,19 @@ from mediagoblin import mg_globals # Translation tools ################### - +AVAILABLE_LOCALES = None TRANSLATIONS_PATH = pkg_resources.resource_filename( 'mediagoblin', 'i18n') -def get_available_locales(): - """Return a list of locales for which we have translations""" - locales=[] +def set_available_locales(): + """Set available locales for which we have translations""" + global AVAILABLE_LOCALES + locales=['en', 'en_US'] # these are available without translations for locale in localedata.list(): if gettext.find('mediagoblin', TRANSLATIONS_PATH, [locale]): locales.append(locale) - return locales + AVAILABLE_LOCALES = locales def locale_to_lower_upper(locale): @@ -71,6 +72,7 @@ def get_locale_from_request(request): Figure out what target language is most appropriate based on the request """ + global AVAILABLE_LOCALES request_form = request.args or request.form if request_form.has_key('lang'): @@ -87,8 +89,7 @@ def get_locale_from_request(request): # 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_match( - mg_globals.available_locales) or 'en' + target_lang = request.accept_languages.best_match(AVAILABLE_LOCALES) return locale_to_lower_upper(target_lang) -- 2.25.1