Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用i18n对xml解析内容进行django语言翻译_Python_Django_Localization_Internationalization_Django I18n - Fatal编程技术网

Python 使用i18n对xml解析内容进行django语言翻译

Python 使用i18n对xml解析内容进行django语言翻译,python,django,localization,internationalization,django-i18n,Python,Django,Localization,Internationalization,Django I18n,这是我的设置.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.middlewa

这是我的设置.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”翻译为“محبا”

下面是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>

msgstr
应在locale\ar\LC\u MESSAGES\django.po中设置为
“محبا”

应该是-:

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

然后运行命令
python manage.py compilemessages

您可以使用添加到
django.po
文件中的
hello
hi
等的翻译

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

msgid "hi"
msgstr "<whatever>"
msgid“你好”
msgstr“محبا”
msgid“你好”
msgstr“”
并在模板中使用它们

<h2>{% trans attribs.head.value %}</h2>
{%trans attribs.head.value%}

您的msgstr在msgid中似乎是空的,它是动态的,并且不断变化。我无法在msgtr中添加一个特定的翻译字符串,例如:“محبا”。你能帮我个忙吗?你是说“你好”。但是xml中的“您好”、“您好”怎么办?这对动态内容不起作用。。你需要使用django翻译软件包来完成这项工作。你能给我推荐任何django翻译软件包用于动态内容吗?我并没有否决投票。首先,我没有否决投票的特权,因为我不熟悉stackoverflow,也不熟悉开发。@sorab这意味着翻译不起作用,可能是您的设置中的问题/config.translation可以正常工作。我添加了{%trans“Content”%},效果很好。我在很多地方使用翻译,一切正常。但不是在上面的上下文中。@sorab和
{%trans“hello”%}
attribs.head.value在forloop中,包含“hello”、“hi”、“你好吗?”,应该翻译成阿拉伯语。当然,{%trans“hello”%}是翻译的,但它是静态内容而不是动态内容。@sorab您的值中可能还有一些额外的字符,例如
\n
\t
,``等等,这就是我询问静态翻译的原因。在动态值等于某些静态值的情况下,这应该可以无缝地工作。
#: .\views.py:15
#: .\templates\index.html.py:7
#, python-format
msgid "%(header)s"
msgstr "مرحبا"
msgid "hello"
msgstr "مرحبا"

msgid "hi"
msgstr "<whatever>"
<h2>{% trans attribs.head.value %}</h2>