<div class="language-selector">
  <button class="btn" type="button" data-dropdown="language-selector-dropdown">
    {% 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="language-selector-dropdown">
{% for language in language_selector.languages %}

    {% set show_language = true %}
    {% 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 %}

    {% 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 %}
  </ul>
</div>
