Skip to content

Commit

Permalink
Merge pull request civicrm#30071 from totten/master-ts
Browse files Browse the repository at this point in the history
(REF) `ts()` - Move from class-file to `functions.php`
  • Loading branch information
totten authored Apr 29, 2024
2 parents 57b4214 + b09cee8 commit c439fc0
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 69 deletions.
68 changes: 0 additions & 68 deletions CRM/Core/I18n.php
Original file line number Diff line number Diff line change
Expand Up @@ -792,71 +792,3 @@ private function getWordReplacements() {
}

}

/**
* 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);
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"Civi\\": [".", "Civi/", "setup/src/"]
},
"files": [
"functions.php",
"guzzle_php81_shim.php"
]
},
Expand Down
2 changes: 1 addition & 1 deletion distmaker/dists/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function dm_install_core() {
done

dm_install_files "$repo" "$to" {agpl-3.0,agpl-3.0.exception,gpl}.txt
dm_install_files "$repo" "$to" composer.json composer.lock package.json Civi.php README.md release-notes.md extension-compatibility.json deleted-files-list.json guzzle_php81_shim.php
dm_install_files "$repo" "$to" composer.json composer.lock package.json Civi.php functions.php README.md release-notes.md extension-compatibility.json deleted-files-list.json guzzle_php81_shim.php

mkdir -p "$to/sql"
pushd "$repo" >> /dev/null
Expand Down
86 changes: 86 additions & 0 deletions functions.php
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);
}

0 comments on commit c439fc0

Please sign in to comment.