<p>You can't get a territory name directly from a locale tag (e.g. 'en' or 'en_gb'). But what you could do is to
take the territory part of the tag (two letter country code; <strong>keep in mind, it's optional</strong>) and get the territory
name via <code>getTerritory</code>:</p>

<pre><code>{% set locales = ['en_gb', 'fr_fr', 'zh_cn'] %}

{% for locale in locales %}

    {% set subTags = locale|split('_') %}

    {% set territory = subTags|length &gt; 1 ? craft.i18n.getLocaleData(craft.locale).getTerritory(subTags|last) : '' %}

    {% set language = craft.i18n.getLocaleById(locale).name %}
    {% set language = language ?: craft.i18n.getLocaleById(subTags|first).name %}
    {% set language = language|replace("/- #{territory}$/", '') %}

    {% if territory and language %}
        &lt;li&gt;Territory: {{ territory }}, Language: {{ language }}&lt;/li&gt;
    {% endif %}

{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-02-23T10:04:22.103Z
Hey @carlcs, I got it tried with the code I updated the question with, but I always got only the same country, for all locales. You're right. There is no relation with locales and territories, so it's impossible yet. Thanks anyway
Dominik Krulak
CC BY-SA 3.0
2015-02-23T12:54:20.497Z
No @Dominic! There actually is a relationship between locals and territories, I think you misunderstood my answer. The code works as is, so you probably didn't implemented it correctly for your needs.
carlcs
CC BY-SA 3.0
2015-03-06T11:53:46.700Z
Finally I got to it and have started testing. Yes, it works, It's great, except regions `en_001` etc. I get empty value for `locale`.
Dominik Krulak
CC BY-SA 3.0
2015-03-06T13:36:25.407Z
@Dominik now you can go crazy and do things like 'en_001', 'hu_sk', 'de_sk' or 'tr_de'.
carlcs
CC BY-SA 3.0
2015-03-06T14:30:28.223Z
Love IT more! @carlcs Thank you, you good.
Dominik Krulak
CC BY-SA 3.0
2015-02-22T20:36:14.767Z
2015-03-06T13:21:32.000Z
2015-03-06T13:21:32.000Z
carlcs
<p>I'm asking, because I was trying to find out the whole afternoon. Found some posts, but they were related to getting all territories.</p>

<p>Example:</p>

<p>I have locales like English, French, Chinese and I'd like to add country to each related locale from territories, because each locale.php in /data folder has the values.</p>

<p>So it would looks like: Territory(United Kingdom) locale(English)</p>

<p>Code I'm working on:</p>

<pre><code>{% set locale = craft.i18n.getCurrentLocale() %}
    {# Configure site locales #}
    {% set locales = ['en', 'en_gb', 'sk'] %}
    {# Check if locale equals the requested page locale #}
    {% if locale == craft.locale %}
        {% set current = true %}
    {% else %}
        {% set current = false %}
    {% endif %}

&lt;ul&gt;
  {% for locale in locales %}
    &lt;li&gt;
      &lt;a href="{{ craft.config.siteUrl[locale] ~ craft.request.getPath() }}"&gt;
         ({{ craft.i18n.getLocaleById(locale).nativeName }})
      &lt;/a&gt;
    &lt;/li&gt;
  {% endfor %} 
&lt;/ul&gt;
</code></pre>

<p>The output is a list of locales with native name:</p>

<ul>
<li>(English)</li>
<li>(British English)</li>
<li>(Slovak)</li>
</ul>

<p>If there would be a way to relate it's country to each locale from territories, that we wouldn't have to touch app folder to customize the lang files, like I read in other posts.
By doing so, We could achieve this:</p>

<ul>
<li>World (English)</li>
<li>United Kingdom (British English)</li>
<li>Slovakia (Slovak)</li>
</ul>

<p><strong>Update</strong></p>

<p>I think it moved on a little bit. With the code @carlcs provided, Craft was able to get a territory. There must to be some bug in it, because <strong>from 3 locales, It got assigned to only one territory</strong> (United Kingdom - British English).</p>

<p>Here is updated code.</p>

<pre><code> &lt;ul class="uk-nav"&gt;
 {% set locales = ['en', 'en_gb', 'sk'] %}
   {% for locale in locales %}
      {% set subTags = locale|split('_') %}
         {% set territory = subTags|length &gt; 1 ? craft.i18n.getLocaleData(craft.locale).getTerritory(subTags|last) : '' %}  
             &lt;li&gt;
                 &lt;a href="{{ craft.config.siteUrl[locale] ~ craft.request.getPath() }}" class="{{ current ? 'uk-active'}}"&gt;
                 {% if territory %}{{ territory }}{% endif %} ({{craft.i18n.getLocaleById(locale).nativeName }})
                 &lt;/a&gt;
             &lt;/li&gt;
  {% endfor %} 
  &lt;/ul&gt;
</code></pre>

CC BY-SA 3.0
2015-02-22T16:00:36.057Z
Dominic, could you please explain a bit on what you're trying to acchive?
carlcs
CC BY-SA 3.0
2015-02-22T17:45:37.523Z
Yeah... not sure I'm following, either.
Brad Bell
CC BY-SA 3.0
2015-03-02T06:16:20.763Z
That's easy, Dominic. No territory part in your locale →no territory!
carlcs
CC BY-SA 3.0
2015-03-02T08:01:44.567Z
@carlcs So I have to modify the files from ../i18n/data/locale.php? I'd like to avoid that if So.
Dominik Krulak
CC BY-SA 3.0
2015-03-03T09:13:08.707Z
No, use locales with territory subtags only! Example: locale `de` has no territory info (and could be spoken anywhere in the world), but `de_de` does have that secific info, it's german as it is spoken in germany. It's that simple!
carlcs
<templating><localization>
Get the territory / country belonging to a locale?
# Get the territory / country belonging to a locale?

(Asked by: username_0 on 2015-02-22)

I'm asking, because I was trying to find out the whole afternoon. Found some posts, but they were related to getting all territories.
Example:
I have locales like English, French, Chinese and I'd like to add country to each related locale from territories, because each locale.php in /data folder has the values.
So it would looks like: Territory(United Kingdom) locale(English)
Code I'm working on:
{% set locale = craft.i18n.getCurrentLocale() %}
    {# Configure site locales #}
    {% set locales = ['en', 'en_gb', 'sk'] %}
    {# Check if locale equals the requested page locale #}
    {% if locale == craft.locale %}
        {% set current = true %}
    {% else %}
        {% set current = false %}
    {% endif %}


  {% for locale in locales %}
    
      
         ({{ craft.i18n.getLocaleById(locale).nativeName }})
      
    
  {% endfor %} 


The output is a list of locales with native name:

(English)
(British English)
(Slovak)

If there would be a way to relate it's country to each locale from territories, that we wouldn't have to touch app folder to customize the lang files, like I read in other posts.
By doing so, We could achieve this:

World (English)
United Kingdom (British English)
Slovakia (Slovak)

Update
I think it moved on a little bit. With the code @carlcs provided, Craft was able to get a territory. There must to be some bug in it, because from 3 locales, It got assigned to only one territory (United Kingdom - British English).
Here is updated code.
 
 {% set locales = ['en', 'en_gb', 'sk'] %}
   {% for locale in locales %}
      {% set subTags = locale|split('_') %}
         {% set territory = subTags|length > 1 ? craft.i18n.getLocaleData(craft.locale).getTerritory(subTags|last) : '' %}  
             
                 
                 {% if territory %}{{ territory }}{% endif %} ({{craft.i18n.getLocaleById(locale).nativeName }})
                 
             
  {% endfor %}

**Tags:** templating, localization
> **Comments:**
> **username_1**: Dominic, could you please explain a bit on what you're trying to acchive?  
> **username_2**: Yeah... not sure I'm following, either.  
> **username_1**: That's easy, Dominic. No territory part in your locale →no territory!  
> **username_0**: @carlcs So I have to modify the files from ../i18n/data/locale.php? I'd like to avoid that if So.  
> **username_1**: No, use locales with territory subtags only! Example: locale `de` has no territory info (and could be spoken anywhere in the world), but `de_de` does have that secific info, it's german as it is spoken in germany. It's that simple!  

---

## 1 answers

### Answer by username_1 on 2015-02-22. Score: 3 | Accepted answer

You can't get a territory name directly from a locale tag (e.g. 'en' or 'en_gb'). But what you could do is to
take the territory part of the tag (two letter country code; keep in mind, it's optional) and get the territory
name via getTerritory:
{% set locales = ['en_gb', 'fr_fr', 'zh_cn'] %}

{% for locale in locales %}

    {% set subTags = locale|split('_') %}

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

    {% set language = craft.i18n.getLocaleById(locale).name %}
    {% set language = language ?: craft.i18n.getLocaleById(subTags|first).name %}
    {% set language = language|replace("/- #{territory}$/", '') %}

    {% if territory and language %}
        Territory: {{ territory }}, Language: {{ language }}
    {% endif %}

{% endfor %}

> **Comments:**
> **username_0**: Hey @carlcs, I got it tried with the code I updated the question with, but I always got only the same country, for all locales. You're right. There is no relation with locales and territories, so it's impossible yet. Thanks anyway  
> **username_1**: No @Dominic! There actually is a relationship between locals and territories, I think you misunderstood my answer. The code works as is, so you probably didn't implemented it correctly for your needs.  
> **username_0**: Finally I got to it and have started testing. Yes, it works, It's great, except regions `en_001` etc. I get empty value for `locale`.  
> **username_1**: @Dominik now you can go crazy and do things like 'en_001', 'hu_sk', 'de_sk' or 'tr_de'.  
> **username_0**: Love IT more! @carlcs Thank you, you good.  

---

