<p>Actually, <code>entry.getLocales()</code> (or just <code>entry.locales</code>) will return an array of the locales enabled for the entry's section, not all locales in the system.</p>

<p>Unfortunately, there's no easy way to determine which locales a single entry is enabled for. One way to do it, is to use a <code>craft.entries</code> query per locale, and add the locale ids to an array whenever you get an element back from the query:</p>

<pre><code>{% set localesEnabledForEntry = [entry.locale] %}
{% for locale in entry.locales|keys %}
    {% set localeEntry = locale not in localesEnabledForEntry ? craft.entries.id(entry.id).locale(locale).first() : null %}
    {% if localeEntry %}
        {% set localesEnabledForEntry = localesEnabledForEntry|merge([locale]) %}
    {% endif %}
{% endfor %}
</code></pre>

<p>Of course, if you're looping through a lot of entries (and/or you have a lot of locales enabled for the entries' section(s)), this will quickly become a performance issue (you're basically doing 1 additional database query per entry per locale, except for the current locale being viewed).</p>

CC BY-SA 3.0
2016-08-04T18:10:19.170Z
That makes sense. I'm actually doing this in PHP with the ElementAPI plugin but I can see how that twig would easily translate. Probably couldn't take that performance hit though... I wonder if it's feasible to get it in one query with a direct query to the database.
Jameal G
CC BY-SA 3.0
2016-08-04T18:21:39.673Z
For sure - I assumed you needed to do this in a template, but the concept is easily translatable to PHP. However once you're in PHP land a [custom database query](https://craftcms.com/docs/plugins/database) would make way more sense, and also avoid the performance hit.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-08-03T23:31:15.970Z
2016-08-03T23:31:15.970Z
Mats Mikkel Rummelhoff
<p>I can use <a href="https://craftcms.com/classreference/models/EntryModel#getLocales-detail" rel="nofollow">getLocales()</a> off of the entry model to get all locales in the system, and whether they are enabled by default for that entry type, but it doesn't actually return which locales are enabled for that given entry. Am I missing something?</p>

CC BY-SA 3.0
2016-08-03T23:29:47.727Z
Is it in the context of a plugin/PHP or Twig?
Brad Bell
CC BY-SA 3.0
2016-08-04T14:04:55.733Z
Plugin/PHP. I'm actually using ElementAPI to return all entries from a section and I want to show users what locales each entry is available in.
Jameal G
<localization><entrymodel>
How can I get the locales that are actually enabled for a given entry?
# How can I get the locales that are actually enabled for a given entry?

(Asked by: username_0 on 2016-08-03)

I can use [getLocales()](https://craftcms.com/classreference/models/EntryModel#getLocales-detail) off of the entry model to get all locales in the system, and whether they are enabled by default for that entry type, but it doesn't actually return which locales are enabled for that given entry. Am I missing something?

**Tags:** localization, entrymodel
> **Comments:**
> **username_1**: Is it in the context of a plugin/PHP or Twig?  
> **username_0**: Plugin/PHP. I'm actually using ElementAPI to return all entries from a section and I want to show users what locales each entry is available in.  

---

## 1 answers

### Answer by username_2 on 2016-08-03. Score: 5

Actually, entry.getLocales() (or just entry.locales) will return an array of the locales enabled for the entry's section, not all locales in the system.
Unfortunately, there's no easy way to determine which locales a single entry is enabled for. One way to do it, is to use a craft.entries query per locale, and add the locale ids to an array whenever you get an element back from the query:
{% set localesEnabledForEntry = [entry.locale] %}
{% for locale in entry.locales|keys %}
    {% set localeEntry = locale not in localesEnabledForEntry ? craft.entries.id(entry.id).locale(locale).first() : null %}
    {% if localeEntry %}
        {% set localesEnabledForEntry = localesEnabledForEntry|merge([locale]) %}
    {% endif %}
{% endfor %}

Of course, if you're looping through a lot of entries (and/or you have a lot of locales enabled for the entries' section(s)), this will quickly become a performance issue (you're basically doing 1 additional database query per entry per locale, except for the current locale being viewed).

> **Comments:**
> **username_0**: That makes sense. I'm actually doing this in PHP with the ElementAPI plugin but I can see how that twig would easily translate. Probably couldn't take that performance hit though... I wonder if it's feasible to get it in one query with a direct query to the database.  
> **username_2**: For sure - I assumed you needed to do this in a template, but the concept is easily translatable to PHP. However once you're in PHP land a [custom database query](https://craftcms.com/docs/plugins/database) would make way more sense, and also avoid the performance hit.  

---

