I want to develop a site with 2 languages, a default one, my native language and an optional English. I plan to have my domains as such:
www.mydomain.com/tr/
www.mydomain.com/en/
By default, once a user enter mydomain.com. they will be redirected to /tr/
version and select to go to the /en/ if they want via a top menu. And here is my question.
What is the best Django way to maintain both languages, please note that I don't want automatic translation but I want to maintain texts for both languages myself.
Thanks
As I see it, you have two main options here:
(1) You can keep two separate copies of the site as different Django apps and simply have your urlconf point to those apps-- so
url(r'^/en/', include(myproject.en))
would be in your urlconf to point to your English app, and the other for the other language. This would involve maintaining different sets of urlconfs and different html templates, etc for the two apps, which could be useful if you're interested in having the URLs themselves also reflect the different languages (eg Spanish "/pagina/uno" vs English "/page/one").(2) You record the language preference in a cookie (which you should really do anyway) using Django sessions, and then have the template deliver the appropriate version of the text however you like from that cookie. The code for this could be:
And then in the templates, to gather this information, you'd use:
So here is the long version to your question. Tested on Django 1.4 thru 1.7.1:
In settings.py …
Add to
MIDDLEWEAR_CLASSES
, locale, it enables language selection based on request:Add
LOCALE_PATHS
, this is where your translation files will be stored:Enable i18N
Set
LANGUAGES
that you will be translating the site to:Add i18n template context processor, requests will now include
LANGUAGES
andLANGUAGE_CODE
:For Django <1.8 put it here:
For Django >= 1.8 put it here:
Nest, in urls.py :
In url_patterns, add the below, it will enable the set language redirect view:
See Miscellaneous in Translations for more on this.
Add the following imports, and encapsulate the urls you want translated with i18n_patterns. Here is what mine looks like:
Note: You can also drop your admin urls into the i18n_patterns.
Wrap your text with lazytext! import lazytext (as above) and wrap every string with it like so _('text'), you can even go to your other urls.py files and do url translation like so:
You can wrap text that you want translated in your other files, such as models.py, views.py etc.. Here is an example model field with translations for label and help_text:
Django translation docs are great for this!
In your html templates...
Now you can go into your templates and load the i18n templatetag and use trans and transblock on the static stuff you want to translate. Here is an example:
Now run a makemessages for each of your locales:
And now all is left is to go into your /locales folder, and edit each of the .po files. Fill in the data for each msgstr. Here is one such example of that:
And finally compile the messages:
There is a lot more to learn with translations and internationalization is closely related to this topic, so check out the docs for it too. I also recommend checking out some of the internationalization packages available for Django like django-rosetta, and django-linguo. They help translate model content, django-rosetta does not create new entries for this in your database, while django-linguo does.
I also created a django translation demo for those interested to look at a full working solution.
If you followed this you should be off to a good start. I believe this is the most standardized way to get your site running in multiple languages. Cheers!
You should go for the django-localeurl app if you need to to specify the language of a page in the URL. I.e. specifically what you're doing in your example. The default Django translation machinery just prefixes your URLs which can be ineffective in your SEO strategy.
django-localeurl
just fixes the URL handling part, whilst playing nice with the rest of the default Django translation machinery.You could use translatable URL patterns, which are available since 1.4.
https://docs.djangoproject.com/en/dev/topics/i18n/translation/#url-internationalization