Approach: Add a general field on all API-actions which activates the translation behavior.
class AbstractAction {
public function setLanguage(string $locale): static;
}
For example:
$msgs = MessageTemplate::get()
->setLimit(2)
->setLanguage('en_NZ')
->execute();
// Ex: User preferred 'en_NZ'. For $msgs[0], there was a prefect match.
assert $msgs[0]['msg_title'] === 'Gumboots: Are they too skux or too egg? Vote now!';
assert $msgs[0]['api.language'] === ['preferred' => 'en_NZ', 'matched' => 'en_NZ', ...];
// Ex: User preferred `en_NZ`. For $msgs[1], the closest match was `en_US`.
assert $msgs[1]['msg_title'] === 'Religion in the workplace: Is it holy or blasphemous? Vote now!';
assert $msgs[1]['api.language'] === ['preferred' => 'en_NZ', 'matched' => 'en_US', ...];
A couple fidgety things about this sketch:
- Which name to give the setter-method (eg
setLanguage()
orsetPreferredLanguage()
orsetLocale()
)?setLanguage()
is nice and pithy, but it already exists and does something different, and it sounds more matter-of-fact than it is. (The behavior is fuzzy/magical). - Which name to give the extra result field (eg
actual_language
orapi.language
or#locale
). There's no convention of outputting this kind of extra field in APIv4.