-
-
Notifications
You must be signed in to change notification settings - Fork 818
/
functions.php
86 lines (78 loc) · 2.45 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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);
}