2 Utilities for urlconfs.
6 from django.conf import settings
7 from django.core.urlresolvers import LocaleRegexURLResolver
8 from django.utils.translation import get_language
11 class MyLocaleRegexURLResolver(LocaleRegexURLResolver):
13 A URL resolver that always matches the active language code as URL prefix.
15 Rather than taking a regex argument, we just override the ``regex``
16 function to always return the active language-code as regex.
20 language_code = get_language()
21 if language_code == settings.LANGUAGE_CODE:
23 if language_code not in self._regex_dict:
24 regex_compiled = re.compile('^%s/' % language_code, re.UNICODE)
25 self._regex_dict[language_code] = regex_compiled
26 return self._regex_dict[language_code]
29 def i18n_patterns(*args):
31 Adds the language code prefix to every URL pattern within this
32 function. This may only be used in the root URLconf, not in an included
36 pattern_list = list(args)
37 if not settings.USE_I18N:
39 return pattern_list + [MyLocaleRegexURLResolver(pattern_list)]