<p>If you look at <a href="https://github.com/pixelandtonic/Craft-Release/blob/master/app/framework/i18n/data/en_gb.php" rel="nofollow noreferrer">the data file for a locale in Yii</a> you can see that the territories property includes regions and continents as you've found. These all have keys with numeric values, whereas countries have two character keys. Using this knowledge, you can loop over the keys and values of the territories array as you are doing, but only output those with two character keys. You'll also probably want to omit the <code>Unknown Region</code> value which has a key of <code>zz</code>.</p>

<p>Untested, but this should work:</p>

<pre><code>&lt;select name="country" required&gt;
    {% for key, value in craft.i18n.getLocaleData().getAllTerritories() %}
        {% if (key matches '/[a-z][a-z]/') and (key != 'zz') %}
            &lt;option&gt;{{ value }}&lt;/option&gt;
        {% endif %}
    {% endfor %}
&lt;/select&gt;
</code></pre>

<hr>

<p>EDIT</p>

<p>OK, so my original idea doesn't work as <code>craft.i18n.getLocaleData().getAllTerritories()</code> doesn't preserve the territory array keys from the Yii i18n data files.</p>

<p>I'm not sure you can do what you want to do natively in Craft, but it looks like <a href="https://github.com/johndwells/craft.territories" rel="nofollow noreferrer">John D. Wells' Territories fieldtype plugin</a> may provide what you need. You don't need to use the fieldtype as there is an undocumented Twig variable provided that gives you an array of localised territory names, excluding the regions. Try this:</p>

<pre><code>&lt;select name="country" required&gt;
    {% for country in craft.territories.getTerritories() %}
        &lt;option&gt;{{ country }}&lt;/option&gt;
    {% endfor %}
&lt;/select&gt;
</code></pre>

CC BY-SA 3.0
2017-03-06T15:51:36.410Z
Thanks Steve. I get a preg_match(): Unknown modifier '{' error. Been Googling around but I can't figure it out.
Ian Ebden
CC BY-SA 3.0
2017-03-06T15:53:57.973Z
Ha! Looks like I was trying to be too clever with the regex. Try the amended version to see if that works. ;-) `{% if (key matches '[a-z][a-z]') and (key != 'zz') %}`
Steve Rowling
CC BY-SA 3.0
2017-03-06T15:55:18.167Z
Hehe, nearly there! preg_match(): Unknown modifier '['
Ian Ebden
CC BY-SA 3.0
2017-03-06T15:57:22.433Z
Oops, looks like I forgot to enclose the pattern in slashes. This _should_ work now: `{% if (key matches '/[a-z][a-z]/') and (key != 'zz') %}`
Steve Rowling
CC BY-SA 3.0
2017-03-06T16:03:51.450Z
Thnx. Well, no errors this time but no output either.
Ian Ebden
CC BY-SA 3.0
2017-03-06T16:23:29.460Z
OK, I see what is happening. `craft.i18n.getLocaleData().getAllTerritories() ` returns a simple array with numeric keys. It doesn't keep the territory keys from the Yii data file. I'm not sure you can do what you want to do natively in Craft, but it looks like John D. Wells' [Territories][1] [1]:https://github.com/johndwells/craft.territories fieldtype plugin may provide what you need. You don't need to use the fieldtype as there is a Twig variable provided that gives you an array of localised territory names, excluding the regions. More details in the edited answer above.
Steve Rowling
CC BY-SA 3.0
2017-03-06T16:49:54.957Z
Thanks Steve. I did start off with that plugin but I couldn't order the list, and just wanted to do it natively if poss. I'll have a fiddle but thanks, I should be good from here.
Ian Ebden
CC BY-SA 3.0
2017-03-06T13:33:30.343Z
2017-03-06T16:23:26.253Z
2017-03-06T16:23:26.253Z
Steve Rowling
<p>Wonder if you can help? Got a booking form on a multi-locale site and I need to include the user's country in the form. I'm using Craft's LocaleData class to output locale-specific locales (e.g. Angleterre for England)...</p>

