Phrixus
Phrixus

Reputation: 1219

Django how to allow translation from admin and guest language picker

I have read the documentation from django on how to make your website available in more languages.

To be honest all sounds really complicated so I need help to clear everything.

First of all, I would like to do the following:

  1. Provide the website into multiple languages but only to guests. So they will be able to change languages even if they are not logged in. I thought that this maybe possible using URL like website.com/en/index website.com/es/index etc. I found this project but seems out of date and I am looking if there is any built-in way to do the same.
  2. If I do the above, if user gets into the website using a direct link like website.com/es/index, clicking then the website's url how can I ensure that will follow the same langauge that the user entered into the website?
  3. Is such a shame that translating django apps is made through plain files!! Is there a way that will allow translations using the admin interface? I found this but I have problems with (is out of date as well) it and there is no such information in the internet.

I am not looking someone to solve my problems but rather to put me in the right direction for the following:

  1. Make the website available to different languages to guests (clicking website urls will remember guest's language preference).
  2. If we can allow translators to make the transaltions using the admin ineterface instead of the unfriendly for translator django files?

Thank you in advance.

Upvotes: 0

Views: 502

Answers (1)

Gokhan Sari
Gokhan Sari

Reputation: 7924

You don't need any third party package to accomplish what you want to do, Django has it built-in.

  • You add django.middleware.locale.LocaleMiddleware to your middlewares,
  • configure locales in settings.py,
  • wrap your urls in urls.py with django.conf.urls.i18n.i18n_patterns,
  • translate your strings

and you are done.

You get urls like site.com/en/lorem-ipsum for the urls you wrapped with django.conf.urls.i18n.i18n_patterns.

To be more specific about your questions:

1) This is the default behaviour. If you wrap admin url with django.conf.urls.i18n.i18n_patterns, you'll get admin site in multiple languages, too. With the same url style, like site.com/en/admin/. Here is an example urls.py:

from django.conf.urls import url, include, i18n
from django.contrib import admin

urlpatterns += i18n.i18n_patterns(
    url(r'^admin/', admin.site.urls),
    url(r'^products/', include('your_project.apps.products.urls', namespace='products')),
    # ...
)

2) The all urls will be consistent locale-wise. E.g. if the user is on site.com/en/home and there is a link to the about products page the link will be `site.com/en/about'

3) I can understand, it would be a lot easier to update translations on admin site without editing gettext files and such. However, I like the way django handles translation. Making translation something about admin site would involve database and I don't think it is a good idea to use database for translation.

Upvotes: 1

Related Questions