8

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
friedemann_bach
  • 1,418
  • 14
  • 29
user1261774
  • 3,525
  • 10
  • 55
  • 103
  • Did you tried https://pypi.python.org/pypi/django-right-to-left/0.1.1? – K K May 16 '14 at 17:29
  • I haven't tried django-right-to-left because the problem would still occur due to the mixture of rtl, ltr and neutral text. the bracket is considered a neutral text field. – user1261774 May 17 '14 at 01:11
  • Have you tried embedding the Unicode control characters directly into the source (in `LANGUAGES`)? I would think that adding those control characters to views.py doesn't make sense since you don't know whether it'll be a RTL or LTR language ahead of time. – user193130 May 21 '14 at 10:51
  • Thanks user193130 - I have tried that approach, but the Unicode control characters are rendered to the template page as text, that is the the Unicode control characters are displayed to the user! This problem is really frustrating! – user1261774 May 21 '14 at 22:04

3 Answers3

3

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)...

cchristelis
  • 1,985
  • 1
  • 13
  • 17
  • 1
    cchristelis thank you. I did know that adding text before/after the brackets did solve the issue, but then that text would be displayed in the select list. What I did not know was that adding **\u200E** solved the BiDi brackets issues and is **not** displayed to the user in the select list. The **\u200E** left-to-right mark (LRM) is a control character or invisible formatting character. Also, to any other users trying to solve this problem, just adding the \u200E to all the brackets seems to works just as well as only adding the \u200E to the rtl languages. Again, thanks. – user1261774 May 22 '14 at 09:03
1

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

django-admin.py compilemessages
Jeff Sheffield
  • 5,768
  • 3
  • 25
  • 32
0

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>
josef
  • 872
  • 9
  • 8