forked from civicrm/civicrm-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request civicrm#30071 from totten/master-ts
(REF) `ts()` - Move from class-file to `functions.php`
- Loading branch information
Showing
4 changed files
with
88 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,7 @@ | |
"Civi\\": [".", "Civi/", "setup/src/"] | ||
}, | ||
"files": [ | ||
"functions.php", | ||
"guzzle_php81_shim.php" | ||
] | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
/* | ||
+--------------------------------------------------------------------+ | ||
| Copyright CiviCRM LLC. All rights reserved. | | ||
| | | ||
| This work is published under the GNU AGPLv3 license with some | | ||
| permitted exceptions and without any warranty. For full license | | ||
| and copyright information, see https://civicrm.org/licensing | | ||
+--------------------------------------------------------------------+ | ||
*/ | ||
|
||
/** | ||
* @file | ||
* | ||
* CiviCRM is generally organized around classes, but there are a handful of global functions. | ||
* Declare them here. | ||
*/ | ||
|
||
/** | ||
* Short-named function for string translation, defined in global scope so it's available everywhere. | ||
* | ||
* @param string $text | ||
* String for translating. | ||
* Ex: 'Hello, %1!' | ||
* @param array $params | ||
* An array of additional parameters, as per `crm_translate()`. | ||
* Ex: [1 => 'Dave'] | ||
* @return string | ||
* The translated string | ||
* Ex: '¡Buenos días Dave!` | ||
* @see \CRM_Core_I18n::crm_translate() | ||
*/ | ||
function ts($text, $params = []) { | ||
static $bootstrapReady = FALSE; | ||
static $lastLocale = NULL; | ||
static $i18n = NULL; | ||
static $function = NULL; | ||
|
||
if ($text == '') { | ||
return ''; | ||
} | ||
|
||
// When the settings become available, lookup customTranslateFunction. | ||
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 (!empty($config->customTranslateFunction) && function_exists($config->customTranslateFunction)) { | ||
$function = $config->customTranslateFunction; | ||
} | ||
} | ||
} | ||
|
||
$civicrmLocale = CRM_Core_I18n::getLocale(); | ||
if (!$i18n or $lastLocale != $civicrmLocale) { | ||
$i18n = CRM_Core_I18n::singleton(); | ||
$lastLocale = $civicrmLocale; | ||
} | ||
|
||
if ($function) { | ||
return $function($text, $params); | ||
} | ||
else { | ||
return $i18n->crm_translate($text, $params); | ||
} | ||
} | ||
|
||
/** | ||
* Alternate name for `ts()` | ||
* | ||
* This is functionally equivalent to `ts()`. However, regular `ts()` is subject to extra linting | ||
* rules. Using `_ts()` can bypass the linting rules for the rare cases where you really want | ||
* special/dynamic values. | ||
* | ||
* @param array ...$args | ||
* @return string | ||
* @see ts() | ||
* @see \CRM_Core_I18n::crm_translate() | ||
* @internal | ||
*/ | ||
function _ts(...$args) { | ||
$f = 'ts'; | ||
return $f(...$args); | ||
} |