0

This is my 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',
)

This is my xml file. I want to translate the header tag content i.e "hello" as "مرحبا"

<?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>

Below is the function in 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})

Below is template for 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>

I have changed the preferred-languages setting in mozilla as "Arabic/U.A.E" (under Tools->Options->Content->Languages in Firefox) .But still it displays as hi, hello, how are you. And the below is my django.po for "ar" in locale\ar\LC_MESSAGES\django.po

#: .\views.py:15
#: .\templates\index.html.py:7
#, python-format
msgid "%(header)s"
msgstr ""
psorab
  • 135
  • 1
  • 16
  • your msgstr appears to be empty – alko Nov 27 '13 at 09:34
  • header in msgid is dynamic and keeps on changing. I cannot add one specific translated string ex:"مرحبا" in msgtr. Can you help me out with that? – psorab Nov 27 '13 at 10:22

2 Answers2

0

You can add to your django.po file translations of hello, hi and so on with

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

msgid "hi"
msgstr "<whatever>"

and use them in template with

<h2>{% trans attribs.head.value %}</h2>
alko
  • 46,136
  • 12
  • 94
  • 102
  • @sorab so it means translation don't work, probably issue in your settings/config. – alko Nov 27 '13 at 10:58
  • translation works fine. I have added {% trans "Content" %}, which is working fine. And i am using translation at many places, its all working fine.But not in the above context. – psorab Nov 27 '13 at 11:15
  • @sorab and what about `{% trans "hello" %}` – alko Nov 27 '13 at 11:53
  • attribs.head.value is in forloop and contains "hello", "hi", "how are you?", which should be translated to arabic. Ofcourse {% trans "hello" %} is translated but it is static content not dynamic. – psorab Nov 27 '13 at 12:10
  • @sorab there might be some additional chars in your value, such as `\n`, `\t`, ` ` etc, that's why I ask about static translation. that should work seamlessly in case dynamic value **equals** some of statics. – alko Nov 27 '13 at 12:13
-1

msgstr should be set as "مرحبا" in locale\ar\LC_MESSAGES\django.po

It should be -:

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

And then run command python manage.py compilemessages

Nilesh
  • 2,407
  • 1
  • 12
  • 20
  • You mentioned it for "hello". But what about "hi", "how are you" from xml? – psorab Nov 27 '13 at 10:05
  • This will not work for dynamic content.. you need to use django translation packages for this.. – Nilesh Nov 27 '13 at 10:08
  • can you suggest me any django translation package for dynamic content? – psorab Nov 27 '13 at 10:16
  • I did not down vote. First of all i do not have privilege to down vote because i am new to stackoverflow and new to development. – psorab Nov 27 '13 at 11:17