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
4

3 回答 3

3

在您的阿拉伯语文本示例中,文本的主要方向已设置为从右到左。如果括号在从右到左和从左到右的文本之间,则它采用流行语言的方向。因此,在您的示例中,如果您将文本更改为括号后有一个单词,问题就会消失。我建议您使用添加的单词对其进行测试以了解解决方案。

这很麻烦,对于您的生产代码,我会建议另一种解决方案。假设您知道括号仅出现在您的从左到右的语言中,您需要使用 unicode 控制字符终止括号(尝试手动将其添加到您的测试中)。所以我在这种情况下的解决方案只是用括号和控制字符替换括号:

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)

显然,如果您在从右到左的语言中有括号,则必须首先测试 use_language_code 的方向,然后仅在适当的情况下更新母语(rtl 中的 ltr,反之亦然)...

于 2014-05-22T08:05:51.270 回答
1

我想知道它是否对您的阿拉伯语转换不利。您是否尝试过重新编译您的消息?

django-admin.py compilemessages
于 2014-05-16T17:22:44.517 回答
0

解决双向括号问题:您只需在最后一个括号后添加 LRM 字符。HTML 实体:‎

<html dir="rtl">
<body>
<p>hello (world)&#x200E;</p>
</body></html>
于 2014-09-02T12:35:35.510 回答