Merge pull request #15434 from civicrm/5.19
[civicrm-core.git] / CRM / Core / I18n.php
index 217aec7397cd25876db3a0e76cbae3f6e39b8280..02557bbb7b698701edcb57454cfa247ea0c240e1 100644 (file)
@@ -40,7 +40,7 @@ class CRM_Core_I18n {
   const NONE = 'none', AUTO = 'auto';
 
   /**
-   * @var callable|NULL
+   * @var callable|null
    *   A callback function which handles SQL string encoding.
    *   Set NULL to use the default, CRM_Core_DAO::escapeString().
    *   This is used by `ts(..., [escape=>sql])`.
@@ -230,11 +230,11 @@ class CRM_Core_I18n {
     }
 
     if ($enabled === NULL) {
-      $config = CRM_Core_Config::singleton();
+      $languageLimit = Civi::settings()->get('languageLimit');
       $enabled = [];
-      if (isset($config->languageLimit) and $config->languageLimit) {
+      if ($languageLimit) {
         foreach ($all as $code => $name) {
-          if (in_array($code, array_keys($config->languageLimit))) {
+          if (array_key_exists($code, $languageLimit)) {
             $enabled[$code] = $name;
           }
         }
@@ -326,6 +326,7 @@ class CRM_Core_I18n {
    *   The params of the translation (if any).
    *   - domain: string|array a list of translation domains to search (in order)
    *   - context: string
+   *   - skip_translation: flag (do only escape/replacement, skip the actual translation)
    *
    * @return string
    *   the translated string
@@ -378,24 +379,26 @@ class CRM_Core_I18n {
     $raw = !empty($params['raw']);
     unset($params['raw']);
 
-    if (!empty($domain)) {
-      // It might be prettier to cast to an array, but this is high-traffic stuff.
-      if (is_array($domain)) {
-        foreach ($domain as $d) {
-          $candidate = $this->crm_translate_raw($text, $d, $count, $plural, $context);
-          if ($candidate != $text) {
-            $text = $candidate;
-            break;
+    if (!isset($params['skip_translation'])) {
+      if (!empty($domain)) {
+        // It might be prettier to cast to an array, but this is high-traffic stuff.
+        if (is_array($domain)) {
+          foreach ($domain as $d) {
+            $candidate = $this->crm_translate_raw($text, $d, $count, $plural, $context);
+            if ($candidate != $text) {
+              $text = $candidate;
+              break;
+            }
           }
         }
+        else {
+          $text = $this->crm_translate_raw($text, $domain, $count, $plural, $context);
+        }
       }
       else {
-        $text = $this->crm_translate_raw($text, $domain, $count, $plural, $context);
+        $text = $this->crm_translate_raw($text, NULL, $count, $plural, $context);
       }
     }
-    else {
-      $text = $this->crm_translate_raw($text, NULL, $count, $plural, $context);
-    }
 
     // replace the numbered %1, %2, etc. params if present
     if (count($params) && !$raw) {
@@ -419,8 +422,8 @@ class CRM_Core_I18n {
    * Lookup the raw translation of a string (without any extra escaping or interpolation).
    *
    * @param string $text
-   * @param string|NULL $domain
-   * @param int|NULL $count
+   * @param string|null $domain
+   * @param int|null $count
    * @param string $plural
    * @param string $context
    *
@@ -768,7 +771,7 @@ class CRM_Core_I18n {
  *   the translated string
  */
 function ts($text, $params = []) {
-  static $areSettingsAvailable = FALSE;
+  static $bootstrapReady = FALSE;
   static $lastLocale = NULL;
   static $i18n = NULL;
   static $function = NULL;
@@ -778,14 +781,19 @@ function ts($text, $params = []) {
   }
 
   // When the settings become available, lookup customTranslateFunction.
-  if (!$areSettingsAvailable) {
-    $areSettingsAvailable = (bool) \Civi\Core\Container::getBootService('settings_manager');
-    if ($areSettingsAvailable) {
+  if (!$bootstrapReady) {
+    $bootstrapReady = (bool) \Civi\Core\Container::isContainerBooted();
+    if ($bootstrapReady) {
+      // just got ready: determine whether there is a working custom translation function
       $config = CRM_Core_Config::singleton();
       if (isset($config->customTranslateFunction) and function_exists($config->customTranslateFunction)) {
         $function = $config->customTranslateFunction;
       }
     }
+    else {
+      // don't _translate_ anything until bootstrap has progressed enough
+      $params['skip_translation'] = 1;
+    }
   }
 
   $activeLocale = CRM_Core_I18n::getLocale();