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.