简体   繁体   中英

Display language icons in Django template

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 %} class="active"{% 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 %} class="active"{% 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM