Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to translate plurals of a model in the Django admin?

I have a Django application with a model called Topic. I want to translate the plural of this model in the Django admin (see the red ellipse in the screenshot below).

Screenshot

In order to do this, I did following:

1) Added a Meta class to the model in models.py:

from django.utils.translation import ugettext_lazy as _

class Topic(models.Model):
    title = models.CharField(max_length=140)

    def __unicode__(self):
        return self.title
    class Meta:
        verbose_name = _('topic')
        verbose_name_plural = _('topics')

2) Ran django-admin.py makemessages -l ru-RU, which generated a file locale/ru/django.po.

3) Added translations to the django.po file:

msgid "topic"
msgstr "Тема"

msgid "topics"
msgstr "Темы"

4) Ran django-admin.py compilemessages.

5) Changed settings.py so that there are following settings there:

LANGUAGE_CODE = 'ru-RU'

ugettext = lambda s: s

LANGUAGES = (
  ('ru-RU', ugettext('Russian')),
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.locale.LocaleMiddleware',
)

USE_I18N = True

USE_L10N = True

But it still doesn't work (the marked lettering in the admin still appears in English, not in Russian).

What can I do in order to fix this?

Update 1 (28.09.2013 13:26): Maybe something is wrong with my directory structure. Here it is:

Directory structure

like image 384
Dmitrii Pisarenko Avatar asked Sep 21 '13 18:09

Dmitrii Pisarenko


2 Answers

  1. Probably locale dir does not live in your app's directory or is not defined in LOCALE_PATHS. https://docs.djangoproject.com/en/dev/topics/i18n/translation/#how-django-discovers-translations

  2. It should generate a file locale/ru-RU/LC_MESSAGES/django.po.

like image 97
Meehow Avatar answered Oct 05 '22 02:10

Meehow


I think it's a better practice to add the app translations within the app folder itself, this makes it portable and easier to find. It may also help you with your problem.

Create a "locale" folder within your app:

my-site/opinions/locale/

Standing within your app folder, run the "django-admin.py makemessages -l ru-RU" comand. It should create the django.po file with the the needed translations in blank.

After you fill the missing translations, run "django-admin.py compilemessages" and restart your server just in case.

Also, in your settings.py file, don't use hard-coded strings for your folders, it's a better practice to get them dynamically

import os
PROJECT_ROOT = os.path.realpath(os.path.dirname(__file__))
LOCALE_PATHS = (os.path.join(PROJECT_ROOT, 'locale'), )

This last code will assume that the locale folder is with the settings.py file.

like image 21
ignacio.munizaga Avatar answered Oct 05 '22 02:10

ignacio.munizaga