简体   繁体   English

在 Django 模板中显示语言图标

[英]Display language icons in Django template

I want to display language icons instead of names in Django template.我想在 Django 模板中显示语言图标而不是名称。

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.你必须将图标放在 static 文件夹中,语言名称为英语 -> 英语等。

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>引用,例如

<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.您可能想看看django-countries package。使用它,或将其用于想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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