Pass language into msg_admin api calls
authorEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 22 Jun 2022 03:29:27 +0000 (15:29 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 1 Sep 2022 06:51:02 +0000 (18:51 +1200)
Civi/Api4/Action/WorkflowMessage/Render.php
ext/message_admin/ang/crmMsgadm/Edit.js
ext/message_admin/ang/crmMsgadm/Preview.js
tests/phpunit/api/v4/Entity/ConformanceTest.php

index 86611072b9f7170ea52dea0b455096bdfe6a0d56..c645374ba81f9fbc1ad917e2b4bfd633084f3653 100644 (file)
@@ -75,13 +75,24 @@ class Render extends \Civi\Api4\Generic\AbstractAction {
 
   public function _run(\Civi\Api4\Generic\Result $result) {
     $this->validateValues();
-
+    global $moneyFormatLocale;
+    $separatorConfig = \CRM_Utils_Constant::value('IGNORE_SEPARATOR_CONFIG');
+    $originalValue = $moneyFormatLocale;
+
+    if ($this->getTranslationLanguage()) {
+      // Passing in translation language forces money formatting, useful when the
+      // template is previewed before being saved.
+      $moneyFormatLocale = $this->getTranslationLanguage();
+      putenv('IGNORE_SEPARATOR_CONFIG=' . 1);
+    }
     $r = \CRM_Core_BAO_MessageTemplate::renderTemplate([
       'model' => $this->_model,
       'messageTemplate' => $this->getMessageTemplate(),
       'messageTemplateId' => $this->getMessageTemplateId(),
+      'language' => $this->getPreferredLanguage(),
     ]);
-
+    $moneyFormatLocale = $originalValue;
+    putenv('IGNORE_SEPARATOR_CONFIG=' . $separatorConfig);
     $result[] = \CRM_Utils_Array::subset($r, ['subject', 'html', 'text']);
   }
 
index 6e01a76a7a4dbbda4b338035bd55521134712da7..ccba2dfcac75a6da81c79d990726b2ddb8ba876b 100644 (file)
       crmApi4({
         examples: ['ExampleData', 'get', {
           // FIXME: workflow name
+          language: $ctrl.lang,
           where: [["tags", "CONTAINS", "preview"], ["name", "LIKE", "workflow/" + $ctrl.records.main.workflow_name + "/%"]],
           select: ['name', 'title', 'data']
         }],
           format: 'example'
         }]
       }).then(function(resp) {
-        console.log('resp',resp);
         if ((!resp.examples || resp.examples.length === 0) && resp.adhoc) {
           // In the future, if Preview dialog allows editing adhoc examples, then we can show the dialog. But for now, it won't work without explicit examples.
           crmUiAlert({
index 82b994c27d1e1fbe63eeec576c85cd64b52686da..1b202c577d5794193e52c1eb4a861ea6c62f919a 100644 (file)
@@ -1,10 +1,14 @@
 (function(angular, $, _) {
 
-  angular.module('crmMsgadm').controller('MsgtpluiPreviewCtrl', function($scope, crmUiHelp, crmStatus, crmApi4, crmUiAlert, $timeout, $q, dialogService) {
+  angular.module('crmMsgadm').controller('MsgtpluiPreviewCtrl', function($scope, crmUiHelp, crmStatus, crmApi4, crmUiAlert, $timeout, $q, dialogService, $location) {
     var ts = $scope.ts = CRM.ts('crmMsgadm');
     var hs = $scope.hs = crmUiHelp({file: 'CRM/MessageAdmin/crmMsgadm'}); // See: templates/CRM/MessageAdmin/crmMsgadm.hlp
 
     var $ctrl = this, model = $scope.model;
+    var args = $location.search();
+    if (args.lang) {
+      $ctrl.lang = args.lang;
+    }
 
     $ctrl.exampleId = parseInt(_.findKey(model.examples, {name: model.exampleName}));
     $ctrl.revisionId = parseInt(_.findKey(model.revisions, {name: model.revisionName}));
@@ -35,6 +39,7 @@
       dlgModel.refresh = function(){
         return crmApi4('ExampleData', 'get', {
           where: [["name", "=", model.examples[$ctrl.exampleId].name]],
+          language: $ctrl.lang,
           select: ['name', 'file', 'title', 'data']
         }).then(function(response){
           dlgModel.title = ts('Example: %1', {1: response[0].title || response[0].name});
@@ -64,6 +69,7 @@
     function requestStoredExample() {
       return crmApi4('ExampleData', 'get', {
         where: [["name", "=", model.examples[$ctrl.exampleId].name]],
+        language: $ctrl.lang,
         select: ['data']
       }).then(function(response) {
         return response[0].data;
@@ -82,6 +88,8 @@
       rendering.then(function(exampleData) {
         var filteredData = model.filterData ? model.filterData(exampleData) : exampleData;
         return crmApi4('WorkflowMessage', 'render', {
+          language: $ctrl.lang,
+          translationLanguage: $ctrl.lang,
           workflow: filteredData.workflow,
           values: filteredData.modelProps,
           messageTemplate: model.revisions[$ctrl.revisionId].rec
index f7b43a04cdf158efdf4b6407d95a9f7661e633f6..773012b4f577125ebf0fb3ae4d7bf26bfaf90b70 100644 (file)
@@ -67,6 +67,7 @@ class ConformanceTest extends Api4TestBase implements HookInterface {
       'civicrm_participant',
       'civicrm_batch',
       'civicrm_product',
+      'civicrm_translation',
     ];
     $this->cleanup(['tablesToTruncate' => $tablesToTruncate]);
     parent::tearDown();