1

I'm working on a multilingual site with english and spanish locales. I've built persistent switcher that resides in the site footer. When users click it, it redirects to a corresponding entry in the other locale if one exists and the homepage if one does not.

The problem is that I have a handful of pages on my site that aren't entries: they are routes that are tied to particular templates. For example:

news-events/tags

Is the route for a template that lists all tags in use in my news section, and:

noticias-eventos/tags

would be the corresponding route on the spanish site.

I'd like to be able to check if the current uri is covered by a route and if so, whether there is a corresponding route in the other locale. If so, I'd redirect to that and if not, back to the homepage as per usual.

But from what I can see, routes are not localizable and exist independently of each other. Am I wrong? How would I go about this?

3

This is a rather hacky workaround, but I have no idea what else you could do to handle it. What I do is to compose an array of "route objects" that basically mirror the routes set up in the CP. This array can then be compared to the URL segments (→ getSegments). Not nice but it works :)

{# Map the corresponding routes #}
{% set routes = [
    { en: 'news-events/tags', es: 'noticias-eventos/tags' },
    { en: 'news-events/categories', es: 'noticias-eventos/categorias' },
] %}

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

    {# Is this an entry page? #}
    {% if entry is not defined %}

        {# ... Do entry stuff #}

    {# Not an entry page --> check the routes #}
    {% else %}

        {# Get URL segments #}
        {% set segments = craft.request.getSegments %}

        {# Set var before loop #}
        {% set noRoute = true %}

        {# Loop routes array #}
        {% for prop in routes %}

            {# Check if URL segments match a route #}
            {% if prop[craft.locale]|split('/') == segments %}

                {# Output link to the mapped route #}
                <a href="{{ craft.config.siteUrl[locale] ~ prop[locale] }}">{{ craft.i18n.getLocaleById(locale).name }}</a>

                {# Switch var value #}
                {% set noRoute = false %}

            {% endif %}

        {% endfor %}

        {# No route found --> output a link to the homepage #}
        {% if noRoute %}
            <a href="{{ craft.config.siteUrl[locale] }}">{{ craft.i18n.getLocaleById(locale).name }}</a>
        {% endif %}

    {% endif %}

{% endfor %}
  • To clean this up a bit you could try to use the containment operator in instead of the for loop. Not the first time I fail on getting it to work with objects. I'm not sure what I am doing wrong, should be possible according to the Twig docs. – carlcs Oct 11 '14 at 0:40
  • Thanks for the thoughtful response @carlcs. A great starting place. – Adam Snetman Oct 12 '14 at 17:35
2

I need something like this and found a solution: craft-languagelink

UPDATE: This solution does not work for what Adam needs.

  • Can you please share how to set up the plugin to link from news-events/tags to noticias-eventos/tags if the pages are no entry pages? – carlcs Jun 6 '16 at 13:13
  • sorry, @carlcs I got it wrong, and also need something and it did not work. – Luiz Boaretto Jun 23 '16 at 1:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.