现在我得到了问题的答案,我正在逐一解释。
因为我想翻译我的
model names
,
model field names
,其他一些
texts
提供模块和一些
static/dynamic texts
出现在我的Django模板中。
假设我有
models.py
和
index.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
.
-
替换的代码
模型。py公司
和
指数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>
-
在设置中。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",
)
-
运行
makemessages
当命令通过运行它的所有子目录查找翻译文本时,根据需要从两个位置中的任何一个位置发出命令。
一
项目的根目录
哪里
manage.py
居住。
b
应用程序的根目录
哪里
模型。py公司
,
views.py
居住。
django-admin.py makemessages -l ar
Click here to check the language codes
-
上述命令将创建一个名为
ar
在…内
场所
具有以下文件夹结构的目录。
.
âââ 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/
查看/访问
英语
基于管理站点。您将看到如下登录页面。
参观
http://127.0.0.1:8000/ar/admin/
查看/访问
阿拉伯语
基于管理站点。现在,您将看到如下登录页面。
将尽可能多的翻译文本添加到
模块
和
模板
,在中进行正确配置
设置。py公司
,从适当的位置执行命令,选择适当的语言代码,并享受
multilingual support
在您的
Django powered
网站
参考文献:
how to use django-admin.py makemessages --all
https://docs.djangoproject.com/en/2.0/topics/i18n/translation/
Localization: How to Create Language Files