1

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>
  • 1
    Dominic, could you please explain a bit on what you're trying to acchive? – carlcs Feb 22 '15 at 16:00
  • Yeah... not sure I'm following, either. – Brad Bell Feb 22 '15 at 17:45
  • That's easy, Dominic. No territory part in your locale →no territory! – carlcs Mar 2 '15 at 6:16
  • @carlcs So I have to modify the files from ../i18n/data/locale.php? I'd like to avoid that if So. – Dom Krulak Mar 2 '15 at 8:01
  • 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 Mar 3 '15 at 9:13
3

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 %}
        <li>Territory: {{ territory }}, Language: {{ language }}</li>
    {% endif %}

{% endfor %}
  • 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 – Dom Krulak Feb 23 '15 at 10:04
  • 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 Feb 23 '15 at 12:54
  • 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. – Dom Krulak Mar 6 '15 at 11:53
  • @Dominik now you can go crazy and do things like 'en_001', 'hu_sk', 'de_sk' or 'tr_de'. – carlcs Mar 6 '15 at 13:36
  • Love IT more! @carlcs Thank you, you good. – Dom Krulak Mar 6 '15 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.