From 4954910a708f2d7dc210d02f7a265e685c511322 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 16 Feb 2018 03:45:29 -0800 Subject: [PATCH] (civicrm-setup#1) CRM_Core_I18n - Don't require immediate bootstrap 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 | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/CRM/Core/I18n.php b/CRM/Core/I18n.php index 9e20536dfe..dc434f1f04 100644 --- a/CRM/Core/I18n.php +++ b/CRM/Core/I18n.php @@ -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) { -- 2.25.1