Created
April 11, 2017 23:54
-
-
Save mgd020/b6ca52e7d0d887fd93c7e0b3e86805f8 to your computer and use it in GitHub Desktop.
Translate django url strings by slugs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import absolute_import, division, print_function, unicode_literals | |
import re | |
from django.utils import six | |
from django.utils.functional import lazy, SimpleLazyObject | |
from django.utils.translation import ugettext | |
URL_SLUG_RE = SimpleLazyObject(lambda: re.compile(r'^[a-zA-Z0-9\-]+$')) | |
URL_DELIMS_RE = SimpleLazyObject(lambda: re.compile(r'(\^?)(?<=\^|/)?(?P<slug>[a-zA-Z0-9\-]+)(?=/|$)')) | |
def ugeturl(url): | |
return ''.join(ugettext(part) if part and URL_SLUG_RE.match(part) else part for part in URL_DELIMS_RE.split(url)) | |
ugeturl_lazy = lazy(ugeturl, six.text_type) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment