Search code examples
cssdjangonginxinternationalizationdjango-i18n

How to serve different CSS file when user change language in Django i18n translation


I am using Django i18n translation system and there are two languages:

LANGUAGES = (
    ('fa', _('Persian')),
    ('en', _('English')),
    )

Persian is Right-To-Left and I want to serve another CSS file when user change language to persian. Or maybe there is a better way to change LTR to RTL when user change the language?

I am using nginx with wsgi on a Ubuntu vps.


Solution

  • One possible way:

    I assume you have 2 css files for example static/css/fa.css and static/css/en.css

    1. In template, when you link your css you can do it like this:

      <link type="text/css" href="{% trans 'static/css/en.css' %}">
      
    2. In your Persian translation file you put then something like this:

      msgid "static/css/en.css"
      msgstr "static/css/fa.css"
      

    Alternatively, if the only thing you want from css is to change LTR to RTL you could do in your template:

    {%load i18n%}
    {%get_current_language as LANGUAGE_CODE%}
    {%get_language_info for LANGUAGE_CODE as lang%}
    ....
    {% if lang.bidi %}
    <!-- Some HTML code for RTL -->
    {% else %}
    <!-- Some HTML code for LTR -->
    {% endif %}