1

这是我的 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 中的首选语言设置更改为“阿拉伯语/阿联酋”(在 Firefox 中的工具->选项->内容->语言下)。但它仍然显示为你好,你好,你好吗。以下是我在 locale\ar\LC_MESSAGES\django.po 中的“ar”的 django.po

#: .\views.py:15
#: .\templates\index.html.py:7
#, python-format
msgid "%(header)s"
msgstr ""
4

2 回答 2

0

您可以将 等的翻译添加到您的django.po文件hellohi

msgid "hello"
msgstr "مرحبا"

msgid "hi"
msgstr "<whatever>"

并在模板中使用它们

<h2>{% trans attribs.head.value %}</h2>
于 2013-11-27T10:35:35.167 回答
-1

msgstr应设置为"مرحبا" locale \ar\LC_MESSAGES\django.po

它应该是 -:

#: .\views.py:15
#: .\templates\index.html.py:7
#, python-format
msgid "%(header)s"
msgstr "مرحبا"

然后运行命令python manage.py compilemessages

于 2013-11-27T09:40:21.230 回答