Merge pull request #10796 from tschuettler/CRM-20995
[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 165
ece6501c
ML
166 // get labels
167 $rows = array();
168 $labels = array();
169 CRM_Core_OptionValue::getValues(array('name' => 'languages'), $rows);
170 foreach ($rows as $id => $row) {
171 $labels[$row['name']] = $row['label'];
172 }
173
6a488035 174 // check which ones are available; add them to $all if not there already
6a488035 175 $codes = array();
4d8ce7f0 176 if (is_dir(CRM_Core_I18n::getResourceDir()) && $dir = opendir(CRM_Core_I18n::getResourceDir())) {
6a488035
TO
177 while ($filename = readdir($dir)) {
178 if (preg_match('/^[a-z][a-z]_[A-Z][A-Z]$/', $filename)) {
179 $codes[] = $filename;
180 if (!isset($all[$filename])) {
ece6501c 181 $all[$filename] = $labels[$filename];
6a488035
TO
182 }
183 }
184 }
185 closedir($dir);
186 }
187
188 // drop the unavailable languages (except en_US)
189 foreach (array_keys($all) as $code) {
190 if ($code == 'en_US') {
191 continue;
192 }
408b79bf 193 if (!in_array($code, $codes)) {
194 unset($all[$code]);
195 }
6a488035 196 }
ece6501c
ML
197
198 ksort($all);
6a488035
TO
199 }
200
201 if ($enabled === NULL) {
202 $config = CRM_Core_Config::singleton();
203 $enabled = array();
204 if (isset($config->languageLimit) and $config->languageLimit) {
205 foreach ($all as $code => $name) {
206 if (in_array($code, array_keys($config->languageLimit))) {
207 $enabled[$code] = $name;
208 }
209 }
210 }
211 }
212
213 return $justEnabled ? $enabled : $all;
214 }
215
216 /**
217 * Replace arguments in a string with their values. Arguments are represented by % followed by their number.
218 *
5a4f6742
CW
219 * @param string $str
220 * source string.
6a488035 221 *
a6c01b45
CW
222 * @return string
223 * modified string
6a488035 224 */
00be9182 225 public function strarg($str) {
6a488035
TO
226 $tr = array();
227 $p = 0;
228 for ($i = 1; $i < func_num_args(); $i++) {
229 $arg = func_get_arg($i);
230 if (is_array($arg)) {
231 foreach ($arg as $aarg) {
232 $tr['%' . ++$p] = $aarg;
233 }
234 }
235 else {
236 $tr['%' . ++$p] = $arg;
237 }
238 }
239 return strtr($str, $tr);
240 }
241
f2ac86d1 242 /**
243 * Get the directory for l10n resources.
244 *
245 * @return string
246 */
4d8ce7f0
TO
247 public static function getResourceDir() {
248 static $dir = NULL;
249 if ($dir === NULL) {
250 $dir = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'l10n' . DIRECTORY_SEPARATOR;
251 }
252 return $dir;
253 }
254
6a488035
TO
255 /**
256 * Smarty block function, provides gettext support for smarty.
257 *
258 * The block content is the text that should be translated.
259 *
260 * Any parameter that is sent to the function will be represented as %n in the translation text,
261 * where n is 1 for the first parameter. The following parameters are reserved:
262 * - escape - sets escape mode:
263 * - 'html' for HTML escaping, this is the default.
264 * - 'js' for javascript escaping.
265 * - 'no'/'off'/0 - turns off escaping
266 * - plural - The plural version of the text (2nd parameter of ngettext())
267 * - count - The item count for plural mode (3rd parameter of ngettext())
268 * - context - gettext context of that string (for homonym handling)
269 *
5a4f6742
CW
270 * @param string $text
271 * the original string.
272 * @param array $params
3f3bba82
TO
273 * The params of the translation (if any).
274 * - domain: string|array a list of translation domains to search (in order)
275 * - context: string
e3b7fc11 276 *
a6c01b45
CW
277 * @return string
278 * the translated string
6a488035 279 */
00be9182 280 public function crm_translate($text, $params = array()) {
6a488035
TO
281 if (isset($params['escape'])) {
282 $escape = $params['escape'];
283 unset($params['escape']);
284 }
285
286 // sometimes we need to {ts}-tag a string, but don’t want to
287 // translate it in the template (like civicrm_navigation.tpl),
288 // because we handle the translation in a different way (CRM-6998)
289 // in such cases we return early, only doing SQL/JS escaping
290 if (isset($params['skip']) and $params['skip']) {
291 if (isset($escape) and ($escape == 'sql')) {
74032946 292 $text = CRM_Core_DAO::escapeString($text);
6a488035
TO
293 }
294 if (isset($escape) and ($escape == 'js')) {
295 $text = addcslashes($text, "'");
296 }
297 return $text;
298 }
299
3f3bba82 300 $plural = $count = NULL;
6a488035
TO
301 if (isset($params['plural'])) {
302 $plural = $params['plural'];
303 unset($params['plural']);
304 if (isset($params['count'])) {
305 $count = $params['count'];
306 }
307 }
308
309 if (isset($params['context'])) {
310 $context = $params['context'];
311 unset($params['context']);
312 }
313 else {
314 $context = NULL;
315 }
316
3f3bba82
TO
317 if (isset($params['domain'])) {
318 $domain = $params['domain'];
319 unset($params['domain']);
320 }
321 else {
322 $domain = NULL;
323 }
324
1b4710da
TO
325 $raw = !empty($params['raw']);
326 unset($params['raw']);
327
3f3bba82
TO
328 if (!empty($domain)) {
329 // It might be prettier to cast to an array, but this is high-traffic stuff.
330 if (is_array($domain)) {
331 foreach ($domain as $d) {
332 $candidate = $this->crm_translate_raw($text, $d, $count, $plural, $context);
333 if ($candidate != $text) {
334 $text = $candidate;
335 break;
336 }
337 }
338 }
339 else {
340 $text = $this->crm_translate_raw($text, $domain, $count, $plural, $context);
341 }
342 }
343 else {
344 $text = $this->crm_translate_raw($text, NULL, $count, $plural, $context);
345 }
346
347 // replace the numbered %1, %2, etc. params if present
1b4710da 348 if (count($params) && !$raw) {
3f3bba82
TO
349 $text = $this->strarg($text, $params);
350 }
351
352 // escape SQL if we were asked for it
353 if (isset($escape) and ($escape == 'sql')) {
354 $text = CRM_Core_DAO::escapeString($text);
355 }
356
357 // escape for JavaScript (if requested)
358 if (isset($escape) and ($escape == 'js')) {
359 $text = addcslashes($text, "'");
360 }
361
362 return $text;
363 }
364
365 /**
366 * Lookup the raw translation of a string (without any extra escaping or interpolation).
367 *
368 * @param string $text
369 * @param string|NULL $domain
370 * @param int|NULL $count
371 * @param string $plural
372 * @param string $context
e3b7fc11 373 *
374 * @return string
3f3bba82
TO
375 */
376 protected function crm_translate_raw($text, $domain, $count, $plural, $context) {
1a3cba0e
ML
377 // gettext domain for extensions
378 $domain_changed = FALSE;
3f3bba82
TO
379 if (!empty($domain) && $this->_phpgettext) {
380 if ($this->setGettextDomain($domain)) {
1a3cba0e
ML
381 $domain_changed = TRUE;
382 }
383 }
384
6a488035 385 // do all wildcard translations first
a565a9b6 386
7def74fa
TO
387 // FIXME: Is there a constant we can reference instead of hardcoding en_US?
388 $replacementsLocale = $this->locale ? $this->locale : 'en_US';
389 if (!isset(Civi::$statics[__CLASS__]) || !array_key_exists($replacementsLocale, Civi::$statics[__CLASS__])) {
0d096477 390 if (defined('CIVICRM_DSN') && !CRM_Core_Config::isUpgradeMode()) {
7def74fa 391 Civi::$statics[__CLASS__][$replacementsLocale] = CRM_Core_BAO_WordReplacement::getLocaleCustomStrings($replacementsLocale);
234d8f09
TO
392 }
393 else {
7def74fa 394 Civi::$statics[__CLASS__][$replacementsLocale] = array();
234d8f09
TO
395 }
396 }
7def74fa 397 $stringTable = Civi::$statics[__CLASS__][$replacementsLocale];
6a488035
TO
398
399 $exactMatch = FALSE;
400 if (isset($stringTable['enabled']['exactMatch'])) {
401 foreach ($stringTable['enabled']['exactMatch'] as $search => $replace) {
402 if ($search === $text) {
403 $exactMatch = TRUE;
404 $text = $replace;
405 break;
406 }
407 }
408 }
409
6cf5bb6f
DL
410 if (
411 !$exactMatch &&
6a488035
TO
412 isset($stringTable['enabled']['wildcardMatch'])
413 ) {
353ffa53 414 $search = array_keys($stringTable['enabled']['wildcardMatch']);
6a488035 415 $replace = array_values($stringTable['enabled']['wildcardMatch']);
353ffa53 416 $text = str_replace($search, $replace, $text);
6a488035
TO
417 }
418
419 // dont translate if we've done exactMatch already
420 if (!$exactMatch) {
421 // use plural if required parameters are set
422 if (isset($count) && isset($plural)) {
423
424 if ($this->_phpgettext) {
425 $text = $this->_phpgettext->ngettext($text, $plural, $count);
426 }
427 else {
428 // if the locale's not set, we do ngettext work by hand
429 // if $count == 1 then $text = $text, else $text = $plural
430 if ($count != 1) {
431 $text = $plural;
432 }
433 }
434
435 // expand %count in translated string to $count
436 $text = strtr($text, array('%count' => $count));
437
438 // if not plural, but the locale's set, translate
439 }
440 elseif ($this->_phpgettext) {
441 if ($context) {
442 $text = $this->_phpgettext->pgettext($context, $text);
443 }
444 else {
445 $text = $this->_phpgettext->translate($text);
446 }
447 }
448 }
449
1a3cba0e
ML
450 if ($domain_changed) {
451 $this->setGettextDomain('civicrm');
452 }
453
6a488035
TO
454 return $text;
455 }
456
457 /**
458 * Translate a string to the current locale.
459 *
5a4f6742
CW
460 * @param string $string
461 * this string should be translated.
6a488035 462 *
a6c01b45
CW
463 * @return string
464 * the translated string
6a488035 465 */
00be9182 466 public function translate($string) {
6a488035
TO
467 return ($this->_phpgettext) ? $this->_phpgettext->translate($string) : $string;
468 }
469
470 /**
471 * Localize (destructively) array values.
472 *
5a4f6742
CW
473 * @param array $array
474 * the array for localization (in place).
475 * @param array $params
476 * an array of additional parameters.
6a488035 477 */
408b79bf 478 public function localizeArray(
6a488035
TO
479 &$array,
480 $params = array()
481 ) {
98466ff9 482 $tsLocale = CRM_Core_I18n::getLocale();
6a488035
TO
483
484 if ($tsLocale == 'en_US') {
485 return;
486 }
487
488 foreach ($array as & $value) {
489 if ($value) {
490 $value = ts($value, $params);
491 }
492 }
493 }
494
495 /**
496 * Localize (destructively) array elements with keys of 'title'.
497 *
5a4f6742
CW
498 * @param array $array
499 * the array for localization (in place).
6a488035 500 */
00be9182 501 public function localizeTitles(&$array) {
6a488035
TO
502 foreach ($array as $key => $value) {
503 if (is_array($value)) {
504 $this->localizeTitles($value);
505 $array[$key] = $value;
506 }
507 elseif ((string ) $key == 'title') {
eb7d6f39 508 $array[$key] = ts($value, array('context' => 'menu'));
6a488035
TO
509 }
510 }
511 }
512
1a3cba0e
ML
513 /**
514 * Binds a gettext domain, wrapper over bindtextdomain().
515 *
6a0b768e
TO
516 * @param $key
517 * Key of the extension (can be 'civicrm', or 'org.example.foo').
1a3cba0e 518 *
408b79bf 519 * @return Bool
a6c01b45 520 * True if the domain was changed for an extension.
1a3cba0e 521 */
00be9182 522 public function setGettextDomain($key) {
82bcff63 523 /* No domain changes for en_US */
353ffa53 524 if (!$this->_phpgettext) {
82bcff63
ML
525 return FALSE;
526 }
1a3cba0e 527
74eb462f 528 // It's only necessary to find/bind once
353ffa53 529 if (!isset($this->_extensioncache[$key])) {
1a3cba0e
ML
530 try {
531 $mapper = CRM_Extension_System::singleton()->getMapper();
532 $path = $mapper->keyToBasePath($key);
533 $info = $mapper->keyToInfo($key);
534 $domain = $info->file;
535
74eb462f
ML
536 if ($this->_nativegettext) {
537 bindtextdomain($domain, $path . DIRECTORY_SEPARATOR . 'l10n');
538 bind_textdomain_codeset($domain, 'UTF-8');
539 $this->_extensioncache[$key] = $domain;
540 }
541 else {
542 // phpgettext
b52647df 543 $mo_file = $path . DIRECTORY_SEPARATOR . 'l10n' . DIRECTORY_SEPARATOR . $this->locale . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . $domain . '.mo';
74eb462f
ML
544 $streamer = new FileReader($mo_file);
545 $this->_extensioncache[$key] = new gettext_reader($streamer);
546 }
1a3cba0e
ML
547 }
548 catch (CRM_Extension_Exception $e) {
b44e3f84 549 // Intentionally not translating this string to avoid possible infinite loops
74eb462f
ML
550 // Only developers should see this string, if they made a mistake in their ts() usage.
551 CRM_Core_Session::setStatus('Unknown extension key in a translation string: ' . $key, '', 'error');
552 $this->_extensioncache[$key] = FALSE;
1a3cba0e
ML
553 }
554 }
555
74eb462f
ML
556 if (isset($this->_extensioncache[$key]) && $this->_extensioncache[$key]) {
557 if ($this->_nativegettext) {
558 textdomain($this->_extensioncache[$key]);
559 }
560 else {
561 $this->_phpgettext = $this->_extensioncache[$key];
562 }
563
564 return TRUE;
1a3cba0e 565 }
74eb462f
ML
566
567 return FALSE;
1a3cba0e
ML
568 }
569
07844ccf
SV
570
571 /**
572 * Is the CiviCRM in multilingual mode.
573 *
574 * @return Bool
575 * True if CiviCRM is in multilingual mode.
576 */
577 public static function isMultilingual() {
578 $domain = new CRM_Core_DAO_Domain();
579 $domain->find(TRUE);
580 return (bool) $domain->locales;
581 }
582
3a55aa6b
ML
583 /**
584 * Is the language written "right-to-left"?
585 *
586 * @param $language
587 * Language (for example 'en_US', or 'fr_CA').
588 *
589 * @return Bool
590 * True if it is an RTL language.
591 */
592 public static function isLanguageRTL($language) {
593 $rtl = CRM_Core_I18n_PseudoConstant::getRTLlanguages();
594 $short = CRM_Core_I18n_PseudoConstant::shortForLong($language);
595
596 return (in_array($short, $rtl));
597 }
07844ccf
SV
598
599 /**
600 * Change the processing language without changing the current user language
601 *
c86241f4
SV
602 * @param $locale
603 * Locale (for example 'en_US', or 'fr_CA').
07844ccf
SV
604 * True if the domain was changed for an extension.
605 */
c86241f4 606 public function setLocale($locale) {
fd1f3a26
SV
607
608 // Change the language of the CMS as well, for URLs.
c86241f4 609 CRM_Utils_System::setUFLocale($locale);
fd1f3a26
SV
610
611 // change the gettext ressources
07844ccf 612 if ($this->_nativegettext) {
c86241f4 613 $this->setNativeGettextLocale($locale);
07844ccf
SV
614 }
615 else {
616 // phpgettext
c86241f4 617 $this->setPhpGettextLocale($locale);
07844ccf
SV
618 }
619
620 // for sql queries
53d46849 621 global $dbLocale;
c86241f4 622 $dbLocale = "_{$locale}";
07844ccf
SV
623
624 }
625
6a488035
TO
626 /**
627 * Static instance provider - return the instance for the current locale.
c4c311c1
CW
628 *
629 * @return CRM_Core_I18n
6a488035 630 */
00be9182 631 public static function &singleton() {
6a488035
TO
632 static $singleton = array();
633
98466ff9 634 $tsLocale = CRM_Core_I18n::getLocale();
6a488035
TO
635 if (!isset($singleton[$tsLocale])) {
636 $singleton[$tsLocale] = new CRM_Core_I18n($tsLocale);
637 }
638
639 return $singleton[$tsLocale];
640 }
641
642 /**
643 * Set the LC_TIME locale if it's not set already (for a given language choice).
644 *
a6c01b45
CW
645 * @return string
646 * the final LC_TIME that got set
6a488035 647 */
00be9182 648 public static function setLcTime() {
6a488035
TO
649 static $locales = array();
650
98466ff9 651 $tsLocale = CRM_Core_I18n::getLocale();
6a488035
TO
652 if (!isset($locales[$tsLocale])) {
653 // with the config being set to pl_PL: try pl_PL.UTF-8,
654 // then pl_PL, if neither present fall back to C
655 $locales[$tsLocale] = setlocale(LC_TIME, $tsLocale . '.UTF-8', $tsLocale, 'C');
656 }
657
658 return $locales[$tsLocale];
659 }
96025800 660
9747df8a 661 /**
662 * Get the default language for contacts where no language is provided.
663 *
664 * Note that NULL is a valid option so be careful with checking for empty etc.
665 *
666 * NULL would mean 'we don't know & we don't want to hazard a guess'.
667 *
668 * @return string
669 */
670 public static function getContactDefaultLanguage() {
aaffa79f 671 $language = Civi::settings()->get('contact_default_language');
9747df8a 672 if ($language == 'undefined') {
673 return NULL;
674 }
675 if (empty($language) || $language === '*default*') {
676 $language = civicrm_api3('setting', 'getvalue', array(
677 'name' => 'lcMessages',
678 'group' => CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME,
679 ));
680 }
681 elseif ($language == 'current_site_language') {
98466ff9 682 return CRM_Core_I18n::getLocale();
9747df8a 683 }
684
685 return $language;
686 }
687
186e53b3 688 /**
689 * Get the current locale
690 *
691 * @return string
692 */
693 public static function getLocale() {
694 global $tsLocale;
338ff411 695 return $tsLocale ? $tsLocale : 'en_US';
186e53b3 696 }
697
6a488035
TO
698}
699
700/**
701 * Short-named function for string translation, defined in global scope so it's available everywhere.
702 *
e3b7fc11 703 * @param string $text
6a0b768e 704 * String string for translating.
16b10e64 705 * @param array $params
6a0b768e 706 * Array an array of additional parameters.
6a488035 707 *
a6c01b45 708 * @return string
353ffa53 709 * the translated string
6a488035
TO
710 */
711function ts($text, $params = array()) {
712 static $config = NULL;
713 static $locale = NULL;
714 static $i18n = NULL;
715 static $function = NULL;
716
717 if ($text == '') {
718 return '';
719 }
720
721 if (!$config) {
722 $config = CRM_Core_Config::singleton();
723 }
724
98466ff9 725 $tsLocale = CRM_Core_I18n::getLocale();
6a488035
TO
726 if (!$i18n or $locale != $tsLocale) {
727 $i18n = CRM_Core_I18n::singleton();
728 $locale = $tsLocale;
729 if (isset($config->customTranslateFunction) and function_exists($config->customTranslateFunction)) {
730 $function = $config->customTranslateFunction;
731 }
732 }
733
734 if ($function) {
735 return $function($text, $params);
736 }
737 else {
738 return $i18n->crm_translate($text, $params);
739 }
740}