Question

I am running python2.7 with django 1.4.

I have the following code in my views.py page which returns the language names in a select list:

python code: (views.py)

available_languages = []
for lv in language_versions:
    language = LANGUAGES[lv.language_code]
    if lv.language_code == user_language_code:
        language_label = (lv.language_code, u"%s" % (language.name_native))
    else:
        language_label = (lv.language_code, u"%s / %s" % (language.name, language.name_native))
    available_languages.append(language_label)
....
available_languages = sorted(available_languages, key=lambda x: x[1], cmp=compare)

return available_languages

HTML code:

{{ field }}

select list:

German / Deutsch
Polish / polski
Portuguese (Brasil) / português (Brasil)
Spanish (Latin America) / español (Latinoamérica)
Russian / pусский

The problem is when the page is viewed in a rtl language like Arabic the select list is rendered as follows with the brackets of the ltr text in the wrong positions:

لألمانية / Deutsch
البولندية / polski
البرتغالية) لبرتغال) / português) Portugal)
لإسبانية أمريكا) اللاتينية) / español) Latinoamérica)
الروسية / pусский

How do I write the code in the view.py file to correctly display the bidi brackets?

I have read quite a few threads and tried many different ways to fix the issue, but I have run out of ideas.

One attempt that did not work was bookending the language names with the Unicode control (directional) characters & #8234; & #8236; & rlm; in the view.py file - the & #8234; & #8236; & rlm; is rendered to the select list in the page. For example:

& #8234;إسبانية أمريكا) اللاتينية) / português (Brasil)& #8236; & rlm;

Also adding the following to the css has no effect:

direction: rtl; unicode-bidi: embed
Was it helpful?

Solution

In your arabic text example, the prevailing direction of the text has been set to right-to-left. A bracket takes the direction of the prevailing language if it is between right-to-left and left-to-right text. So in your example if you would change the text to have a word after the brackets the problem would go away. I would suggest you test it with added words to understand the solution.

This is cumbersome and for your production code, I would suggest another solution. Assuming you know that the brackets only occur in your left to right languages, you need to terminate the bracket with a unicode control character ‎ (Try adding this to your test manually). So my solution in this case is just replace the brackets, with brackets and control characters:

for lv in language_versions:
    language = LANGUAGES[lv.language_code]
    if lv.language_code == user_language_code:
        language_label = (lv.language_code, u"%s" % (language.name_native))
    else:
        native = language.name_native.replace(u')', u')\u200E') # I haven't tested this and you might have to add the html character code ‎ here. please edit this if you find that to be the case.
        language_label = (lv.language_code, u"%s / %s" % (language.name, native))
    available_languages.append(language_label)

Obviously if you have brackets in your right-to-left languages, you'll have to first test what the use_language_code's direction, and then only update the native language, if appropriate (ltr in rtl and vice versa)...

OTHER TIPS

I wonder if its bad data for your Arabic conversion. Have you tried recompiling your messages?

django-admin.py compilemessages

to solve BiDi bracket problem : You just need to add the LRM character after the last bracket. HTML entity: ‎

<html dir="rtl">
<body>
<p>hello (world)&#x200E;</p>
</body></html>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top