现在我得到了问题的解决方案,我一一解释。
因为我想翻译我的model names
, model field names
, 模块中的其他一些texts
可用的和static/dynamic texts
我的 Django 模板中存在的一些。
假设,我有models.py
andindex.html
如下。
模型.py
from django.db import models
class Student(models.Model):
first_name = models.CharField(max_length=100, null=True, blank=True, help_text="First name of student", verbose_name="First name")
last_name = models.CharField(max_length=100, null=True, blank=True, help_text="Last name of student" , verbose_name="Last name")
age = models.IntegerField(help_text="Age" , verbose_name="Age")
def __str__(self):
return "Student " + str(self.id) + " - " + self.first_name + " " + self.last_name + created_by_user % (self.userid.username)
class Meta:
verbose_name = "Student"
verbose_name_plural = "Students"
索引.html
{% extends "base.html" %}
<h1> HOME PAGE </h1>
<h2> Django is nice </h2>
现在,我需要将翻译文本放在我的modules
和templates
. 我希望我的 Django 应用程序支持Arabic
& English
。
- 替换和的代码
models.py
如下index.html
。
模型.py
from django.db import models
from django.utils.translation import gettext_lazy as _
class Student(models.Model):
first_name = models.CharField(max_length=100, null=True, blank=True, help_text = _("First name of student") , verbose_name =_("First name"))
last_name = models.CharField(max_length=100, null=True, blank=True, help_text = _("Last name of student") , verbose_name =_("Last name"))
age = models.IntegerField(help_text = _("Age") , verbose_name =_("Age"))
def __str__(self):
return "Student " + str(self.id) + " - " + self.first_name + " " + self.last_name + created_by_user % (self.userid.username)
class Meta:
verbose_name = _("Student")
verbose_name_plural = _("Students")
索引.html
{% extends "base.html" %}
{% load i18n %}
<h1> {% trans "HOME PAGE" %} </h1>
<h2> {% trans "Django is nice" %} </h2>
在 settings.py 中,添加以下内容(在我的情况下,我在其中创建了名为的文件夹locale
,BASE_DIR
您可以选择另一个位置并在 中指定位置settings.py
)。
from django.utils.translation import gettext_lazy as _
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'locale'),
)
LANGUAGES = (
('ar', _('Arabic')),
('en', _('English')),
)
MULTILINGUAL_LANGUAGES = (
"en-us",
"ar-ae",
)
根据要求从 2 个位置中的任何一个运行makemessages
命令,因为该命令通过运行它的所有子目录查找翻译文本。
一种。项目所在的根目录manage.py
。
湾。,所在的应用程序的根目录。models.py
views.py
django-admin.py makemessages -l ar
点击这里查看语言代码
上面的命令将创建一个名为ar
inside locale
directory 的目录,其文件夹结构如下。
.
└── ar
└── LC_MESSAGES
└── django.po
就我而言,django.po
包含以下几行。
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-12-24 12:34+0530\n"
"PO-Revision-Date: 2017-12-24 11:56+0400\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=6; plural=n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 "
"&& n%100<=10 ? 3 : n%100>=11 && n%100<=99 ? 4 : 5;\n"
"Last-Translator: \n"
"Language-Team: \n"
"X-Generator: Poedit 1.8.11\n"
#: student_activity_reporter_app/models.py:169
msgid "First name of student"
msgstr ""
#: student_activity_reporter_app/models.py:169
msgid "First name"
msgstr ""
#: student_activity_reporter_app/models.py:170
msgid "Last name of student"
msgstr ""
#: student_activity_reporter_app/models.py:170
msgid "Last name"
msgstr ""
#: student_activity_reporter_app/models.py:172
msgid "Age"
msgstr ""
#: student_activity_reporter_app/templates/student_activity_reporter_app/index.html:93
msgid "HOME"
msgstr ""
...
...
现在,将 的值填入如下所示msgstr
的英语字符串的阿拉伯语翻译。msgid
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-12-24 12:34+0530\n"
"PO-Revision-Date: 2017-12-24 11:56+0400\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=6; plural=n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 "
"&& n%100<=10 ? 3 : n%100>=11 && n%100<=99 ? 4 : 5;\n"
"Last-Translator: \n"
"Language-Team: \n"
"X-Generator: Poedit 1.8.11\n"
#: student_activity_reporter_app/models.py:169
msgid "First name of student"
msgstr "الاسم الاول للمتهم"
#: student_activity_reporter_app/models.py:169
msgid "First name"
msgstr "الاسم الاول"
#: student_activity_reporter_app/models.py:170
msgid "Last name of student"
msgstr "اسم المتهم الأخير"
#: student_activity_reporter_app/models.py:170
msgid "Last name"
msgstr "اسم الأخير"
#: student_activity_reporter_app/models.py:172
msgid "Age"
msgstr "العمر"
#: student_activity_reporter_app/templates/student_activity_reporter_app/index.html:93
msgid "HOME"
msgstr "الصفحه الرئيسيه"
现在compilemessages
从上面提到的同一目录运行,它将生成django.mo
文件。
django-admin.py compilemessages
目录的目录结构loacle
将如下所示。
.
└── ar
└── LC_MESSAGES
├── django.mo
└── django.po
使用启动开发服务器
python manage.py runserver
访问http://127.0.0.1:8000/en/admin/以查看/访问English
基于的管理站点。您将看到如下所示的登录页面。
访问http://127.0.0.1:8000/ar/admin/ 以查看/访问Arabic
基于的管理站点。现在,您将看到如下所示的登录页面。
添加尽可能多的翻译文本modules
,templates
在 中进行适当的配置settings.py
,从适当的位置执行命令,选择适当的语言代码并multilingual support
在您的Django powered
网站中享受。
参考:
如何使用 django-admin.py makemessages --all
https://docs.djangoproject.com/en/2.0/topics/i18n/translation/
本地化:如何创建语言文件