Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Some optimization / cleanup.

  • Loading branch information...
commit e5ebd4ac63a5a2247d27fb1d3cd2e4b195c12d97 1 parent 0317a93
@brocaar authored
Showing with 16 additions and 20 deletions.
  1. +12 −16 i18nurls/middleware.py
  2. +4 −4 i18nurls/urlresolvers.py
View
28 i18nurls/middleware.py
@@ -1,13 +1,17 @@
+import re
+
from django.conf import settings
from django.http import HttpResponseRedirect
from django.utils import translation
+language_code_prefix_regex = re.compile(r'^/([\w-]+)/')
+
class LocaleMiddleware(object):
def process_request(self, request):
"""
- If the `request.path` starts with a (valid) language-code prefix, this
+ If `request.path` starts with a (valid) language-code prefix, this
language-code will be activated. Else `get_language_from_request` is
used as a fallback.
"""
@@ -21,8 +25,8 @@ def process_request(self, request):
def process_response(self, request, response):
"""
- Set the Content-Language header. If `response.status_code` is 404, and
- no language-code prefix is found, return a `HttpResponseRedirect` to the
+ Sets the Content-Language header. If `response.status_code` is 404, and
+ no language-code prefix is found, returns a `HttpResponseRedirect` to the
language-code prefixed `request.path`.
"""
language_code = translation.get_language()
@@ -37,16 +41,8 @@ def process_response(self, request, response):
return response
def _language_code_from_path(self, path):
- """
- Return the language-code found in the requested path. For example
- `/en/my/path/` will return `en`. Return `None` if no language-code
- could be found.
- """
- language_code = None
- path_parts = path.split('/')
- if len(path_parts) > 2:
- possible_language_code = path_parts[1]
- for language in settings.LANGUAGES:
- if language[0] == possible_language_code:
- language_code = possible_language_code
- return language_code
+ regex_match = language_code_prefix_regex.match(path)
+ if regex_match:
+ if regex_match.group(1) in dict(settings.LANGUAGES):
+ return regex_match.group(1)
+ return None
View
8 i18nurls/urlresolvers.py
@@ -36,7 +36,8 @@ def __init__(self, regex, callback, default_args=None, name=None):
self._i18n_regex = regex
self._i18n_regex_dict = {}
- def get_regex(self):
+ @property
+ def regex(self):
language_code = get_language()
if language_code not in self._i18n_regex_dict:
@@ -53,7 +54,6 @@ def get_regex(self):
self._i18n_regex_dict[language_code] = compiled_regex
return self._i18n_regex_dict[language_code]
- regex = property(get_regex)
class I18NRegexURLResolver(RegexURLResolver):
@@ -80,7 +80,8 @@ def __init__(self, regex, urlconf_name, default_kwargs=None, app_name=None, name
self._i18n_namespace_dict = {}
self._i18n_app_dict = {}
- def get_regex(self):
+ @property
+ def regex(self):
language_code = get_language()
if language_code not in self._i18n_regex_dict:
@@ -97,7 +98,6 @@ def get_regex(self):
self._i18n_regex_dict[language_code] = compiled_regex
return self._i18n_regex_dict[language_code]
- regex = property(get_regex)
def _populate(self):
# Almost the same as the original `_populate` function, except the last
Please sign in to comment.
Something went wrong with that request. Please try again.