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