Skip to main content
{# Configure and loop through selected site locales #}
{% set locales = ['en', 'hu'] %}
{% for locale in locales %}

    {# Check if locale equals the requested page locale #}
    {% set current = false %}
    {% 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 #}
            <a href="{{ localeEntry.getUrl() }}" class="nav-link{{ current ? ' current'}}">{{ craft.i18n.getLocaleById(locale).name }}</a>

        {% else %}

            {# Output a link to the hompage #}
            <a href="{{ craft.config.siteUrl[locale] }}" class="nav-link{{ current ? ' current'}}">{{ craft.i18n.getLocaleById(locale).name }}</a>

        {% endif %}

    {# Not an entry page #}
    {% else %}

        {# Output the same path with the locale's base URL (`siteUrl`) #}
        <a href="{{ craft.config.siteUrl[locale] ~ craft.request.getPath() }}" class="nav-link{{ current ? ' current'}}">{{ craft.i18n.getLocaleById(locale).name }}</a>

    {% endif %}

{% endfor %}
{# Configure and loop through selected site locales #}
{% set locales = ['en', 'hu'] %}
{% for locale in locales %}

    {# 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 #}
            <a href="{{ localeEntry.getUrl() }}" class="nav-link{{ current ? ' current'}}">{{ craft.i18n.getLocaleById(locale).name }}</a>

        {% else %}

            {# Output a link to the hompage #}
            <a href="{{ craft.config.siteUrl[locale] }}" class="nav-link{{ current ? ' current'}}">{{ craft.i18n.getLocaleById(locale).name }}</a>

        {% endif %}

    {# Not an entry page #}
    {% else %}

        {# Output the same path with the locale's base URL (`siteUrl`) #}
        <a href="{{ craft.config.siteUrl[locale] ~ craft.request.getPath() }}" class="nav-link{{ current ? ' current'}}">{{ craft.i18n.getLocaleById(locale).name }}</a>

    {% endif %}

{% endfor %}
{# Configure and loop through selected site locales #}
{% set locales = ['en', 'hu'] %}
{% for locale in locales %}

    {# Check if locale equals the requested page locale #}
    {% set current = false %}
    {% 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 #}
            <a href="{{ localeEntry.getUrl() }}" class="nav-link{{ current ? ' current'}}">{{ craft.i18n.getLocaleById(locale).name }}</a>

        {% else %}

            {# Output a link to the hompage #}
            <a href="{{ craft.config.siteUrl[locale] }}" class="nav-link{{ current ? ' current'}}">{{ craft.i18n.getLocaleById(locale).name }}</a>

        {% endif %}

    {# Not an entry page #}
    {% else %}

        {# Output the same path with the locale's base URL (`siteUrl`) #}
        <a href="{{ craft.config.siteUrl[locale] ~ craft.request.getPath() }}" class="nav-link{{ current ? ' current'}}">{{ craft.i18n.getLocaleById(locale).name }}</a>

    {% endif %}

{% endfor %}
replaced http://craftcms.stackexchange.com/ with https://craftcms.stackexchange.com/
Source Link

Code edits done to implement a lookup of the (translated) locale name as described herehere by Brandon Kelly

Code edits done to implement a lookup of the (translated) locale name as described here by Brandon Kelly

Code edits done to implement a lookup of the (translated) locale name as described here by Brandon Kelly

deleted 14 characters in body
Source Link
carlcs
  • 36.3k
  • 5
  • 63
  • 140
{# Configure and loop through selected site locales #}
{% set locales = ['en', 'hu'] %}
{% for locale in locales %}

    {# 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.locale == locale %}

            {# Output a link to it #}
            <a href="{{ localeEntry.getUrl() }}" class="nav-link{{ current ? ' current'}}">{{ craft.i18n.getLocaleById(locale).name }}</a>

        {% else %}

            {# Output a link to the hompage #}
            <a href="{{ craft.config.siteUrl[locale] }}" class="nav-link{{ current ? ' current'}}">{{ craft.i18n.getLocaleById(locale).name }}</a>

        {% endif %}

    {# Not an entry page #}
    {% else %}

        {# Output the same path with the locale's base URL (`siteUrl`) #}
        <a href="{{ craft.config.siteUrl[locale] ~ craft.request.getPath() }}" class="nav-link{{ current ? ' current'}}">{{ craft.i18n.getLocaleById(locale).name }}</a>

    {% endif %}

{% endfor %}
{# Configure and loop through selected site locales #}
{% set locales = ['en', 'hu'] %}
{% for locale in locales %}

    {# 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 #}
            <a href="{{ localeEntry.getUrl() }}" class="nav-link{{ current ? ' current'}}">{{ craft.i18n.getLocaleById(locale).name }}</a>

        {% else %}

            {# Output a link to the hompage #}
            <a href="{{ craft.config.siteUrl[locale] }}" class="nav-link{{ current ? ' current'}}">{{ craft.i18n.getLocaleById(locale).name }}</a>

        {% endif %}

    {# Not an entry page #}
    {% else %}

        {# Output the same path with the locale's base URL (`siteUrl`) #}
        <a href="{{ craft.config.siteUrl[locale] ~ craft.request.getPath() }}" class="nav-link{{ current ? ' current'}}">{{ craft.i18n.getLocaleById(locale).name }}</a>

    {% endif %}

{% endfor %}
{# Configure and loop through selected site locales #}
{% set locales = ['en', 'hu'] %}
{% for locale in locales %}

    {# 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.locale == locale %}

            {# Output a link to it #}
            <a href="{{ localeEntry.getUrl() }}" class="nav-link{{ current ? ' current'}}">{{ craft.i18n.getLocaleById(locale).name }}</a>

        {% else %}

            {# Output a link to the hompage #}
            <a href="{{ craft.config.siteUrl[locale] }}" class="nav-link{{ current ? ' current'}}">{{ craft.i18n.getLocaleById(locale).name }}</a>

        {% endif %}

    {# Not an entry page #}
    {% else %}

        {# Output the same path with the locale's base URL (`siteUrl`) #}
        <a href="{{ craft.config.siteUrl[locale] ~ craft.request.getPath() }}" class="nav-link{{ current ? ' current'}}">{{ craft.i18n.getLocaleById(locale).name }}</a>

    {% endif %}

{% endfor %}
{# Configure and loop through selected site locales #}
{% set locales = ['en', 'hu'] %}
{% for locale in locales %}

    {# 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 #}
            <a href="{{ localeEntry.getUrl() }}" class="nav-link{{ current ? ' current'}}">{{ craft.i18n.getLocaleById(locale).name }}</a>

        {% else %}

            {# Output a link to the hompage #}
            <a href="{{ craft.config.siteUrl[locale] }}" class="nav-link{{ current ? ' current'}}">{{ craft.i18n.getLocaleById(locale).name }}</a>

        {% endif %}

    {# Not an entry page #}
    {% else %}

        {# Output the same path with the locale's base URL (`siteUrl`) #}
        <a href="{{ craft.config.siteUrl[locale] ~ craft.request.getPath() }}" class="nav-link{{ current ? ' current'}}">{{ craft.i18n.getLocaleById(locale).name }}</a>

    {% endif %}

{% endfor %}
added 32 characters in body
Source Link
carlcs
  • 36.3k
  • 5
  • 63
  • 140
Loading
Code edits done to implement a lookup of the (translated) locale name
Source Link
carlcs
  • 36.3k
  • 5
  • 63
  • 140
Loading
added 61 characters in body
Source Link
carlcs
  • 36.3k
  • 5
  • 63
  • 140
Loading
added 32 characters in body
Source Link
carlcs
  • 36.3k
  • 5
  • 63
  • 140
Loading
deleted 72 characters in body
Source Link
carlcs
  • 36.3k
  • 5
  • 63
  • 140
Loading
added more info
Source Link
carlcs
  • 36.3k
  • 5
  • 63
  • 140
Loading
code corrections
Source Link
carlcs
  • 36.3k
  • 5
  • 63
  • 140
Loading
Source Link
carlcs
  • 36.3k
  • 5
  • 63
  • 140
Loading