这是我的settings.py
LANGUAGE_CODE = 'en-us'
USE_I18N = True
USE_L10N = True
ugettext = lambda s: s
LANGUAGES = (
('ar', ugettext('Arabic (U.A.E.)')),
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
这是我的xml文件。我想翻译标题标记内容,即“hello”为“مرحبا”
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<node id="1">
<header>hello</header>
</node>
<node id="2">
<header>hi</header>
</node>
<node id="3">
<header>how are you?</header>
</node>
</xml>
以下是views.py
中的功能from django.utils.translation import ugettext as _
from django.shortcuts import render_to_response
import xml.etree.cElementTree as etree
def header_display(request):
xml_dictionary = {}
xml_dictionary ['xml'] = {}
preso = etree.parse(file_path)
root = preso.getroot()
nodes = root.findall('node')
for node in nodes:
node_id = int(node.attrib["id"])
xml_dictionary['xml'][node_id] = {}
head_tag= node.find('header')
header = head_tag.text
head_val=_('%(header)s')% {'header': header}
xml_dictionary['xml'][node_id]['head']={}
xml_dictionary['xml'][node_id]['head']['value']=head_val
return render(request, 'index.html',{'xml':xml_dictionary})
下面是index.html的模板
<html>
{% load i18n %}
<title></title>
<body>
{% for key,value in xml.items %}
{% for id,attribs in value.items %}
{% if attribs.head.value %}
<h2>{% blocktrans with header=attribs.head.value %}{{ header }}{% endblocktrans %}</h2>
{% endif %}
{% endfor %}
{% endfor %}
</body>
</html>
我已将mozilla中的首选语言设置更改为“阿拉伯语/阿联酋”(在工具 - &gt;选项 - &gt;内容 - > Firefox中的语言)。但仍显示为hi,你好,你好吗? 。以下是我的django.po for locale中的“ar”\ ar \ LC_MESSAGES \ django.po
#: .\views.py:15
#: .\templates\index.html.py:7
#, python-format
msgid "%(header)s"
msgstr ""
答案 0 :(得分:0)
您可以使用
添加django.po
,hello
等hi
文件翻译件
msgid "hello"
msgstr "مرحبا"
msgid "hi"
msgstr "<whatever>"
并使用
在模板中使用它们<h2>{% trans attribs.head.value %}</h2>
答案 1 :(得分:-1)
msgstr
应设置为区域设置\ ar \ LC_MESSAGES \ django.po
"مرحبا"
应该是 - :
#: .\views.py:15
#: .\templates\index.html.py:7
#, python-format
msgid "%(header)s"
msgstr "مرحبا"
然后运行命令python manage.py compilemessages