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:
parent
e4e7fbeeaa
commit
826919c97c
@ -19,7 +19,7 @@ from beaker.util import parse_cache_config_options
|
|||||||
import jinja2
|
import jinja2
|
||||||
|
|
||||||
from mediagoblin.tools import staticdirect
|
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 (
|
from mediagoblin.init.config import (
|
||||||
read_mediagoblin_config, generate_validation_report)
|
read_mediagoblin_config, generate_validation_report)
|
||||||
from mediagoblin import mg_globals
|
from mediagoblin import mg_globals
|
||||||
@ -40,7 +40,7 @@ class ImproperlyConfigured(Error):
|
|||||||
|
|
||||||
def setup_locales():
|
def setup_locales():
|
||||||
"""Checks which language translations are available and sets them"""
|
"""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):
|
def setup_global_and_app_config(config_path):
|
||||||
|
@ -45,8 +45,6 @@ workbench_manager = None
|
|||||||
# A thread-local scope
|
# A thread-local scope
|
||||||
thread_scope = threading.local()
|
thread_scope = threading.local()
|
||||||
|
|
||||||
# a list of translated locales
|
|
||||||
available_locales = None
|
|
||||||
# gettext (this will be populated on demand with gettext.Translations)
|
# gettext (this will be populated on demand with gettext.Translations)
|
||||||
thread_scope.translations = None
|
thread_scope.translations = None
|
||||||
|
|
||||||
|
@ -27,18 +27,19 @@ from mediagoblin import mg_globals
|
|||||||
# Translation tools
|
# Translation tools
|
||||||
###################
|
###################
|
||||||
|
|
||||||
|
AVAILABLE_LOCALES = None
|
||||||
TRANSLATIONS_PATH = pkg_resources.resource_filename(
|
TRANSLATIONS_PATH = pkg_resources.resource_filename(
|
||||||
'mediagoblin', 'i18n')
|
'mediagoblin', 'i18n')
|
||||||
|
|
||||||
|
|
||||||
def get_available_locales():
|
def set_available_locales():
|
||||||
"""Return a list of locales for which we have translations"""
|
"""Set available locales for which we have translations"""
|
||||||
locales=[]
|
global AVAILABLE_LOCALES
|
||||||
|
locales=['en', 'en_US'] # these are available without translations
|
||||||
for locale in localedata.list():
|
for locale in localedata.list():
|
||||||
if gettext.find('mediagoblin', TRANSLATIONS_PATH, [locale]):
|
if gettext.find('mediagoblin', TRANSLATIONS_PATH, [locale]):
|
||||||
locales.append(locale)
|
locales.append(locale)
|
||||||
return locales
|
AVAILABLE_LOCALES = locales
|
||||||
|
|
||||||
|
|
||||||
def locale_to_lower_upper(locale):
|
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
|
Figure out what target language is most appropriate based on the
|
||||||
request
|
request
|
||||||
"""
|
"""
|
||||||
|
global AVAILABLE_LOCALES
|
||||||
request_form = request.args or request.form
|
request_form = request.args or request.form
|
||||||
|
|
||||||
if request_form.has_key('lang'):
|
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
|
# TODO: We need a list of available locales, and match with the list
|
||||||
# of accepted locales, and serve the best available locale rather than
|
# of accepted locales, and serve the best available locale rather than
|
||||||
# the most preferred, or fall back to 'en' immediately.
|
# the most preferred, or fall back to 'en' immediately.
|
||||||
target_lang = request.accept_languages.best_match(
|
target_lang = request.accept_languages.best_match(AVAILABLE_LOCALES)
|
||||||
mg_globals.available_locales) or 'en'
|
|
||||||
|
|
||||||
return locale_to_lower_upper(target_lang)
|
return locale_to_lower_upper(target_lang)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user