Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django internationalization for admin pages - translate model name and attributes

Django's internationalization is very nice (gettext based, LocaleMiddleware), but what is the proper way to translate the model name and the attributes for admin pages? I did not find anything about this in the documentation:

  • http://docs.djangoproject.com/en/dev/topics/i18n/internationalization/
  • http://www.djangobook.com/en/2.0/chapter19/

I would like to have "Выберите заказ для изменения" instead of "Выберите order для изменения". Note, the 'order' is not translated.

First, I defined a model, activated USE_I18N = True in settings.py, run django-admin makemessages -l ru. No entries are created by default for model names and attributes.

Grepping in the Django source code I found:

$ ack "Select %s to change"
contrib/admin/views/main.py
70:        self.title = (self.is_popup and ugettext('Select %s') % force_unicode(self.opts.verbose_name) or ugettext('Select %s to change') % force_unicode(self.opts.verbose_name))

So the verbose_name meta property seems to play some role here. Tried to use it:

class Order(models.Model):
    subject = models.CharField(max_length=150)
    description = models.TextField()
    class Meta:
        verbose_name = _('order')

Now the updated po file contains msgid 'order' that can be translated. So I put the translation in. Unfortunately running the admin pages show the same mix of "Выберите order для изменения".

I'm currently using Django 1.1.1. Could somebody point me to the relevant documentation? Because google can not. ;-) In the mean time I'll dig deeper into the django source code...

like image 280
geekQ Avatar asked May 30 '10 12:05

geekQ


People also ask

How can I change Django admin language?

In your settings.py just add 'django. middleware. locale. LocaleMiddleware' to your MIDDLEWARE_CLASSES setting, making sure it appears after 'django.

What is Gettext_lazy Django?

ugettext is a unicode version of a translatable string. ugettext_lazy is a "lazy" version of that. Lazy strings are a Django-ism; they are string-like objects that don't actually turn into the real string until the last possible minute. Often, you can't know how to translate a string until late in the process.


2 Answers

Important things not mentioned in the Django documentation:

  • run django-admin compilemessages, e.g. as a part of your build process. Thanks stevejalim!
  • apply django's ugettext_lazy() to model names ( Meta class and verbose_name )
  • attribute (model field verbose_name) names can also be translated with ugettext_lazy()
  • use lazy translation in your model metadata, otherwise the translation happens while loading the model classes and the settings of your users, especially the browser settings, will not be taken into account
  • I use some scoping for attribute names, e.g. separating the model name and attribute names with a pipe. The same convention is used in ruby-gettext. Background: attribute names like 'title' or 'name' translated differently in the most languages depending on context. Example 'Book|title' -> 'Titel' or 'Buchtitel' in German. But 'Chapter|title' would be translated as 'Überschrift'.

Example using above principles:

from django.utils.translation import ugettext_lazy as _ class Order(models.Model):     subject = models.CharField(max_length=150, verbose_name = _('Order|subject'))     description = models.TextField(            verbose_name = _('Order|description'))     class Meta:         verbose_name = _('order')         verbose_name_plural = _('orders') 

Or is there a better way to translate the model and admin pages?

Either way we should enhance the Django documentation and fill the gap!

like image 67
geekQ Avatar answered Sep 23 '22 13:09

geekQ


See https://automationpanda.com/2018/04/21/django-admin-translations/ It's author made an excellent work in showing how to master all django translation features step by step. It's much better than oficial documentation to me.

like image 31
Alvaro Rodriguez Scelza Avatar answered Sep 22 '22 13:09

Alvaro Rodriguez Scelza