Merge pull request #10540 from colemanw/CRM-20091
[civicrm-core.git] / CRM / Core / I18n.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
0f03f337 31 * @copyright CiviCRM LLC (c) 2004-2017
6a488035
TO
32 */
33class CRM_Core_I18n {
34
07844ccf
SV
35 /**
36 * Constants for communication preferences.
37 *
38 * @var int
39 */
40 const NONE = 'none', AUTO = 'auto';
41
42
6a488035 43 /**
2ba175b6
DL
44 * A PHP-gettext instance for string translation;
45 * should stay null if the strings are not to be translated (en_US).
6a488035
TO
46 */
47 private $_phpgettext = NULL;
48
49 /**
50 * Whether we are using native gettext or not.
51 */
52 private $_nativegettext = FALSE;
53
74eb462f
ML
54 /**
55 * Gettext cache for extension domains/streamers, depending on if native or phpgettext.
56 * - native gettext: we cache the value for textdomain()
57 * - phpgettext: we cache the file streamer.
58 */
59 private $_extensioncache = array();
60
0d096477
TO
61 /**
62 * @var string
63 */
64 private $locale;
65
6a488035
TO
66 /**
67 * A locale-based constructor that shouldn't be called from outside of this class (use singleton() instead).
68 *
5a4f6742
CW
69 * @param string $locale
70 * the base of this certain object's existence.
6a488035 71 *
77b97be7 72 * @return \CRM_Core_I18n
6a488035 73 */
00be9182 74 public function __construct($locale) {
0d096477 75 $this->locale = $locale;
6a488035 76 if ($locale != '' and $locale != 'en_US') {
6a488035
TO
77 if (defined('CIVICRM_GETTEXT_NATIVE') && CIVICRM_GETTEXT_NATIVE && function_exists('gettext')) {
78 // Note: the file hierarchy for .po must be, for example: l10n/fr_FR/LC_MESSAGES/civicrm.mo
79
80 $this->_nativegettext = TRUE;
c86241f4 81 $this->setNativeGettextLocale($locale);
6a488035
TO
82 return;
83 }
84
85 // Otherwise, use PHP-gettext
c86241f4 86 $this->setPhpGettextLocale($locale);
6a488035
TO
87 }
88 }
89
90 /**
91 * Returns whether gettext is running natively or using PHP-Gettext.
92 *
a6c01b45
CW
93 * @return bool
94 * True if gettext is native
6a488035 95 */
00be9182 96 public function isNative() {
6a488035
TO
97 return $this->_nativegettext;
98 }
99
f2ac86d1 100 /**
101 * Set native locale for getText.
102 *
103 * @param string $locale
104 */
c86241f4
SV
105 protected function setNativeGettextLocale($locale) {
106
107 $locale .= '.utf8';
108 putenv("LANG=$locale");
109
110 // CRM-11833 Avoid LC_ALL because of LC_NUMERIC and potential DB error.
111 setlocale(LC_TIME, $locale);
112 setlocale(LC_MESSAGES, $locale);
113 setlocale(LC_CTYPE, $locale);
114
115 bindtextdomain('civicrm', CRM_Core_I18n::getResourceDir());
116 bind_textdomain_codeset('civicrm', 'UTF-8');
117 textdomain('civicrm');
118
119 $this->_phpgettext = new CRM_Core_I18n_NativeGettext();
120 $this->_extensioncache['civicrm'] = 'civicrm';
121
122 }
123
f2ac86d1 124 /**
125 * Set getText locale.
126 *
127 * @param string $locale
128 */
c86241f4
SV
129 protected function setPhpGettextLocale($locale) {
130
131 // we support both the old file hierarchy format and the new:
132 // pre-4.5: civicrm/l10n/xx_XX/civicrm.mo
133 // post-4.5: civicrm/l10n/xx_XX/LC_MESSAGES/civicrm.mo
134 require_once 'PHPgettext/streams.php';
135 require_once 'PHPgettext/gettext.php';
136
137 $mo_file = CRM_Core_I18n::getResourceDir() . $locale . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . 'civicrm.mo';
138
139 if (!file_exists($mo_file)) {
140 // fallback to pre-4.5 mode
141 $mo_file = CRM_Core_I18n::getResourceDir() . $locale . DIRECTORY_SEPARATOR . 'civicrm.mo';
142 }
143
144 $streamer = new FileReader($mo_file);
145 $this->_phpgettext = new gettext_reader($streamer);
146 $this->_extensioncache['civicrm'] = $this->_phpgettext;
147
148 }
149
6a488035
TO
150 /**
151 * Return languages available in this instance of CiviCRM.
152 *
5a4f6742
CW
153 * @param bool $justEnabled
154 * whether to return all languages or just the enabled ones.
6a488035 155 *
a6c01b45 156 * @return array
16b10e64 157 * Array of code/language name mappings
6a488035 158 */
00be9182 159 public static function languages($justEnabled = FALSE) {
6a488035
TO
160 static $all = NULL;
161 static $enabled = NULL;
162
163 if (!$all) {
c0c9cd82 164 $all = CRM_Contact_BAO_Contact::buildOptions('preferred_language');
6a488035
TO
165
166 // check which ones are available; add them to $all if not there already
6a488035 167 $codes = array();
4d8ce7f0 168 if (is_dir(CRM_Core_I18n::getResourceDir()) && $dir = opendir(CRM_Core_I18n::getResourceDir())) {
6a488035
TO
169 while ($filename = readdir($dir)) {
170 if (preg_match('/^[a-z][a-z]_[A-Z][A-Z]$/', $filename)) {
171 $codes[] = $filename;
172 if (!isset($all[$filename])) {
173 $all[$filename] = $filename;
174 }
175 }
176 }
177 closedir($dir);
178 }
179
180 // drop the unavailable languages (except en_US)
181 foreach (array_keys($all) as $code) {
182 if ($code == 'en_US') {
183 continue;
184 }
408b79bf 185 if (!in_array($code, $codes)) {
186 unset($all[$code]);
187 }
6a488035
TO
188 }
189 }
190
191 if ($enabled === NULL) {
192 $config = CRM_Core_Config::singleton();
193 $enabled = array();
194 if (isset($config->languageLimit) and $config->languageLimit) {
195 foreach ($all as $code => $name) {
196 if (in_array($code, array_keys($config->languageLimit))) {
197 $enabled[$code] = $name;
198 }
199 }
200 }
201 }
202
203 return $justEnabled ? $enabled : $all;
204 }
205
206 /**
207 * Replace arguments in a string with their values. Arguments are represented by % followed by their number.
208 *
5a4f6742
CW
209 * @param string $str
210 * source string.
6a488035 211 *
a6c01b45
CW
212 * @return string
213 * modified string
6a488035 214 */
00be9182 215 public function strarg($str) {
6a488035
TO
216 $tr = array();
217 $p = 0;
218 for ($i = 1; $i < func_num_args(); $i++) {
219 $arg = func_get_arg($i);
220 if (is_array($arg)) {
221 foreach ($arg as $aarg) {
222 $tr['%' . ++$p] = $aarg;
223 }
224 }
225 else {
226 $tr['%' . ++$p] = $arg;
227 }
228 }
229 return strtr($str, $tr);
230 }
231
f2ac86d1 232 /**
233 * Get the directory for l10n resources.
234 *
235 * @return string
236 */
4d8ce7f0
TO
237 public static function getResourceDir() {
238 static $dir = NULL;
239 if ($dir === NULL) {
240 $dir = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'l10n' . DIRECTORY_SEPARATOR;
241 }
242 return $dir;
243 }
244
6a488035
TO
245 /**
246 * Smarty block function, provides gettext support for smarty.
247 *
248 * The block content is the text that should be translated.
249 *
250 * Any parameter that is sent to the function will be represented as %n in the translation text,
251 * where n is 1 for the first parameter. The following parameters are reserved:
252 * - escape - sets escape mode:
253 * - 'html' for HTML escaping, this is the default.
254 * - 'js' for javascript escaping.
255 * - 'no'/'off'/0 - turns off escaping
256 * - plural - The plural version of the text (2nd parameter of ngettext())
257 * - count - The item count for plural mode (3rd parameter of ngettext())
258 * - context - gettext context of that string (for homonym handling)
259 *
5a4f6742
CW
260 * @param string $text
261 * the original string.
262 * @param array $params
3f3bba82
TO
263 * The params of the translation (if any).
264 * - domain: string|array a list of translation domains to search (in order)
265 * - context: string
e3b7fc11 266 *
a6c01b45
CW
267 * @return string
268 * the translated string
6a488035 269 */
00be9182 270 public function crm_translate($text, $params = array()) {
6a488035
TO
271 if (isset($params['escape'])) {
272 $escape = $params['escape'];
273 unset($params['escape']);
274 }
275
276 // sometimes we need to {ts}-tag a string, but don’t want to
277 // translate it in the template (like civicrm_navigation.tpl),
278 // because we handle the translation in a different way (CRM-6998)
279 // in such cases we return early, only doing SQL/JS escaping
280 if (isset($params['skip']) and $params['skip']) {
281 if (isset($escape) and ($escape == 'sql')) {
74032946 282 $text = CRM_Core_DAO::escapeString($text);
6a488035
TO
283 }
284 if (isset($escape) and ($escape == 'js')) {
285 $text = addcslashes($text, "'");
286 }
287 return $text;
288 }
289
3f3bba82 290 $plural = $count = NULL;
6a488035
TO
291 if (isset($params['plural'])) {
292 $plural = $params['plural'];
293 unset($params['plural']);
294 if (isset($params['count'])) {
295 $count = $params['count'];
296 }
297 }
298
299 if (isset($params['context'])) {
300 $context = $params['context'];
301 unset($params['context']);
302 }
303 else {
304 $context = NULL;
305 }
306
3f3bba82
TO
307 if (isset($params['domain'])) {
308 $domain = $params['domain'];
309 unset($params['domain']);
310 }
311 else {
312 $domain = NULL;
313 }
314
1b4710da
TO
315 $raw = !empty($params['raw']);
316 unset($params['raw']);
317
3f3bba82
TO
318 if (!empty($domain)) {
319 // It might be prettier to cast to an array, but this is high-traffic stuff.
320 if (is_array($domain)) {
321 foreach ($domain as $d) {
322 $candidate = $this->crm_translate_raw($text, $d, $count, $plural, $context);
323 if ($candidate != $text) {
324 $text = $candidate;
325 break;
326 }
327 }
328 }
329 else {
330 $text = $this->crm_translate_raw($text, $domain, $count, $plural, $context);
331 }
332 }
333 else {
334 $text = $this->crm_translate_raw($text, NULL, $count, $plural, $context);
335 }
336
337 // replace the numbered %1, %2, etc. params if present
1b4710da 338 if (count($params) && !$raw) {
3f3bba82
TO
339 $text = $this->strarg($text, $params);
340 }
341
342 // escape SQL if we were asked for it
343 if (isset($escape) and ($escape == 'sql')) {
344 $text = CRM_Core_DAO::escapeString($text);
345 }
346
347 // escape for JavaScript (if requested)
348 if (isset($escape) and ($escape == 'js')) {
349 $text = addcslashes($text, "'");
350 }
351
352 return $text;
353 }
354
355 /**
356 * Lookup the raw translation of a string (without any extra escaping or interpolation).
357 *
358 * @param string $text
359 * @param string|NULL $domain
360 * @param int|NULL $count
361 * @param string $plural
362 * @param string $context
e3b7fc11 363 *
364 * @return string
3f3bba82
TO
365 */
366 protected function crm_translate_raw($text, $domain, $count, $plural, $context) {
1a3cba0e
ML
367 // gettext domain for extensions
368 $domain_changed = FALSE;
3f3bba82
TO
369 if (!empty($domain) && $this->_phpgettext) {
370 if ($this->setGettextDomain($domain)) {
1a3cba0e
ML
371 $domain_changed = TRUE;
372 }
373 }
374
6a488035 375 // do all wildcard translations first
a565a9b6 376
7def74fa
TO
377 // FIXME: Is there a constant we can reference instead of hardcoding en_US?
378 $replacementsLocale = $this->locale ? $this->locale : 'en_US';
379 if (!isset(Civi::$statics[__CLASS__]) || !array_key_exists($replacementsLocale, Civi::$statics[__CLASS__])) {
0d096477 380 if (defined('CIVICRM_DSN') && !CRM_Core_Config::isUpgradeMode()) {
7def74fa 381 Civi::$statics[__CLASS__][$replacementsLocale] = CRM_Core_BAO_WordReplacement::getLocaleCustomStrings($replacementsLocale);
234d8f09
TO
382 }
383 else {
7def74fa 384 Civi::$statics[__CLASS__][$replacementsLocale] = array();
234d8f09
TO
385 }
386 }
7def74fa 387 $stringTable = Civi::$statics[__CLASS__][$replacementsLocale];
6a488035
TO
388
389 $exactMatch = FALSE;
390 if (isset($stringTable['enabled']['exactMatch'])) {
391 foreach ($stringTable['enabled']['exactMatch'] as $search => $replace) {
392 if ($search === $text) {
393 $exactMatch = TRUE;
394 $text = $replace;
395 break;
396 }
397 }
398 }
399
6cf5bb6f
DL
400 if (
401 !$exactMatch &&
6a488035
TO
402 isset($stringTable['enabled']['wildcardMatch'])
403 ) {
353ffa53 404 $search = array_keys($stringTable['enabled']['wildcardMatch']);
6a488035 405 $replace = array_values($stringTable['enabled']['wildcardMatch']);
353ffa53 406 $text = str_replace($search, $replace, $text);
6a488035
TO
407 }
408
409 // dont translate if we've done exactMatch already
410 if (!$exactMatch) {
411 // use plural if required parameters are set
412 if (isset($count) && isset($plural)) {
413
414 if ($this->_phpgettext) {
415 $text = $this->_phpgettext->ngettext($text, $plural, $count);
416 }
417 else {
418 // if the locale's not set, we do ngettext work by hand
419 // if $count == 1 then $text = $text, else $text = $plural
420 if ($count != 1) {
421 $text = $plural;
422 }
423 }
424
425 // expand %count in translated string to $count
426 $text = strtr($text, array('%count' => $count));
427
428 // if not plural, but the locale's set, translate
429 }
430 elseif ($this->_phpgettext) {
431 if ($context) {
432 $text = $this->_phpgettext->pgettext($context, $text);
433 }
434 else {
435 $text = $this->_phpgettext->translate($text);
436 }
437 }
438 }
439
1a3cba0e
ML
440 if ($domain_changed) {
441 $this->setGettextDomain('civicrm');
442 }
443
6a488035
TO
444 return $text;
445 }
446
447 /**
448 * Translate a string to the current locale.
449 *
5a4f6742
CW
450 * @param string $string
451 * this string should be translated.
6a488035 452 *
a6c01b45
CW
453 * @return string
454 * the translated string
6a488035 455 */
00be9182 456 public function translate($string) {
6a488035
TO
457 return ($this->_phpgettext) ? $this->_phpgettext->translate($string) : $string;
458 }
459
460 /**
461 * Localize (destructively) array values.
462 *
5a4f6742
CW
463 * @param array $array
464 * the array for localization (in place).
465 * @param array $params
466 * an array of additional parameters.
6a488035 467 */
408b79bf 468 public function localizeArray(
6a488035
TO
469 &$array,
470 $params = array()
471 ) {
98466ff9 472 $tsLocale = CRM_Core_I18n::getLocale();
6a488035
TO
473
474 if ($tsLocale == 'en_US') {
475 return;
476 }
477
478 foreach ($array as & $value) {
479 if ($value) {
480 $value = ts($value, $params);
481 }
482 }
483 }
484
485 /**
486 * Localize (destructively) array elements with keys of 'title'.
487 *
5a4f6742
CW
488 * @param array $array
489 * the array for localization (in place).
6a488035 490 */
00be9182 491 public function localizeTitles(&$array) {
6a488035
TO
492 foreach ($array as $key => $value) {
493 if (is_array($value)) {
494 $this->localizeTitles($value);
495 $array[$key] = $value;
496 }
497 elseif ((string ) $key == 'title') {
eb7d6f39 498 $array[$key] = ts($value, array('context' => 'menu'));
6a488035
TO
499 }
500 }
501 }
502
1a3cba0e
ML
503 /**
504 * Binds a gettext domain, wrapper over bindtextdomain().
505 *
6a0b768e
TO
506 * @param $key
507 * Key of the extension (can be 'civicrm', or 'org.example.foo').
1a3cba0e 508 *
408b79bf 509 * @return Bool
a6c01b45 510 * True if the domain was changed for an extension.
1a3cba0e 511 */
00be9182 512 public function setGettextDomain($key) {
82bcff63 513 /* No domain changes for en_US */
353ffa53 514 if (!$this->_phpgettext) {
82bcff63
ML
515 return FALSE;
516 }
1a3cba0e 517
74eb462f 518 // It's only necessary to find/bind once
353ffa53 519 if (!isset($this->_extensioncache[$key])) {
1a3cba0e
ML
520 try {
521 $mapper = CRM_Extension_System::singleton()->getMapper();
522 $path = $mapper->keyToBasePath($key);
523 $info = $mapper->keyToInfo($key);
524 $domain = $info->file;
525
74eb462f
ML
526 if ($this->_nativegettext) {
527 bindtextdomain($domain, $path . DIRECTORY_SEPARATOR . 'l10n');
528 bind_textdomain_codeset($domain, 'UTF-8');
529 $this->_extensioncache[$key] = $domain;
530 }
531 else {
532 // phpgettext
b52647df 533 $mo_file = $path . DIRECTORY_SEPARATOR . 'l10n' . DIRECTORY_SEPARATOR . $this->locale . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . $domain . '.mo';
74eb462f
ML
534 $streamer = new FileReader($mo_file);
535 $this->_extensioncache[$key] = new gettext_reader($streamer);
536 }
1a3cba0e
ML
537 }
538 catch (CRM_Extension_Exception $e) {
b44e3f84 539 // Intentionally not translating this string to avoid possible infinite loops
74eb462f
ML
540 // Only developers should see this string, if they made a mistake in their ts() usage.
541 CRM_Core_Session::setStatus('Unknown extension key in a translation string: ' . $key, '', 'error');
542 $this->_extensioncache[$key] = FALSE;
1a3cba0e
ML
543 }
544 }
545
74eb462f
ML
546 if (isset($this->_extensioncache[$key]) && $this->_extensioncache[$key]) {
547 if ($this->_nativegettext) {
548 textdomain($this->_extensioncache[$key]);
549 }
550 else {
551 $this->_phpgettext = $this->_extensioncache[$key];
552 }
553
554 return TRUE;
1a3cba0e 555 }
74eb462f
ML
556
557 return FALSE;
1a3cba0e
ML
558 }
559
07844ccf
SV
560
561 /**
562 * Is the CiviCRM in multilingual mode.
563 *
564 * @return Bool
565 * True if CiviCRM is in multilingual mode.
566 */
567 public static function isMultilingual() {
568 $domain = new CRM_Core_DAO_Domain();
569 $domain->find(TRUE);
570 return (bool) $domain->locales;
571 }
572
3a55aa6b
ML
573 /**
574 * Is the language written "right-to-left"?
575 *
576 * @param $language
577 * Language (for example 'en_US', or 'fr_CA').
578 *
579 * @return Bool
580 * True if it is an RTL language.
581 */
582 public static function isLanguageRTL($language) {
583 $rtl = CRM_Core_I18n_PseudoConstant::getRTLlanguages();
584 $short = CRM_Core_I18n_PseudoConstant::shortForLong($language);
585
586 return (in_array($short, $rtl));
587 }
07844ccf
SV
588
589 /**
590 * Change the processing language without changing the current user language
591 *
c86241f4
SV
592 * @param $locale
593 * Locale (for example 'en_US', or 'fr_CA').
07844ccf
SV
594 * True if the domain was changed for an extension.
595 */
c86241f4 596 public function setLocale($locale) {
fd1f3a26
SV
597
598 // Change the language of the CMS as well, for URLs.
c86241f4 599 CRM_Utils_System::setUFLocale($locale);
fd1f3a26
SV
600
601 // change the gettext ressources
07844ccf 602 if ($this->_nativegettext) {
c86241f4 603 $this->setNativeGettextLocale($locale);
07844ccf
SV
604 }
605 else {
606 // phpgettext
c86241f4 607 $this->setPhpGettextLocale($locale);
07844ccf
SV
608 }
609
610 // for sql queries
53d46849 611 global $dbLocale;
c86241f4 612 $dbLocale = "_{$locale}";
07844ccf
SV
613
614 }
615
6a488035
TO
616 /**
617 * Static instance provider - return the instance for the current locale.
c4c311c1
CW
618 *
619 * @return CRM_Core_I18n
6a488035 620 */
00be9182 621 public static function &singleton() {
6a488035
TO
622 static $singleton = array();
623
98466ff9 624 $tsLocale = CRM_Core_I18n::getLocale();
6a488035
TO
625 if (!isset($singleton[$tsLocale])) {
626 $singleton[$tsLocale] = new CRM_Core_I18n($tsLocale);
627 }
628
629 return $singleton[$tsLocale];
630 }
631
632 /**
633 * Set the LC_TIME locale if it's not set already (for a given language choice).
634 *
a6c01b45
CW
635 * @return string
636 * the final LC_TIME that got set
6a488035 637 */
00be9182 638 public static function setLcTime() {
6a488035
TO
639 static $locales = array();
640
98466ff9 641 $tsLocale = CRM_Core_I18n::getLocale();
6a488035
TO
642 if (!isset($locales[$tsLocale])) {
643 // with the config being set to pl_PL: try pl_PL.UTF-8,
644 // then pl_PL, if neither present fall back to C
645 $locales[$tsLocale] = setlocale(LC_TIME, $tsLocale . '.UTF-8', $tsLocale, 'C');
646 }
647
648 return $locales[$tsLocale];
649 }
96025800 650
9747df8a 651 /**
652 * Get the default language for contacts where no language is provided.
653 *
654 * Note that NULL is a valid option so be careful with checking for empty etc.
655 *
656 * NULL would mean 'we don't know & we don't want to hazard a guess'.
657 *
658 * @return string
659 */
660 public static function getContactDefaultLanguage() {
aaffa79f 661 $language = Civi::settings()->get('contact_default_language');
9747df8a 662 if ($language == 'undefined') {
663 return NULL;
664 }
665 if (empty($language) || $language === '*default*') {
666 $language = civicrm_api3('setting', 'getvalue', array(
667 'name' => 'lcMessages',
668 'group' => CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME,
669 ));
670 }
671 elseif ($language == 'current_site_language') {
98466ff9 672 return CRM_Core_I18n::getLocale();
9747df8a 673 }
674
675 return $language;
676 }
677
186e53b3 678 /**
679 * Get the current locale
680 *
681 * @return string
682 */
683 public static function getLocale() {
684 global $tsLocale;
338ff411 685 return $tsLocale ? $tsLocale : 'en_US';
186e53b3 686 }
687
6a488035
TO
688}
689
690/**
691 * Short-named function for string translation, defined in global scope so it's available everywhere.
692 *
e3b7fc11 693 * @param string $text
6a0b768e 694 * String string for translating.
16b10e64 695 * @param array $params
6a0b768e 696 * Array an array of additional parameters.
6a488035 697 *
a6c01b45 698 * @return string
353ffa53 699 * the translated string
6a488035
TO
700 */
701function ts($text, $params = array()) {
702 static $config = NULL;
703 static $locale = NULL;
704 static $i18n = NULL;
705 static $function = NULL;
706
707 if ($text == '') {
708 return '';
709 }
710
711 if (!$config) {
712 $config = CRM_Core_Config::singleton();
713 }
714
98466ff9 715 $tsLocale = CRM_Core_I18n::getLocale();
6a488035
TO
716 if (!$i18n or $locale != $tsLocale) {
717 $i18n = CRM_Core_I18n::singleton();
718 $locale = $tsLocale;
719 if (isset($config->customTranslateFunction) and function_exists($config->customTranslateFunction)) {
720 $function = $config->customTranslateFunction;
721 }
722 }
723
724 if ($function) {
725 return $function($text, $params);
726 }
727 else {
728 return $i18n->crm_translate($text, $params);
729 }
730}