ActionSchedule - Convert from global `setLocale()` to `$context['locale']`
authorTim Otten <totten@civicrm.org>
Tue, 10 Aug 2021 04:13:57 +0000 (21:13 -0700)
committerTim Otten <totten@civicrm.org>
Tue, 10 Aug 2021 23:49:31 +0000 (16:49 -0700)
Before: Runs `setLocale()` and then executes the entire pipeline for `TokenProcessor`

After: Leaves the global locale alone. Instead, rely on `TokenProcessor` to switch locale
as it visits each recipient.

CRM/Core/BAO/ActionSchedule.php

index abcd747f4448aa416444c9067a36a8e8df98c8cf..00ac176a983e23130a08cfbe1be3cb46e3da2bba 100644 (file)
@@ -268,18 +268,19 @@ FROM civicrm_action_schedule cas
 
       $multilingual = CRM_Core_I18n::isMultilingual();
       while ($dao->fetch()) {
-        // switch language if necessary
-        if ($multilingual) {
-          $preferred_language = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $dao->contactID, 'preferred_language');
-          CRM_Core_I18n::singleton()->setLocale(CRM_Core_BAO_ActionSchedule::pickLocale($actionSchedule->communication_language, $preferred_language));
-        }
-
         $errors = [];
         try {
           $tokenProcessor = self::createTokenProcessor($actionSchedule, $mapping);
-          $tokenProcessor->addRow()
+          $row = $tokenProcessor->addRow()
             ->context('contactId', $dao->contactID)
             ->context('actionSearchResult', (object) $dao->toArray());
+
+          // switch language if necessary
+          if ($multilingual) {
+            $preferred_language = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $dao->contactID, 'preferred_language');
+            $row->context('locale', CRM_Core_BAO_ActionSchedule::pickLocale($actionSchedule->communication_language, $preferred_language));
+          }
+
           foreach ($tokenProcessor->evaluate()->getRows() as $tokenRow) {
             if ($actionSchedule->mode === 'SMS' || $actionSchedule->mode === 'User_Preference') {
               CRM_Utils_Array::extend($errors, self::sendReminderSms($tokenRow, $actionSchedule, $dao->contactID));