<div class="language-selector">
  <button class="btn" type="button" data-dropdown="langSelectorList">
    {% if language_display.button == 'default' or language_display.button == 'flag' %}
    <img alt="{{ native_name(language_selector.current)|capitalize }}" src="{{ path_flags ~ language_selector.current }}.png" />
    {% endif %}
    {% if language_display.button == 'default' or language_display.button == 'name' %}
    {{ native_name(language_selector.current)|capitalize }}
    {% endif %}
    <i class="fa fa-caret-down"></i>
  </button>

  <ul class="dropdown-menu" id="langSelectorList">
{% for language in language_selector.languages %}

    {% set show_language = true %}

    {% if uri.paths(0) == "lesson" %}
        {% set base_lang_url = base_url_simple ~ grav.language.getLanguageURLPrefix(language) %}
        {% set lang_url = base_lang_url ~ uri.path %}
    {% else %}
        {% if language == language_selector.current %}
            {% set lang_url = page.url %}
        {% else %}
            {% set base_lang_url = base_url_simple ~ grav.language.getLanguageURLPrefix(language) %}
            {% set lang_url = base_lang_url ~ language_selector.page_route ~ page.urlExtension %}
            {% set untranslated_pages_behavior = grav.config.plugins.language_selector.untranslated_pages_behavior %}
            {% if untranslated_pages_behavior != 'none' %}
                {% set translated_page = language_selector.translated_pages[language] %}
                {% if (not translated_page) or (not translated_page.published) %}
                    {% if untranslated_pages_behavior == 'redirect' %}
                        {% set lang_url = base_lang_url ~ '/' %}
                    {% elseif untranslated_pages_behavior == 'hide' %}
                        {% set show_language = false %}
                    {% endif %}
                {% endif %}
            {% endif %}
        {% endif %}
    {% endif %}

    {% if show_language %}
        <li>
          <a href="{{ lang_url ~ uri.params }}">
            {% if language_display.select == 'default' or language_display.select == 'flag' %}
            <img alt="{{ native_name(language)|capitalize }}" src="{{ path_flags ~ language }}.png" />
            {% endif %}
            {% if language_display.select == 'default' or language_display.select == 'name' %}
            {{ native_name(language)|capitalize }}
            {% endif %}
          </a>
        </li>
    {% endif %}

{% endfor %}

{% block polyglot_language_selector %}
  {% if uri.paths and not disable_multilingual %}
  <li>
    <a href="{{ '/polyglot-columns' ~ uri.path }}">
      {% if language_display.select == 'default' or language_display.select == 'flag' %}
        <img alt="Rosetta" src="/{{ grav.locator.findResource('plugin://m3-p2/flags/xx.png', false) }}" />
      {% endif %}
      {% if language_display.select == 'default' or language_display.select == 'name' %}
        Multilingual
      {% endif %}
    </a>
  </li>
  {% endif %}
{% endblock %}

  </ul>
</div>
