How do I output the locale’s name?

17

2

When I output {{ craft.locale }} in my template, I get the current locale’s ID (e.g. “en”) rather than its name (e.g. “English”).

How do I get the locale’s name?

Brandon Kelly

Posted 2014-07-10T00:30:11.727

Reputation: 27 245

Answers

17

You can get the current locale’s name via craft.i18n.getCurrentLocale().

{% set locale = craft.i18n.getCurrentLocale() %}
{{ locale.name }}

That will output the name using the current locale’s translation.

If you would prefer to output the locale’s name in its native tongue, use nativeName instead:

{{ locale.nativeName }}

You can also output the name of any locale using (not necessarily the current one) using craft.i18n.getLocaleById():

{% set locale = craft.i18n.getLocaleById(entry.locale) %}
{{ locale.name }}
{{ locale.nativeName }}

Brandon Kelly

Posted 2014-07-10T00:30:11.727

Reputation: 27 245

Nice Brandon! Can you please add how to access the i18n name for non-current locales? Let's say all I have is a string "de". Thanks – carlcs – 2014-07-10T01:36:14.780

@ChristianSeelbach updated ;) – Brandon Kelly – 2014-07-10T12:54:30.750

Thanks! I now have to update this answer a 6th time ;)

– carlcs – 2014-07-10T13:11:37.900