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
Asked
Active
Viewed 787 times
4
Spoeken
- 327
- 3
- 10
1 Answers
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.
periodical
- 106
- 6