如何覆盖django管理员翻译? [英] How to override the django admin translation?

查看:120
本文介绍了如何覆盖django管理员翻译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试覆盖Django管理网站的默认翻译.

I'm trying to override the default translations of Django's admin site.

我正在使用Django 1.6.我的settings.py包含:

I'm using Django 1.6. My settings.py contains:

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

# ...

LANGUAGE_CODE = 'nl'
USE_I18N = True
USE_L10N = True
LOCALE_PATHS = (os.path.join(BASE_DIR, "locale"),)

我已将文件django/contrib/admin/locale/nl/LC_MESSAGES/django.po复制到my_project/locale/nl/LC_MESSAGES/django.po,并对其进行了一些更改.

I have copied the file django/contrib/admin/locale/nl/LC_MESSAGES/django.po to my_project/locale/nl/LC_MESSAGES/django.po and I've made some changes to it.

接下来,我已经运行python manage.py compilemessagespython manage.py runserver.

Next, I have run python manage.py compilemessages and python manage.py runserver.

但是,当我访问localhost:8000/admin时,仍然看到Django的默认管理员翻译.我在做什么错了?

When I visit localhost:8000/admin, however, I'm still seeing Django's default admin translations. What am I doing wrong?

编辑-我发现了问题:

以上描述是覆盖应用翻译的正确方法.我按照我自己的指示进行操作,它们起作用了.我遇到问题的原因是,我第一次不小心忽略了nl子目录.我是一个愚蠢的人.

The above description is the correct way to override app translations. I followed my own instructions and they work. The reason for my problem was that I accidentally omitted the nl subdirectory the first time. I am a dumb person.

推荐答案

我正在提供一个答案,尽管@hedgie大多回答了他们自己的问题.我将添加一些上下文和对正在发生的事情的描述.从Django 3.0开始,此答案仍然适用.

I'm providing an answer, even though @hedgie mostly answered their own question. I'll add a bit of context and description of what's happening. This answer is still applicable as of Django 3.0.

就像您可以通过在我们自己的项目中复制模板的名称和目录结构来覆盖Django提供的管理模板一样,您可以通过在我们的项目中复制.po文件的名称和目录结构来覆盖Django提供的管理翻译.

Just as you can override a Django-provided admin template by duplicating the template's name and directory structure within our own project, you can override Django-provided admin translations by duplicating a .po file's name and directory structure within our project.

Django的管理员翻译存在于django/contrib/admin/locale/中,并按语言在名为[language code]/LC_MESSAGES/的目录中进行组织.这些单独的语言目录包含两个.po文件django.podjangojs.po,以及它们各自的已编译.mo文件.您将覆盖.po文件,并编译我们自己的.mo文件.

Django's admin translations live in django/contrib/admin/locale/ and are organized by language in directories named [language code]/LC_MESSAGES/. These individual language directories contain two .po files, django.po and djangojs.po, and their respective compiled .mo files. You will be overriding the .po files, and compiling our own .mo files.

您要做的第一件事是在设置中启用翻译,并告诉Django您在哪里存储我们的翻译文件.

The first thing you have to do is enable translations in settings, and tell Django where you store our translation files.

settings.py

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

# ...

LANGUAGE_CODE = 'nl-NL'
USE_I18N = True
USE_L10N = True
LOCALE_PATHS = (os.path.join(BASE_DIR, "locale"),)  # our custom translations will go here

请注意,尽管Django使用的目录为nl,但荷兰语中荷兰语的完整语言标识符为nl-NL.您可以在此处中找到按国家/地区组织的标识符的完整列表.

Note that although the directory Django uses is nl, the full language identifier for Dutch in the Netherlands is nl-NL. You can find a full list of identifiers organized by country here.

接下来,您将模仿Django的目录结构,并在新目录中创建两个文件:

Next, you'll mimic Django's directory structure and create two files in a new directory:

my_project/locale/nl/LC_MESSAGES/django.po

my_project/locale/nl/LC_MESSAGES/djangojs.po

请注意,此路径还必须与您在settings.py中提供的路径匹配.

Note that this path must also match what you provided in settings.py.

复制并粘贴 Django的内容翻译文件.现在,您可以为所需的任何字符串编辑翻译.例如:

Copy and paste the contents of Django's translation files. You can now edit the translations for whichever strings you like. For example:

django.po

msgid "Are you sure?"
--- msgstr "Weet u het zeker?"
+++ msgstr "Weet u het zeker?!"

现在,您需要使用以下命令编译消息:

Now you need to compile the messages with:

python manage.py compilemessages

此命令将您的.po文件编译为.mo文件,Django将使用这些文件转换任何匹配的gettext调用.现在,您应该在管理界面中看到您的自定义翻译.

This command compiles your .po files into .mo files, which Django will use to translate any matching gettext calls. You should now see your custom translations in the admin interface.

这篇关于如何覆盖django管理员翻译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