Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django right-to-left language with LANGUAGE_BIDI does not work

I am building a multi-language site with one of the languages "farsi": Everything worked fine so far, but the right to left language "farsi/persian" is not aligned right, when beginning a next line of text. That means the next line is not aligned at the right as usual for right-to-left languages. The translation work.

settings.py

gettext = lambda s: s

#default language should be german
LANGUAGE_CODE = 'de'
#LANGUAGE_CODE = 'en'
#LANGUAGE_CODE = 'fa'


LANGUAGES = (
    #('fr', gettext('French')),
    ('de', gettext('German')),
    ('en', gettext('English')),
    ('fa', gettext('Farsi')),
    #('pt-br', gettext("Brazil")),
)

language_chooser.html

{% load localeurl_tags %}
{% load i18n %}
{% load tabs %} 


{% for lang in LANGUAGES %}
        {% ifequal lang.0 LANGUAGE_CODE %}
           <li class="active"><a>{{ lang.1 }}</a></li>
        {% else %}

<!--
        {% if LANGUAGE_BIDI %}
          <li>The current language is bidirectional</li>
        {% else %}
          <li>The current language is <b>not</b> bidirectional</li>
        {% endif %}
-->
           <li class="{% ifactivetab "en" %}active{% else %}inactive{% endifactivetab %}"><a href="{{ request.path|chlocale:lang.0 }}" accesskey="2">{{ lang.1 }}</a></li>
        {% endifequal %}
{% endfor %}

in the base.html I also load:

{% load i18n %}
{% get_current_language as LANGUAGE_CODE %}
{% get_available_languages as LANGUAGES %}
{% get_current_language_bidi as LANGUAGE_BIDI %}

My django.po file for "farsi/persian" language looks like: alt text

How can I manage this?

Solution: After defining a new css class "article_right_aligned_language" with the attribute "text-align:right; direction:rtl" and modifying my base template as follows, it works now !!

      <div {% if LANGUAGE_BIDI %} class="article_right_aligned_language" {% else %} class="article" {% endif %}>
        {% block site_wrapper %}{% endblock %}
      </div>
like image 501
saeed Avatar asked Jan 08 '11 01:01

saeed


People also ask

How to make a Django project translateable?

In order to make a Django project translatable, you have to add a minimal number of hooks to your Python code and templates. These hooks are called translation strings. They tell Django: “This text should be translated into the end user’s language, if a translation for this text is available in that language.”

What is the Django translation mechanisms?

The Django translation mechanisms can be used to translate arbitrary texts to any language that is supported by Django (as long as an appropriate translation catalog exists, of course).

What are translation hooks in Django?

These hooks are called translation strings. They tell Django: “This text should be translated into the end user’s language, if a translation for this text is available in that language.” It’s your responsibility to mark translatable strings; the system can only translate strings it knows about.

What is the Standard lang format in Django?

According django-i18n-docs "es-mx" and "es-ar" are standard lang format. Am I missing something here? thanks in advace. Show activity on this post.


2 Answers

Text alignment is handled by CSS not Django. Set the text-align property on the container element:

.container.right-aligned-language {
    text-align: right;
}

Then you can apply the class right-aligned-language to your container (or body tag for that matter) with a conditional statement in your template.

like image 103
Marcus Whybrow Avatar answered Oct 13 '22 19:10

Marcus Whybrow


nowadays you should use in CSS:

direction: rtl

http://www.w3schools.com/cssref/pr_text_direction.asp

like image 24
andilabs Avatar answered Oct 13 '22 19:10

andilabs