<p>I had the same problem lately and solved it with a <code>locale</code> query parameter.</p>

<p>The code for this inside <code>craft/config/elementapi.php</code> looks like this:</p>

<pre><code>return [
    'endpoints' =&gt; function () {
        'entries.json' =&gt; function () {
            // Get the `locale` query param and fallback to
            // current locale if not defined.
            $locale = craft()-&gt;request-&gt;getQuery('locale', craft()-&gt;getLanguage());

            return [
                'elementType' =&gt; 'Entry',
                'criteria' =&gt; [
                    'section' =&gt; 'myEntries',
                    'locale' =&gt; $locale
                ],
                'transformer' =&gt; new EntriesTransformer()
            ];
        }
    }
];
</code></pre>

<p>If you want to use static translations or formatted dates inside your <code>transformer</code> you will need to specify the locale for that too. Look at <a href="https://craftcms.stackexchange.com/questions/16653/modify-craft-locale-inside-element-api-plugin">Modify Craft locale inside Element API plugin?</a> for a possible solutions.</p>

CC BY-SA 3.0
2016-09-03T14:22:49.753Z
2016-09-03T14:22:49.753Z
2017-04-13T12:51:00.160Z
periodical
<p>Is it possible to get the right locale based on url? For instance 
<code>http://example.com/entries.json</code> gives english content while <code>http://example.com/no/entries.json</code> gives norwegian content? Or can I somehow specify different locales in <code>criteria</code> or some other option, per endpoint in <code>elementapi.php</code></p>

<localization><plugin-elementapi>
Element API locale
# Element API locale

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

Is it possible to get the right locale based on url? For instance
`http://example.com/entries.json` gives english content while `http://example.com/no/entries.json` gives norwegian content? Or can I somehow specify different locales in `criteria` or some other option, per endpoint in `elementapi.php`

**Tags:** localization, plugin-elementapi

---

## 1 answers

### Answer by username_1 on 2016-09-03. Score: 4 | Accepted answer

I had the same problem lately and solved it with a locale query parameter.
The code for this inside craft/config/elementapi.php looks like this:
return [
    'endpoints' => function () {
        'entries.json' => function () {
            // Get the `locale` query param and fallback to
            // current locale if not defined.
            $locale = craft()->request->getQuery('locale', craft()->getLanguage());

            return [
                'elementType' => 'Entry',
                'criteria' => [
                    'section' => 'myEntries',
                    'locale' => $locale
                ],
                'transformer' => new EntriesTransformer()
            ];
        }
    }
];

If you want to use static translations or formatted dates inside your transformer you will need to specify the locale for that too. Look at Modify Craft locale inside Element API plugin? for a possible solutions.


---

