diff options
| author | Jannis Leidel <jannis@leidel.info> | 2011-06-15 17:29:10 +0000 |
|---|---|---|
| committer | Jannis Leidel <jannis@leidel.info> | 2011-06-15 17:29:10 +0000 |
| commit | 896e3c69c7eec311085da349a329ee80c8fca132 (patch) | |
| tree | dcc608e4b47fb9621e731f3af476443a56ca56aa /django/utils | |
| parent | 62bb4b8c37ada04c10d2a37990326636ea7791d6 (diff) | |
Fixed #11585 -- Added ability to translate and prefix URL patterns with a language code as an alternative method for language discovery. Many thanks to Orne Brocaar for his initial work and Carl Meyer for feedback.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16405 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/utils')
| -rw-r--r-- | django/utils/translation/__init__.py | 3 | ||||
| -rw-r--r-- | django/utils/translation/trans_null.py | 4 | ||||
| -rw-r--r-- | django/utils/translation/trans_real.py | 24 |
3 files changed, 29 insertions, 2 deletions
diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py index adb5566da3..acf4d8dead 100644 --- a/django/utils/translation/__init__.py +++ b/django/utils/translation/__init__.py @@ -144,6 +144,9 @@ def to_locale(language): def get_language_from_request(request): return _trans.get_language_from_request(request) +def get_language_from_path(path): + return _trans.get_language_from_path(path) + def templatize(src, origin=None): return _trans.templatize(src, origin) diff --git a/django/utils/translation/trans_null.py b/django/utils/translation/trans_null.py index 5622c03f55..daee96eb75 100644 --- a/django/utils/translation/trans_null.py +++ b/django/utils/translation/trans_null.py @@ -58,3 +58,7 @@ def to_locale(language): def get_language_from_request(request): return settings.LANGUAGE_CODE + +def get_language_from_path(request): + return None + diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py index 9e8285bcf7..9f930b54ef 100644 --- a/django/utils/translation/trans_real.py +++ b/django/utils/translation/trans_real.py @@ -35,6 +35,8 @@ accept_language_re = re.compile(r''' (?:\s*,\s*|$) # Multiple accepts per header. ''', re.VERBOSE) +language_code_prefix_re = re.compile(r'^/([\w-]+)/') + def to_locale(language, to_lower=False): """ Turns a language name (en-us) into a locale name (en_US). If 'to_lower' is @@ -336,14 +338,28 @@ def check_for_language(lang_code): """ Checks whether there is a global language file for the given language code. This is used to decide whether a user-provided language is - available. This is only used for language codes from either the cookies or - session and during format localization. + available. This is only used for language codes from either the cookies + or session and during format localization. """ for path in all_locale_paths(): if gettext_module.find('django', path, [to_locale(lang_code)]) is not None: return True return False +def get_language_from_path(path, supported=None): + """ + Returns the language-code if there is a valid language-code + found in the `path`. + """ + if supported is None: + from django.conf import settings + supported = dict(settings.LANGUAGES) + regex_match = language_code_prefix_re.match(path) + if regex_match: + lang_code = regex_match.group(1) + if lang_code in supported and check_for_language(lang_code): + return lang_code + def get_language_from_request(request): """ Analyzes the request to find what language the user wants the system to @@ -355,6 +371,10 @@ def get_language_from_request(request): from django.conf import settings supported = dict(settings.LANGUAGES) + lang_code = get_language_from_path(request.path_info, supported) + if lang_code is not None: + return lang_code + if hasattr(request, 'session'): lang_code = request.session.get('django_language', None) if lang_code in supported and lang_code is not None and check_for_language(lang_code): |
