Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scheduled Reminders - Pass locale through to TokenProcessor #21085

Merged
merged 5 commits into from
Aug 11, 2021

Conversation

totten
Copy link
Member

@totten totten commented Aug 11, 2021

Overview

Update the mechanism by which Scheduled Reminders (ActionSchedule) activate the appropriate locale.

In a subsequent PR, this will allow significant simplification/maintainability improvements for the CRM_*_Tokens adapters.

Before

The ActionSchedule controller calls I18n->setLocale() directly. It then creates a separate TokenProcessor for each recipient and runs the full TokenProcessor routine.

After

The ActionSchedule controller does not call I18n->setLocale() directly. Instead, it passes the locale to the TokenProcessor. At key moments (while handling each recipient/message), it will switch the active locale.

Technical Details

There are a couple ways for a translated string to get into a scheduled-reminder -- most likely by way of Smarty expression ({ts}...{/ts}). There are a couple hook_civicrm_tokenValues consumers in universe which call ts(). At a stretch, it's hypothetically possible that hook_civircm_alterMail might inject a translated string. This patch should give the same outcomes in all those cases.

This can currently be done with `swap()`, but we may be doing this several
times, and `swapLocale()` is a little more pithy and a little more
microoptimal.
There are two likely ways in which a tokenized email winds up with localized strings - either
the Smarty `{ts}...{/ts}` tags define it, or a custom/hook-based tag uses `ts()`.

This ensures that the locale is set in both cases.

It's hypothetically possible that some other `civi.token.eval` listeners
need to use the `row->context['locale']`.  However, I grepped universe and
couldn't find anything that would be affected.  (There were two contrib
listeners for `civi.token.eval` and neither seemed to be affected.)
…le']`

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.
From a core POV, this should be unnecessary.  And I'm not sure that it's a
good idea for contrib to assume that the locale has been set to the email
receipient.  (It probably isn't done that way in some contexts.)

Never-the-less, this should provide greater continuity with the pre-existing
behavior.  If anyone, say, uses `Hook::alterMail()` to append a translated
footer, then it should continue to work in the same way as before.
@civibot
Copy link

civibot bot commented Aug 11, 2021

(Standard links)

@eileenmcnaughton
Copy link
Contributor

This seems straight forward & sensible & the test gives confidence

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants