<p>Here are some other <a href="https://craftcms.stackexchange.com/questions/9005/how-to-create-a-query-to-group-alternate-language-pages">similar</a> <a href="https://craftcms.stackexchange.com/questions/8244/retrieving-an-entry-by-id-in-all-locales">questions</a> and answers to this, but the short of it is that you could probably do it with something like using QueryBuilder to query the database directly (and bypassing Craft's APIs) or grabbing all site locales and looping through and re-querying for the entries on a per locale basis.</p>

<p>Both of them are pretty ugly and probably not that efficient in the context of the ElementAPI plugin.</p>

CC BY-SA 3.0
2015-12-30T19:46:05.647Z
2015-12-30T19:46:05.647Z
Brad Bell
<p>I use Craft to return some JSON data. The content however is available in different languages and I would like to get all those translations in a single API call. </p>

<p>I tried something like </p>

<pre><code>'endpoints' =&gt; [
    'products.json' =&gt; [
        'elementType' =&gt; 'Entry',
        'criteria' =&gt; ['section' =&gt; 'products', 'order' =&gt; ['default' =&gt; 'lft asc']],
        'transformer' =&gt; function(EntryModel $entry) {
            $translate = [];
            foreach(array_keys($entry-&gt;getLocales()) as $locale) {
                //$entry-&gt;setLanguage($locale); Doesn't work with EntryModel
                $translate[$locale] = [
                    'title' =&gt; $entry-&gt;title,
                    'slug' =&gt; $entry-&gt;slug
                    // ...
                ];
            }
            return $translate;
        },
    ],
</code></pre>

<p>Unfortunately, there doesn't seem to be a method call <code>setLanguage()</code> like with fields. 
I'm out of ideas. </p>

<localization><elementcriteriamodel><plugin-elementapi>
Receive all locales with ElementAPI plugin
# Receive all locales with ElementAPI plugin

(Asked by: username_0 on 2015-12-30)

I use Craft to return some JSON data. The content however is available in different languages and I would like to get all those translations in a single API call. 
I tried something like 
'endpoints' => [
    'products.json' => [
        'elementType' => 'Entry',
        'criteria' => ['section' => 'products', 'order' => ['default' => 'lft asc']],
        'transformer' => function(EntryModel $entry) {
            $translate = [];
            foreach(array_keys($entry->getLocales()) as $locale) {
                //$entry->setLanguage($locale); Doesn't work with EntryModel
                $translate[$locale] = [
                    'title' => $entry->title,
                    'slug' => $entry->slug
                    // ...
                ];
            }
            return $translate;
        },
    ],

Unfortunately, there doesn't seem to be a method call setLanguage() like with fields. 
I'm out of ideas.

**Tags:** localization, elementcriteriamodel, plugin-elementapi

---

## 1 answers

### Answer by username_1 on 2015-12-30. Score: 2 | Accepted answer

Here are some other [similar](https://craftcms.stackexchange.com/questions/9005/how-to-create-a-query-to-group-alternate-language-pages) [questions](https://craftcms.stackexchange.com/questions/8244/retrieving-an-entry-by-id-in-all-locales) and answers to this, but the short of it is that you could probably do it with something like using QueryBuilder to query the database directly (and bypassing Craft's APIs) or grabbing all site locales and looping through and re-querying for the entries on a per locale basis.

Both of them are pretty ugly and probably not that efficient in the context of the ElementAPI plugin.


---

