namespace Civi\API\Subscriber;
use Civi\API\Events;
+use Civi\Core\Locale;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Used for rolling back language to its original setting after the api call.
*
* @var array
+ * Array(string $requestId => \Civi\Core\Locale $locale).
*/
- public $originalLang = [];
+ protected $originalLocale = [];
/**
* @return array
$language = $params['language'] ?? NULL;
}
if ($language) {
- $this->setLocale($language, $apiRequest['id']);
+ $newLocale = Locale::negotiate($language);
+ if ($newLocale) {
+ $this->originalLocale[$apiRequest['id']] = Locale::detect();
+ $newLocale->apply();
+ }
}
}
public function onApiRespond(\Civi\API\Event\Event $event) {
$apiRequest = $event->getApiRequest();
- if (!empty($this->originalLang[$apiRequest['id']])) {
- global $tsLocale;
- global $dbLocale;
- $tsLocale = $this->originalLang[$apiRequest['id']]['tsLocale'];
- $dbLocale = $this->originalLang[$apiRequest['id']]['dbLocale'];
- }
- }
-
- /**
- * Sets the tsLocale and dbLocale for multi-lingual sites.
- * Some code duplication from CRM/Core/BAO/ConfigSetting.php retrieve()
- * to avoid regressions from refactoring.
- * @param string $newLocale
- * @param int $requestId
- * @throws \API_Exception
- */
- public function setLocale($newLocale, $requestId) {
- $domain = new \CRM_Core_DAO_Domain();
- $domain->id = \CRM_Core_Config::domainID();
- $domain->find(TRUE);
-
- // Check if the site is multi-lingual
- if ($domain->locales && $newLocale) {
- // Validate language, otherwise a bad dbLocale could probably lead to sql-injection.
- if (!array_key_exists($newLocale, \Civi::settings()->get('languageLimit'))) {
- throw new \API_Exception(ts('Language not enabled: %1', [1 => $newLocale]));
- }
-
- global $dbLocale;
- global $tsLocale;
-
- // Store original value to be restored in $this->onApiRespond
- $this->originalLang[$requestId] = [
- 'tsLocale' => $tsLocale,
- 'dbLocale' => $dbLocale,
- ];
-
- // Set suffix for table names - use views if more than one language
- $dbLocale = "_{$newLocale}";
-
- // Also set tsLocale - CRM-4041
- $tsLocale = $newLocale;
+ if (!empty($this->originalLocale[$apiRequest['id']])) {
+ $this->originalLocale[$apiRequest['id']]->apply();
+ unset($this->originalLocale[$apiRequest['id']]);
}
}