Django provides two mechanisms to internationalize URL patterns:
Adding the language prefix to the root of the URL patterns to make it possible for LocaleMiddleware to detect the language to activate from the requested URL.
Making URL patterns themselves translatable via the django.utils.translation.ugettext_lazy() function.
Using either one of these features requires that an active language be set for each request; in other words, you need to have django.middleware.locale.LocaleMiddleware in your MIDDLEWARE_CLASSES setting.
Language Prefix in URL Patterns
This function can be used in your root URLconf and Django will automatically prepend the current active language code to all url patterns defined within i18n_patterns(). Example URL patterns:
After defining these URL patterns, Django will automatically add the language prefix to the URL patterns that were added by the i18n_patterns function. Example:
i18n_patterns() is only allowed in your root URLconf. Using it within an included URLconf will throw an ImproperlyConfigured exception.
Translating URL Patterns
URL patterns can also be marked translatable using the ugettext_lazy() function. Example:
After you’ve created the translations, the reverse() function will return the URL in the active language. Example:
In most cases, it’s best to use translated URLs only within a language-code-prefixed block of patterns (using i18n_patterns()), to avoid the possibility that a carelessly translated URL causes a collision with a non-translated URL pattern.
Reversing in Templates
If localized URLs get reversed in templates, they always use the current language. To link to a URL in another language use the language template tag. It enables the given language in the enclosed template section: