1

How would you go about changing one segment of the current url? I have a select in the footer that you can click on a country name and then it redirects you to the homepage of that country version of the website.

<!-- language: lang-twig -->
<form action="" name="dropdown">
    <select name="country-select"  id="country-select" class="form-control" onchange="location = this.value;" autocomplete="off">>
        {% set locales = craft.i18n.getSiteLocaleIds() %}
        {% set locale = craft.locale %}
        {% for loc in locales %}
            {# checking if current loop has active locale #}
            {% set isHome = (loc == locale) %}
            {% if isHome %}

                {% if loc == "en" %}
                    <option value="/{{ loc | replace({'en': 'eu'}) }}" selected>Europe</option>   

                    {% set myJs %}

                        var currentCountryLocale = "Europe";
                        var currentCountryLocale = "{{ loc | replace({'en': 'eu'}) | trim('_') }}";

                    {% endset %}

                    {% includeJs myJs %}

                {% else %}
                    {% set subTags = locale|split('_') %}
                    {% set territory = subTags|length > 1 ? craft.i18n.getLocaleData(craft.locale).getTerritory(subTags|last) : '' %}  
                        <option value="/{{ loc | replace({'en': ''}) | trim('_') }}" selected>{% if territory %}{{ territory }}{% endif %}</option>  

                    {% set myJs %}

                        var currentCountryName = "{{ territory }}";
                        var currentCountryLocale = "{{ loc | replace({'en': ''}) | trim('_') }}";


                    {% endset %}

                    {% includeJs myJs %}

                {% endif %}

            {% else %}

                {% if loc == "en" %}
                    <option value="/{{ loc | replace({'en': 'eu'}) }}">EU</option>   
                {% else %}
                    <option value="/{{ loc | replace({'en': ''}) | trim('_') }}">{{ loc | replace({'en': ''}) | trim('_') | upper }} </option>   
                {% endif %}

            {% endif %}

        {% endfor %}
    </select>
</form>

I have been trying to figure out how to switch only the country/region part of the url, which is segment(2), but all of my experiments so far have been extremely complicated and confusing and I haven't found a good solution yet. Am I missing something? There must be a more efficient way to accomplish this, as it's probably not a good idea to wrap conditionals inside of conditionals inside of more conditionals.

I also have the added complexity of needing to include a continent (Europe) as one of the versions of the website, for which I'm using the "en" locale, as you can see in the bit of code above.

2
  • 1
    Glad you got it sorted, @hcone5006 and thanks for sharing what worked for you! Would you mind adding your solution as an official answer instead of an edit to your original question?
    – Brad Bell
    Commented Nov 16, 2017 at 3:50
  • Thanks, @BradBell I've done that as you suggested. Hope it helps someone else!
    – hcone5006
    Commented Nov 19, 2017 at 20:44

1 Answer 1

1

So!!!! I was getting confused about the various craft properties pertaining to urls, and I thought that even if I grab the path, that it would include the region segment, but surprise! {{ craft.request.path }} grabs the remaining portion of the url, after the region segment! So, I can use code that I've written above and I only need to add:

<!-- language: lang-twig -->

    /{{ craft.request.path }}

Example:

<!-- language: lang-twig -->

    <form action="" name="dropdown">
        <select name="country-select"  id="country-select" class="form-control" onchange="location = this.value;" autocomplete="off">>
            {% set locales = craft.i18n.getSiteLocaleIds() %}
            {% set locale = craft.locale %}
            {% for loc in locales %}
                {# checking if current loop has active locale #}
                {% set isHome = (loc == locale) %}
                {% if isHome %}

                    {% if loc == "en" %}
                        <option value="/{{ loc | replace({'en': 'eu'}) }}/{{ craft.request.path }}" selected>Europe</option>   

                    {% else %}
                        {% set subTags = locale|split('_') %}
                        {% set territory = subTags|length > 1 ? craft.i18n.getLocaleData(craft.locale).getTerritory(subTags|last) : '' %}  
                            <option value="/{{ loc | replace({'en': ''}) | trim('_') }}/{{ craft.request.path }}" selected>{% if territory %}{{ territory }}{% endif %}</option>  

                    {% endif %}

                {% else %}

                    {% if loc == "en" %}
                        <option value="/{{ loc | replace({'en': 'eu'}) }}/{{ craft.request.path }}">EU</option>   
                    {% else %}
                        <option value="/{{ loc | replace({'en': ''}) | trim('_') }}/{{ craft.request.path }}">{{ loc | replace({'en': ''}) | trim('_') | upper }} </option>   
                    {% endif %}

                {% endif %}

            {% endfor %}
        </select>
    </form>

Yay!! I wanted to share my answer because I've been working on this issue for a few days and couldn't find a solution anywhere. Maybe it will help someone else that runs into this issue when building a multi-country website with Craft. Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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