<pre><code>&lt;select name="country" required&gt;
{% for country in craft.i18n.getLocaleData().getAllTerritories() %}
    &lt;option&gt;{{ country }}&lt;/option&gt;
{% endfor %}&lt;/select&gt;
</code></pre>

<p>Works great except it outputs stuff I don't need like "World", "Oceana", "Ascension Island" etc... Is there any way to control the output, like maybe show countries only and not regions?</p>

CC BY-SA 3.0
2017-03-06T11:17:40.700Z
I think it should be `{{ country.name }}` now it wont output anything good if im right.
Justin Dekkers
CC BY-SA 3.0
2017-03-06T11:28:08.300Z
Yeah that throws an error.
Ian Ebden
<localization>
Locale-specific country select menu
# Locale-specific country select menu

(Asked by: username_0 on 2017-03-06)

Wonder if you can help? Got a booking form on a multi-locale site and I need to include the user's country in the form. I'm using Craft's LocaleData class to output locale-specific locales (e.g. Angleterre for England)...

{% for country in craft.i18n.getLocaleData().getAllTerritories() %}
    {{ country }}
{% endfor %}

Works great except it outputs stuff I don't need like "World", "Oceana", "Ascension Island" etc... Is there any way to control the output, like maybe show countries only and not regions?

**Tags:** localization
> **Comments:**
> **username_1**: I think it should be `{{ country.name }}` now it wont output anything good if im right.  
> **username_0**: Yeah that throws an error.  

---

## 1 answers

### Answer by username_2 on 2017-03-06. Score: 1

If you look at the data file for a locale in Yii you can see that the territories property includes regions and continents as you've found. These all have keys with numeric values, whereas countries have two character keys. Using this knowledge, you can loop over the keys and values of the territories array as you are doing, but only output those with two character keys. You'll also probably want to omit the Unknown Region value which has a key of zz.
Untested, but this should work:

    {% for key, value in craft.i18n.getLocaleData().getAllTerritories() %}
        {% if (key matches '/[a-z][a-z]/') and (key != 'zz') %}
            {{ value }}
        {% endif %}
    {% endfor %}



EDIT
OK, so my original idea doesn't work as craft.i18n.getLocaleData().getAllTerritories() doesn't preserve the territory array keys from the Yii i18n data files.
I'm not sure you can do what you want to do natively in Craft, but it looks like John D. Wells' Territories fieldtype plugin may provide what you need. You don't need to use the fieldtype as there is an undocumented Twig variable provided that gives you an array of localised territory names, excluding the regions. Try this:

    {% for country in craft.territories.getTerritories() %}
        {{ country }}
    {% endfor %}

> **Comments:**
> **username_0**: Thanks Steve. I get a preg_match(): Unknown modifier '{' error. Been Googling around but I can't figure it out.  
> **username_2**: Ha! Looks like I was trying to be too clever with the regex. Try the amended version to see if that works. ;-) `{% if (key matches '[a-z][a-z]') and (key != 'zz') %}`  
> **username_0**: Hehe, nearly there! preg_match(): Unknown modifier '['  
> **username_2**: Oops, looks like I forgot to enclose the pattern in slashes. This _should_ work now: `{% if (key matches '/[a-z][a-z]/') and (key != 'zz') %}`  
> **username_0**: Thnx. Well, no errors this time but no output either.  
> **username_2**: OK, I see what is happening. `craft.i18n.getLocaleData().getAllTerritories() ` returns a simple array with numeric keys. It doesn't keep the territory keys from the Yii data file. I'm not sure you can do what you want to do natively in Craft, but it looks like John D. Wells' [Territories][1] [1]:https://github.com/johndwells/craft.territories fieldtype plugin may provide what you need. You don't need to use the fieldtype as there is a Twig variable provided that gives you an array of localised territory names, excluding the regions. More details in the edited answer above.  
> **username_0**: Thanks Steve. I did start off with that plugin but I couldn't order the list, and just wanted to do it natively if poss. I'll have a fiddle but thanks, I should be good from here.  

---

