(civicrm-setup#1) CRM_Core_I18n - Don't require immediate bootstrap
authorTim Otten <totten@civicrm.org>
Fri, 16 Feb 2018 11:45:29 +0000 (03:45 -0800)
committerTim Otten <totten@civicrm.org>
Fri, 16 Feb 2018 11:45:29 +0000 (03:45 -0800)
For civicrm/civicrm-setup#1, the general goal is to allow installing the
database schema without needing to run `GenCode`.

The current draft is crashing because the SQL does translation using `ts()`.
But if you try to use `ts()` in a pre-boot environment, it will attempt to
boot automatically so that it can read `$config->customTranslateFunction.

This is a chicken-egg situation.  We haven't yet reached the phase where the
installer can boot up Civi...  because we don't have the SQL...  but the SQL
can't be generated (translated) because Civi hasn't been booted.

The aim of this patch is to loosen the coupling between `ts()`
and `CRM_Core_Config` so that `ts()` can be used on its own.

> Aside: You might ask how this works today -- basically, `GenCode` does a
> database-less-boot, which placates `ts()`.  However, the `civicrm-setup`
> will eventually need to do full-boot, and AFAIK we don't have any
> situations where one transitions from database-less-boot to full-boot; I
> have a gut fear that such a transition would be its own slipper slope.

CRM/Core/I18n.php

index 9e20536dfe3854345203734dca639356d89a4872..dc434f1f0434976e429858bf5569b56a40d132b0 100644 (file)
@@ -709,8 +709,8 @@ class CRM_Core_I18n {
  *   the translated string
  */
 function ts($text, $params = array()) {
-  static $config = NULL;
-  static $locale = NULL;
+  static $areSettingsAvailable = FALSE;
+  static $lastLocale = NULL;
   static $i18n = NULL;
   static $function = NULL;
 
@@ -718,17 +718,21 @@ function ts($text, $params = array()) {
     return '';
   }
 
-  if (!$config) {
-    $config = CRM_Core_Config::singleton();
+  // When the settings become available, lookup customTranslateFunction.
+  if (!$areSettingsAvailable) {
+    $areSettingsAvailable = (bool) \Civi\Core\Container::getBootService('settings_manager');
+    if ($areSettingsAvailable) {
+      $config = CRM_Core_Config::singleton();
+      if (isset($config->customTranslateFunction) and function_exists($config->customTranslateFunction)) {
+        $function = $config->customTranslateFunction;
+      }
+    }
   }
 
-  $tsLocale = CRM_Core_I18n::getLocale();
-  if (!$i18n or $locale != $tsLocale) {
+  $activeLocale = CRM_Core_I18n::getLocale();
+  if (!$i18n or $lastLocale != $activeLocale) {
     $i18n = CRM_Core_I18n::singleton();
-    $locale = $tsLocale;
-    if (isset($config->customTranslateFunction) and function_exists($config->customTranslateFunction)) {
-      $function = $config->customTranslateFunction;
-    }
+    $lastLocale = $activeLocale;
   }
 
   if ($function) {