简体   繁体   English

如何在Django管理员中翻译模型的复数?

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

I have a Django application with a model called Topic . 我有一个名为Topic的模型的Django应用程序。 I want to translate the plural of this model in the Django admin (see the red ellipse in the screenshot below). 我想在Django管理员中翻译这个模型的复数(参见下面屏幕截图中的红色椭圆)。

截图

In order to do this, I did following: 为了做到这一点,我做了以下事情:

1) Added a Meta class to the model in models.py : 1)在models.py中为模型添加了一个Meta类:

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 . 2)Ran django-admin.py makemessages -l ru-RU ,它生成一个文件locale/ru/django.po

3) Added translations to the django.po file: 3)添加了对django.po文件的翻译:

msgid "topic"
msgstr "Тема"

msgid "topics"
msgstr "Темы"

4) Ran django-admin.py compilemessages . 4)Ran django-admin.py compilemessages

5) Changed settings.py so that there are following settings there: 5)更改了settings.py以便在那里进行以下设置:

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. 更新1(2013年9月28日13:26):我的目录结构可能有问题。 Here it is: 这里是:

目录结构

  1. Probably locale dir does not live in your app's directory or is not defined in LOCALE_PATHS . 可能locale dir不在您的应用程序目录中,或者未在LOCALE_PATHS定义。 https://docs.djangoproject.com/en/dev/topics/i18n/translation/#how-django-discovers-translations 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 . 它应该生成文件locale/ru-RU/LC_MESSAGES/django.po

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. 我认为在app文件夹中添加应用程序翻译是一种更好的做法,这使得它便于携带并且更容易找到。 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. 站在您的app文件夹中,运行“django-admin.py makemessages -l ru-RU”命令。 It should create the django.po file with the the needed translations in blank. 它应该创建django.po文件,其中所需的翻译为空白。

After you fill the missing translations, run "django-admin.py compilemessages" and restart your server just in case. 填写缺失的翻译后,运行“django-admin.py compilemessages”并重新启动服务器以防万一。

Also, in your settings.py file, don't use hard-coded strings for your folders, it's a better practice to get them dynamically 此外,在您的settings.py文件中,不要对文件夹使用硬编码字符串,更好的做法是动态获取它们

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. 最后一个代码将假定locale文件夹与settings.py文件一起使用。

I tried Django 1.7. 我试过Django 1.7。

I tried the same method, but didn't work at the first time, after some debugging into Django, I found some hints and found the solution, there should be some document in Django for locale names, actually there are some rules not documented. 我尝试了相同的方法,但是第一次没有工作,在对Django进行一些调试之后,我发现了一些提示并找到了解决方案,Django中应该有一些文档用于语言环境名称,实际上有一些规则没有记录。

And in my test, only messages under project can be loaded, all messages in app cannot be translated. 在我的测试中,只能加载项目下的消息,app中的所有消息都无法翻译。

I tried print _("") in the same index() method in my app's view. 我在应用程序视图中的同一index()方法中尝试了print _(“”)。 only msgid in settings.py file can be translated. 只有在settings.py文件中的msgid才能被翻译。

The code looks like below: 代码如下所示:

def index(request): def索引(请求):

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 checked the some Django code, I found it allowed only the locale names given under page 检查了一些Django代码后,我发现它只允许在页面下给出的语言环境名称

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

Please note the directory names, there is no 'ru-RU', no 'zh-cn'. 请注意目录名称,没有'ru-RU',没有'zh-cn'。

code in gettext.py: 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 ran the following commands: 我通过将'zh-cn'更改为'zh_CN'来修复我自己的问题,然后运行以下命令:

python manage.py makemessages -l zh_CN 

copied django.po file from folder '/zh-cn/LC_MESSAGES/' to folder '/zh_CN/LC_MESSAGES', to keep my translations. 将文件夹'/ zh-cn / LC_MESSAGES /'中的django.po文件复制到文件夹'/ zh_CN / LC_MESSAGES',以保留我的翻译。

python manage.py compilemessages

then, 然后,

python manage.py runserver

Now everything is OK! 现在一切都好!

So, I think to change from 'ru-RU' to 'ru' may solve your problem. 所以,我认为从'ru-RU'改为'ru'可以解决你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM