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 <Sebastian@SSpaeth.de>
This commit is contained in:
Sebastian Spaeth 2012-12-02 02:35:04 +01:00
parent e4e7fbeeaa
commit 826919c97c
3 changed files with 10 additions and 11 deletions

View File

@ -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):

View File

@ -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

View File

@ -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)