Home > Mobile >  Display language icons in Django template
Display language icons in Django template

Time:02-02

I want to display language icons instead of names in Django template.

My code looks like this:

{% load static i18n %}

    {% get_current_language as CURRENT_LANGUAGE %}
    {% get_available_languages as AVAILABLE_LANGUAGES %}
    {% get_language_info_list for AVAILABLE_LANGUAGES as languages %}
    <div id="language">
          {% for language in languages %}
            <ul>
                <li>
                   <a href="/{{ language.code }}/"
                    {% if language.code == LANGUAGE_CODE %} {% endif %}>
                    {{ language.name|slice:":3" }}
                   </a>
                </li>
            </ul>
          {% endfor %}
    </div>

Is there any possible ways to achieve that goal or I have to try different ways?

solution for that was to change that rows to this:

           <a href="/{{ language.code }}/"
            {% if language.code == LANGUAGE_CODE %} {% endif %}>
               <img src="/static/images/{{ language.name }}.png"  alt="Geo">
           </a>

And you have to place icons in static folder with the language names for english -> English and so on.

CodePudding user response:

The harder part is assembling your set of country flag images together somewhere.

The easy part is using your language context to get an <img> reference, for example

<img ... src="somewhere/{{language.name|slice:":3"}}.gif" >

You might want to take a look at the django-countries package. Use it, or use it for ideas.

  • Related