8đź‘Ť
Checkout django-locale-url.
It provides a middleware that does exactly what you are asking for, so you don’t need to check for the language in urls.py
3đź‘Ť
A number of ways to do this that come to mind. Arguably the most “standards compliant” way would be to use the HTTP Accept-Language
header, which is available to views as request.META['HTTP_ACCEPT_LANGUAGE']
to determine the language in which the user prefers to receive resources and simply return a translated HttpResponse
in the appropriate language.
Another common way, more along the lines of what you are describing, is to ask the user to select a language on their first arrival and store the selection in the session. Once the user makes a choice, redirect the browser to the appropriate language subdirectory and use relative links in your views so as not to have to worry about crossing languages. You can adjust your URLconf to pass a language keyword to your view like so:
urlpatterns = patterns('',
(r'^(?P<lang>[a-zA-Z]{2})/ ...
There is an Internationalization/Localization page on the Django documentation site about i18n that might help you get started.
- TypeError: argument of type 'WindowsPath' is not iterable – in django python
- Django – how to know in clean method if the form-data is new or if old data is being changed
3đź‘Ť
Nowadays the best way is using the built-in language prefix in url patterns:
https://docs.djangoproject.com/en/1.8/topics/i18n/translation/#language-prefix-in-url-patterns
- Django Bi-directional ManyToMany – How to prevent table creation on second model?
- NoReverseMatch at /
- Use prefetch_related in django_simple_history
- Django Custom User Model errors ((admin.E108) The value of 'list_display[2]' refers to 'first_name', which is not a callable
- Separating Django App Views