3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
28 namespace Civi\API\Subscriber
;
31 use Symfony\Component\EventDispatcher\EventSubscriberInterface
;
34 * Class I18nSubscriber
35 * @package Civi\API\Subscriber
37 class I18nSubscriber
implements EventSubscriberInterface
{
40 * Used for rolling back language to its original setting after the api call.
44 public $originalLang = [];
49 public static function getSubscribedEvents() {
51 Events
::PREPARE
=> ['onApiPrepare', Events
::W_MIDDLE
],
52 Events
::RESPOND
=> ['onApiRespond', Events
::W_LATE
],
57 * Support multi-lingual requests
59 * @param \Civi\API\Event\Event $event
60 * API preparation event.
62 * @throws \API_Exception
64 public function onApiPrepare(\Civi\API\Event\Event
$event) {
65 $apiRequest = $event->getApiRequest();
67 $params = $apiRequest['params'];
68 if ($apiRequest['version'] < 4) {
69 $language = !empty($params['options']['language']) ?
$params['options']['language'] : \CRM_Utils_Array
::value('option.language', $params);
72 $language = \CRM_Utils_Array
::value('language', $params);
75 $this->setLocale($language, $apiRequest['id']);
80 * Reset language to the default.
82 * @param \Civi\API\Event\Event $event
84 * @throws \API_Exception
86 public function onApiRespond(\Civi\API\Event\Event
$event) {
87 $apiRequest = $event->getApiRequest();
89 if (!empty($this->originalLang
[$apiRequest['id']])) {
92 $tsLocale = $this->originalLang
[$apiRequest['id']]['tsLocale'];
93 $dbLocale = $this->originalLang
[$apiRequest['id']]['dbLocale'];
98 * Sets the tsLocale and dbLocale for multi-lingual sites.
99 * Some code duplication from CRM/Core/BAO/ConfigSetting.php retrieve()
100 * to avoid regressions from refactoring.
101 * @param string $lcMessages
102 * @param int $requestId
103 * @throws \API_Exception
105 public function setLocale($lcMessages, $requestId) {
106 $domain = new \
CRM_Core_DAO_Domain();
107 $domain->id
= \CRM_Core_Config
::domainID();
110 // Check if the site is multi-lingual
111 if ($domain->locales
&& $lcMessages) {
112 // Validate language, otherwise a bad dbLocale could probably lead to sql-injection.
113 if (!array_key_exists($lcMessages, \Civi
::settings()->get('languageLimit'))) {
114 throw new \
API_Exception(ts('Language not enabled: %1', [1 => $lcMessages]));
120 // Store original value to be restored in $this->onApiRespond
121 $this->originalLang
[$requestId] = [
122 'tsLocale' => $tsLocale,
123 'dbLocale' => $dbLocale,
126 // Set suffix for table names - use views if more than one language
127 $dbLocale = "_{$lcMessages}";
129 // Also set tsLocale - CRM-4041
130 $tsLocale = $lcMessages;