0

i'm trying to show languages label list in my template , but it only shows the original name of the language my settings.py

LANGUAGES = (      
   ('en', _('english uk')),
   ('ar', _('arabic uae')),
   ('fa', _('Persian')),

)

my template

        <li class="nav-item dropdown">
          <a class="nav-link" data-toggle="dropdown" href="#">
            <i class="fas fa-globe"></i>
          </a>
          <div class="text-center dropdown-menu dropdown-menu-lg dropdown-menu-right">
            {% get_current_language as LANGUAGE_CODE %}
            {% get_available_languages as LANGUAGES %}
            {% get_language_info_list for LANGUAGES  as languages %}
            {% for lang in languages %}
                <a class="dropdown-item" href="/{{lang.code}}/"> {{lang.name_local}}</a>
            {% endfor %}    
                      
          </div>
        </li> 
i need to show _('english uk'), values instead of the original name is it possible please ? thank you for helping ..
artiest
  • 554
  • 5
  • 20
  • 1
    I think it better you keep the value as a string in a separate list. Ex: ***`('en', "_('english uk')")`*** – JPG Aug 11 '21 at 15:25
  • and the how to access to that tuple value – artiest Aug 11 '21 at 15:28
  • 1
    may be [this](https://stackoverflow.com/questions/271077/django-how-to-do-tuple-unpacking-in-a-template-for-loop) – JPG Aug 11 '21 at 15:29
  • it doesnt return the provided values – artiest Aug 11 '21 at 15:38
  • Can you add your view and/or models related to this template? What is it that you are trying to do? Just show your possible languages in the template or is it a form where you select an option? – gdef_ Aug 11 '21 at 15:43
  • @JPG thank you i've fixed by accessing tuples from template – artiest Aug 11 '21 at 15:49

3 Answers3

1

for those who looks for iterate through tuples from template

<div class="text-center dropdown-menu dropdown-menu-lg dropdown-menu-right">
     {% get_current_language as LANGUAGE_CODE %}
     {% get_available_languages as LANGUAGES %}
     {% for lang in LANGUAGES %}
          <a class="dropdown-item" href="/{{lang.0}}/"> {{lang.1}}</a>
      {% endfor %}    
</div>
artiest
  • 554
  • 5
  • 20
0

You can try lang.name and lang.name_translated. Those may be the answer for you, the first shows the language name in english and the second in the currently active language.

0

I use the code bellow in django 3, 4 and python 3.6+, it works great.

{% load i18n %}

<form action="{% url 'set_language' %}" method="post">
    {% csrf_token %}
    <div class="" style="background-color: #fff; padding: 4px; border-radius:2px;">
        <select name="language" style="border-width: 0px;">
        {% get_current_language as LANGUAGE_CODE %}
        {% get_available_languages as LANGUAGES %}
        {% get_language_info_list for LANGUAGES as languages %}
        {% for language in languages %}
        <option value="{{ language.code }}"{% if language.code == LANGUAGE_CODE %} selected{% endif %}>
                {{ language.name_local }}
        </option>
        {% endfor %}
        </select>
        <input type="submit" value="{% trans "Go" %}" style="border:none; background-color: #198754; color:#fff; font-weight:bold;">
    </div>
</form>