<p>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:</p>

<pre><code>&lt;!-- language: lang-twig --&gt;

    /{{ craft.request.path }}
</code></pre>

<p>Example: </p>

<pre><code>&lt;!-- language: lang-twig --&gt;

    &lt;form action="" name="dropdown"&gt;
        &lt;select name="country-select"  id="country-select" class="form-control" onchange="location = this.value;" autocomplete="off"&gt;&gt;
            {% 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" %}
                        &lt;option value="/{{ loc | replace({'en': 'eu'}) }}/{{ craft.request.path }}" selected&gt;Europe&lt;/option&gt;   

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

                    {% endif %}

                {% else %}

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

                {% endif %}

            {% endfor %}
        &lt;/select&gt;
    &lt;/form&gt;
</code></pre>

<p>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!</p>

CC BY-SA 3.0
2017-11-19T20:42:48.677Z
2017-11-19T20:42:48.677Z
hcone5006
<p>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. </p>

<pre><code>&lt;!-- language: lang-twig --&gt;
&lt;form action="" name="dropdown"&gt;
    &lt;select name="country-select"  id="country-select" class="form-control" onchange="location = this.value;" autocomplete="off"&gt;&gt;
        {% 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" %}
                    &lt;option value="/{{ loc | replace({'en': 'eu'}) }}" selected&gt;Europe&lt;/option&gt;   

                    {% set myJs %}

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

                    {% endset %}

                    {% includeJs myJs %}

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

                    {% set myJs %}

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


                    {% endset %}

                    {% includeJs myJs %}

                {% endif %}

            {% else %}

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

            {% endif %}

        {% endfor %}
    &lt;/select&gt;
&lt;/form&gt;
</code></pre>

<p>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. </p>

<p>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.</p>

CC BY-SA 3.0
2017-11-16T03:50:56.630Z
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
CC BY-SA 3.0
2017-11-19T20:44:42.890Z
Thanks, @BradBell I've done that as you suggested. Hope it helps someone else!
hcone5006
<localization><multi-environment><segments>
Is it possible to change one segment of the current url and keep the rest of it the same in a cleaner way?
# Is it possible to change one segment of the current url and keep the rest of it the same in a cleaner way?

(Asked by: username_0 on 2017-11-16)

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. 


    >
        {% 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" %}
                    Europe   

                    {% 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) : '' %}  
                        {% if territory %}{{ territory }}{% endif %}  

                    {% set myJs %}

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


                    {% endset %}

                    {% includeJs myJs %}

                {% endif %}

            {% else %}

                {% if loc == "en" %}
                    EU   
                {% else %}
                    {{ loc | replace({'en': ''}) | trim('_') | upper }}    
                {% endif %}

            {% endif %}

        {% endfor %}
    


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.

**Tags:** localization, multi-environment, segments

> **Comments:**
> **username_1**: Glad you got it sorted, @username_0 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?  
> **username_0**: Thanks, @BradBell I've done that as you suggested. Hope it helps someone else!  

---

## 1 answers

### Answer by username_0 on 2017-11-19. Score: 1 | Accepted answer

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:


    /{{ craft.request.path }}

Example: 


    
        >
            {% 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" %}
                        Europe   

                    {% else %}
                        {% set subTags = locale|split('_') %}
                        {% set territory = subTags|length > 1 ? craft.i18n.getLocaleData(craft.locale).getTerritory(subTags|last) : '' %}  
                            {% if territory %}{{ territory }}{% endif %}  

                    {% endif %}

                {% else %}

                    {% if loc == "en" %}
                        EU   
                    {% else %}
                        {{ loc | replace({'en': ''}) | trim('_') | upper }}    
                    {% endif %}

                {% endif %}

            {% endfor %}
        
    

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!


---

