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 %}
<ul>
{% for locale in locales %}
<li>
<a href="{{ craft.config.siteUrl[locale] ~ craft.request.getPath() }}">
({{ craft.i18n.getLocaleById(locale).nativeName }})
</a>
</li>
{% endfor %}
</ul>
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.
<ul class="uk-nav">
{% 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) : '' %}
<li>
<a href="{{ craft.config.siteUrl[locale] ~ craft.request.getPath() }}" class="{{ current ? 'uk-active'}}">
{% if territory %}{{ territory }}{% endif %} ({{craft.i18n.getLocaleById(locale).nativeName }})
</a>
</li>
{% endfor %}
</ul>
de
has no territory info (and could be spoken anywhere in the world), butde_de
does have that secific info, it's german as it is spoken in germany. It's that simple! – carlcs Mar 3 '15 at 9:13