2 Utilities for urlconfs.
6 if django.VERSION >= (1, 10):
8 from django.conf.urls.i18n import i18n_patterns as django_i18n_patterns
10 def i18n_patterns(*args):
12 "As of Django 1.10, fnpdjango.utils.urls.i18n_patterns "
13 "is deprecated in favor of directly using "
14 "django.urls.i18n_patterns(prefix_default_language=False).",
16 return django_i18n_patterns(*args, prefix_default_language=False)
21 from django.conf import settings
22 from django.core.urlresolvers import LocaleRegexURLResolver
23 from django.utils.translation import get_language
25 class MyLocaleRegexURLResolver(LocaleRegexURLResolver):
27 A URL resolver that always matches the active language code as URL prefix.
29 Rather than taking a regex argument, we just override the ``regex``
30 function to always return the active language-code as regex.
34 language_code = get_language()
35 if language_code == settings.LANGUAGE_CODE:
37 if language_code not in self._regex_dict:
38 regex_compiled = re.compile('^%s/' % language_code, re.UNICODE)
39 self._regex_dict[language_code] = regex_compiled
40 return self._regex_dict[language_code]
42 def i18n_patterns(*args):
44 Adds the language code prefix to every URL pattern within this
45 function. This may only be used in the root URLconf, not in an included
48 pattern_list = list(args)
49 if not settings.USE_I18N:
51 return pattern_list + [MyLocaleRegexURLResolver(pattern_list)]