Skip to content

Instantly share code, notes, and snippets.

@ow
Created June 9, 2016 05:43
Show Gist options
  • Save ow/79479553dfc0e04f16ee6960103eff22 to your computer and use it in GitHub Desktop.
Save ow/79479553dfc0e04f16ee6960103eff22 to your computer and use it in GitHub Desktop.
<li class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{ craft.i18n.getLocaleById(craft.locale).name }} <span class="caret"></span>
</button>
{# Configure and loop through selected site locales #}
{% set locales = ['en', 'da'] %}
{% for locale in locales %}
{% if loop.first %}<ul class="dropdown-menu lggnav">{% endif %}
{% set current = false %}
{# Check if locale equals the requested page locale #}
{% if locale == craft.locale %}
{% set current = true %}
{% endif %}
{# Is this an entry page? #}
{% if entry is defined %}
{# Find the current entry in the other locale #}
{% set localeEntry = craft.entries.id(entry.id).locale(locale).first() %}
{# Make sure that it's actually localized to this locale #}
{% if localeEntry and localeEntry.locale == locale %}
{# Output a link to it #}
{% if locale != craft.locale %}
<li class="nav-link{{ current ? ' current'}}"><a href="{{ localeEntry.getUrl() }}" class="nav-link{{ current ? ' current'}}">{{ craft.i18n.getLocaleById(locale).name }}</a></li>
{% endif %}
{% else %}
{# Output a link to the homepage #}
{% if locale != craft.locale %}
<li class="nav-link{{ current ? ' current'}}"><a href="{{ craft.config.siteUrl[locale] }}" class="nav-link{{ current ? ' current'}}">{{ craft.i18n.getLocaleById(locale).name }}</a></li>
{% endif %}
{% endif %}
{# Not an entry page #}
{% else %}
{# Output the same path with the locale's base URL (`siteUrl`) #}
{% if locale != craft.locale %}
<li class="nav-link{{ current ? ' current'}}"><a href="{{ craft.config.siteUrl[locale] ~ craft.request.getPath() }}" class="nav-link{{ current ? ' current'}}">{{ craft.i18n.getLocaleById(locale).name }}</a></li>
{% endif %}
{% endif %}
{% if loop.last %}</ul>{% endif %}
{% endfor %}
</li>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment