Merge pull request #22662 from colemanw/softDeleteEntity
[civicrm-core.git] / CRM / Utils / AutoClean.php
index 1be38b0ece20ac11d069018901a3d69458914395..514283f434676ce7f085001720e22abfe9f3d2b8 100644 (file)
@@ -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.