Skip to content

Commit

Permalink
(civicrm-setup#1) CRM_Core_I18n - Don't require immediate bootstrap
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
totten committed Feb 16, 2018
1 parent dac3ef1 commit 4954910
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions CRM/Core/I18n.php
Original file line number Diff line number Diff line change
Expand Up @@ -709,26 +709,30 @@ public static function getLocale() {
* 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;

if ($text == '') {
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) {
Expand Down

0 comments on commit 4954910

Please sign in to comment.