X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FUtils%2FAutoClean.php;h=514283f434676ce7f085001720e22abfe9f3d2b8;hb=1d315b34a5061bd49fd96a4398f953c6da5344b6;hp=1be38b0ece20ac11d069018901a3d69458914395;hpb=50fc670a676282df5aaf49ef983d45a93a82401d;p=civicrm-core.git diff --git a/CRM/Utils/AutoClean.php b/CRM/Utils/AutoClean.php index 1be38b0ece..514283f434 100644 --- a/CRM/Utils/AutoClean.php +++ b/CRM/Utils/AutoClean.php @@ -48,6 +48,26 @@ class CRM_Utils_AutoClean { return $ac; } + /** + * Temporarily set the active locale. Cleanup locale when the autoclean handle disappears. + * + * @param string|null $newLocale + * Ex: 'fr_CA' + * @return \CRM_Utils_AutoClean|null + */ + public static function swapLocale(?string $newLocale) { + $oldLocale = $GLOBALS['tsLocale'] ?? NULL; + if ($oldLocale === $newLocale) { + return NULL; + } + + $i18n = \CRM_Core_I18n::singleton(); + $i18n->setLocale($newLocale); + return static::with(function() use ($i18n, $oldLocale) { + $i18n->setLocale($oldLocale); + }); + } + /** * Temporarily swap values using callback functions, and cleanup * when the current context shuts down.