3

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

4

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.

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.