translation of extension strings fails after upgrade from 5.52.2 to 5.56
Overview
After upgrading from 5.52.2 to 5.56.0, translations in a message template that is sent by email using a custom invocation of the token code fail to translate.
Other UI translations are properly displayed (e.g. in the angular app).
Example use-case
- We have defined custom message templates in
templates/MessageTemplates/Folder/html.tpl
(see for example: https://code.mayfirst.org/mfmt/outreach/-/blob/master/web/sites/all/extensions/mayfirst/templates/MessageTemplate/MayfirstDuesReminderGrace/html.tpl). - We have defined that path in our
*.mgd.php
file (see https://code.mayfirst.org/mfmt/outreach/-/blob/master/web/sites/all/extensions/mayfirst/mayfirst.mgd.php). - We send the email via custom code. Here's a sample:
$p = new \Civi\Token\TokenProcessor(\Civi::dispatcher(), array(
'controller' => __CLASS__,
// We need smarty for translation.
'smarty' => TRUE,
'schema' => [ 'contactId', 'membershipId' ],
));
// Fill the processor with a batch of data.
$p->addMessage('body_html', $html, 'text/html');
$p->addMessage('subject', $subject, 'text/plain');
// Now we have to create content for each of the contactIds that we
// need to send to. Each message might be different (different first name
// and also it might be in english or spanish).
foreach($recipients as $recipientContactId => $recipient) {
$p->addRow()
->context('contactId', $recipientContactId)
->context('membershipId', $this->membershipId)
->context('contributionId', $this->contributionId)
->context('locale', $recipient['locale']);
}
$p->evaluate();
Full code available here: https://code.mayfirst.org/mfmt/outreach/-/blob/master/web/sites/all/extensions/mayfirst/Civi/Mayfirst/Email.php#L450
- When we send to a contact with locale set to
es_MX
using the UI, it sends the english content. - Interestingly, in our unit test, it translates the strings present in CiviCRM core's translation file, but not the extension's translation file.
Current behaviour
The email is not properly translated.
Proposed behaviour
The email should be translated.
Comments
I traced the change to this commit. Specifically, when I changed these lines, it started working in the UI (but not in the unit tests):
diff --git a/web/sites/all/modules/civicrm/CRM/Core/I18n.php b/web/sites/all/modules/civicrm/CRM/Core/I18n.php
index b1aa48974..4e690fc64 100644
--- a/web/sites/all/modules/civicrm/CRM/Core/I18n.php
+++ b/web/sites/all/modules/civicrm/CRM/Core/I18n.php
@@ -672,18 +672,18 @@ class CRM_Core_I18n {
}
// Change the language of the CMS as well, for URLs.
- CRM_Utils_System::setUFLocale($civicrmLocale->uf);
+ CRM_Utils_System::setUFLocale($locale);
// For sql queries, if running in DB multi-lingual mode.
global $dbLocale;
if ($dbLocale) {
- $dbLocale = '_' . $civicrmLocale->db;
+ $dbLocale = '_' . $locale;
}
// For self::getLocale()
global $tsLocale;
- $tsLocale = $civicrmLocale->ts;
+ $tsLocale = $locale;
CRM_Core_I18n::singleton()->reactivate();
}
The weird part is that $locale
seems to be the same as $civicrmLocale->ts
, $civicrmLocale->db
, and $civicrmLocale->uf
. It's merely accessing the {ts,db,uf} properties of $civicrmLocale
that causes the problem. In other words, if I simply log $civicrmLocale->ts
it fails to translate.
@totten - since the commit is yours I was hoping you might have some insight? I'm available to try things out if you have any suggestions.