Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
django default language_code no prefix urls
import re
import logging
from django.conf import settings
from django.urls import LocaleRegexURLResolver
from django.utils.translation import get_language
logger = logging.getLogger("django")
class NoPrefixLocaleRegexURLResolver(LocaleRegexURLResolver):
@property
def regex(self):
language_code = get_language()
if language_code not in self._regex_dict:
regex_compiled = (re.compile('', re.UNICODE)
if language_code == settings.LANGUAGE_CODE
else re.compile('^%s/' % language_code, re.UNICODE))
self._regex_dict[language_code] = regex_compiled
return self._regex_dict[language_code]
def i18n_patterns(*urls, **kwargs):
if not settings.USE_I18N:
return list(urls)
prefix_default_language = kwargs.pop('prefix_default_language', True)
logger.info(prefix_default_language)
assert not kwargs, 'Unexpected kwargs for i18n_patterns(): %s' % kwargs
return [NoPrefixLocaleRegexURLResolver(list(urls), prefix_default_language=prefix_default_language)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.