I am currently working on a API allowing users to get some event messages on their devices. Users call my API specifying me their preferred locale (for instance fr_FR
).
On the back-office, administrators can choose the languages they want to support. Let's assume they choose English and Chinese. So, they do not support French language.
I am using the Propel I18N behavior to deal with all the translations. In my requests, I am currently doing:
SystemMessageQuery::create()
->joinWithI18N('fr_FR')
->findOne();
It works correctly if I specify an existing locale. Yet, if the translation is not available, it returns null. Sounds logical.
My question is: can I fallback the user on a default locale (in this case, en_US
) if the provided locale is not planned by an administrator? If yes, how can I proceed?
I thought to send a prior request to check if the locale exists. But I do not think it is a good way for performances reasons (especially for an API).
Any idea?
Finally, I used the following code:
public function setLocale($locale = 'en_US', $fallbackEnabled = false)
{
if($fallbackEnabled) {
$localeExists = SystemMessageI18nQuery::create()
->filterByLocale($locale)
->filterById($this->getId())
->count();
if(!$localeExists) {
$locale = SystemMessageI18nQuery::create()
->filterById($this->getId())
->select('locale')
->findOne();
}
}
parent::setLocale($locale);
}
It is not the best solution for performance reasons, but it is functionnal.