How to translate plurals of a model in a 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 Red ellipse in the screenshot below).

Screen shot

To do this, I did the 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 the 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 the following settings:

 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 does not work (the marked inscription in the admin still appears in English, and not in Russian).

What can I do to fix this?

Update 1 (09/28/2013 13:26): Maybe something is wrong with my directory structure. Here he is:

Directory structure

+6
source share
3 answers
+4
source

I find it better to use application translations in the application folder itself, which makes it portable and easier to find. It can also help you solve your problem.

Create the "locale" folder in your application:

 my-site/opinions/locale/ 

In the application folder, run the django-admin.py makemessages -l ru-RU command. He should create a django.po file with the necessary translations in a space.

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

Also, in your settings.py file do not use hard-coded lines for your folders, it is better to use 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 has settings.py file.

+3
source

I tried Django 1.7.

I tried the same method, but did not work the first time, after some debugging in Django, I found some hints and found a solution, there should be some kind of document in Django for locale names, in fact there are some rules not documented.

And in my test, only messages in the project can be downloaded, all messages in the application can not be translated.

I tried print _ (") in the same index () method in my application view. Msgstr" The file in settings.py can be translated.

The code is as follows:

def index (request):

 print request.LANGUAGE_CODE str_msg = _("Welcome to poll list!") ## this keep English output, only appears in app print str_msg str_msg = _("Simplified Chinese") ## this one worked, it appears in settings.py print str_msg context = {'latest_question_list': latest_question_list, 'message': str_msg} .... 

After checking some Django code, I found that it is only allowed for locale names listed on the page

https://github.com/django/django/tree/master/django/conf/locale

Pay attention to the directory names, no 'ru-RU', no 'zh-cn'.

code in gettext.py:

 def translation(domain, localedir=None, languages=None, class_=None, fallback=False, codeset=None): if class_ is None: class_ = GNUTranslations mofiles = find(domain, localedir, languages, all=1) if not mofiles: if fallback: return NullTranslations() raise IOError(ENOENT, 'No translation file found for domain', domain) 

I fixed my own problem by changing "zh-cn" to "zh_CN", then executed the following commands:

 python manage.py makemessages -l zh_CN 

copied the django.po file from the folder '/ zh-cn / LC_MESSAGES /' to the folder '/ zh_CN / LC_MESSAGES' to save my translations.

 python manage.py compilemessages 

then

 python manage.py runserver 

Now everything is all right!

So, I think moving from ru-ru to ru might solve your problem.

0
source

Source: https://habr.com/ru/post/954324/


All Articles