Support api3 & 4 language syntax & test
[civicrm-core.git] / Civi / API / Subscriber / I18nSubscriber.php
index 1446e242f89ee8786d1ccd2a9e1bb7aa6e910ca5..5b60b1eb7075ecf57f364ead06b64ab2c54fc854 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 5                                                  |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2018                                |
+ | Copyright CiviCRM LLC (c) 2004-2019                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -35,16 +35,19 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  * @package Civi\API\Subscriber
  */
 class I18nSubscriber implements EventSubscriberInterface {
+
   /**
    * @return array
    */
   public static function getSubscribedEvents() {
-    return array(
-      Events::PREPARE => array('onApiPrepare', Events::W_MIDDLE),
-    );
+    return [
+      Events::PREPARE => ['onApiPrepare', Events::W_MIDDLE],
+    ];
   }
 
   /**
+   * Support multi-lingual requests
+   *
    * @param \Civi\API\Event\Event $event
    *   API preparation event.
    *
@@ -53,8 +56,14 @@ class I18nSubscriber implements EventSubscriberInterface {
   public function onApiPrepare(\Civi\API\Event\Event $event) {
     $apiRequest = $event->getApiRequest();
 
-    // support multi-lingual requests
-    if ($language = \CRM_Utils_Array::value('option.language', $apiRequest['params'])) {
+    $params = $apiRequest['params'];
+    if ($apiRequest['version'] < 4) {
+      $language = !empty($params['options']['language']) ? $params['options']['language'] : \CRM_Utils_Array::value('option.language', $params);
+    }
+    else {
+      $language = \CRM_Utils_Array::value('language', $params);
+    }
+    if ($language) {
       $this->setLocale($language);
     }
   }
@@ -80,7 +89,7 @@ class I18nSubscriber implements EventSubscriberInterface {
     // on multi-lang sites based on request and civicrm_uf_match
     if ($multiLang) {
       $config = \CRM_Core_Config::singleton();
-      $languageLimit = array();
+      $languageLimit = [];
       if (isset($config->languageLimit) and $config->languageLimit) {
         $languageLimit = $config->languageLimit;
       }
@@ -89,7 +98,7 @@ class I18nSubscriber implements EventSubscriberInterface {
         $lcMessages = $lcMessagesRequest;
       }
       else {
-        throw new \API_Exception(ts('Language not enabled: %1', array(1 => $lcMessagesRequest)));
+        throw new \API_Exception(ts('Language not enabled: %1', [1 => $lcMessagesRequest]));
       }
     }