| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 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-2015 |
| 32 | */ |
| 33 | class CRM_Core_I18n { |
| 34 | |
| 35 | /** |
| 36 | * A PHP-gettext instance for string translation; |
| 37 | * should stay null if the strings are not to be translated (en_US). |
| 38 | */ |
| 39 | private $_phpgettext = NULL; |
| 40 | |
| 41 | /** |
| 42 | * Whether we are using native gettext or not. |
| 43 | */ |
| 44 | private $_nativegettext = FALSE; |
| 45 | |
| 46 | /** |
| 47 | * Gettext cache for extension domains/streamers, depending on if native or phpgettext. |
| 48 | * - native gettext: we cache the value for textdomain() |
| 49 | * - phpgettext: we cache the file streamer. |
| 50 | */ |
| 51 | private $_extensioncache = array(); |
| 52 | |
| 53 | /** |
| 54 | * A locale-based constructor that shouldn't be called from outside of this class (use singleton() instead). |
| 55 | * |
| 56 | * @param string $locale |
| 57 | * the base of this certain object's existence. |
| 58 | * |
| 59 | * @return \CRM_Core_I18n |
| 60 | */ |
| 61 | public function __construct($locale) { |
| 62 | if ($locale != '' and $locale != 'en_US') { |
| 63 | $config = CRM_Core_Config::singleton(); |
| 64 | |
| 65 | if (defined('CIVICRM_GETTEXT_NATIVE') && CIVICRM_GETTEXT_NATIVE && function_exists('gettext')) { |
| 66 | // Note: the file hierarchy for .po must be, for example: l10n/fr_FR/LC_MESSAGES/civicrm.mo |
| 67 | |
| 68 | $this->_nativegettext = TRUE; |
| 69 | |
| 70 | $locale .= '.utf8'; |
| 71 | putenv("LANG=$locale"); |
| 72 | |
| 73 | // CRM-11833 Avoid LC_ALL because of LC_NUMERIC and potential DB error. |
| 74 | setlocale(LC_TIME, $locale); |
| 75 | setlocale(LC_MESSAGES, $locale); |
| 76 | setlocale(LC_CTYPE, $locale); |
| 77 | |
| 78 | bindtextdomain('civicrm', CRM_Core_I18n::getResourceDir()); |
| 79 | bind_textdomain_codeset('civicrm', 'UTF-8'); |
| 80 | textdomain('civicrm'); |
| 81 | |
| 82 | $this->_phpgettext = new CRM_Core_I18n_NativeGettext(); |
| 83 | $this->_extensioncache['civicrm'] = 'civicrm'; |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | // Otherwise, use PHP-gettext |
| 88 | // we support both the old file hierarchy format and the new: |
| 89 | // pre-4.5: civicrm/l10n/xx_XX/civicrm.mo |
| 90 | // post-4.5: civicrm/l10n/xx_XX/LC_MESSAGES/civicrm.mo |
| 91 | require_once 'PHPgettext/streams.php'; |
| 92 | require_once 'PHPgettext/gettext.php'; |
| 93 | |
| 94 | $mo_file = CRM_Core_I18n::getResourceDir() . $locale . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . 'civicrm.mo'; |
| 95 | |
| 96 | if (!file_exists($mo_file)) { |
| 97 | // fallback to pre-4.5 mode |
| 98 | $mo_file = CRM_Core_I18n::getResourceDir() . $locale . DIRECTORY_SEPARATOR . 'civicrm.mo'; |
| 99 | } |
| 100 | |
| 101 | $streamer = new FileReader($mo_file); |
| 102 | $this->_phpgettext = new gettext_reader($streamer); |
| 103 | $this->_extensioncache['civicrm'] = $this->_phpgettext; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Returns whether gettext is running natively or using PHP-Gettext. |
| 109 | * |
| 110 | * @return bool |
| 111 | * True if gettext is native |
| 112 | */ |
| 113 | public function isNative() { |
| 114 | return $this->_nativegettext; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Return languages available in this instance of CiviCRM. |
| 119 | * |
| 120 | * @param bool $justEnabled |
| 121 | * whether to return all languages or just the enabled ones. |
| 122 | * |
| 123 | * @return array |
| 124 | * Array of code/language name mappings |
| 125 | */ |
| 126 | public static function languages($justEnabled = FALSE) { |
| 127 | static $all = NULL; |
| 128 | static $enabled = NULL; |
| 129 | |
| 130 | if (!$all) { |
| 131 | $all = CRM_Contact_BAO_Contact::buildOptions('preferred_language'); |
| 132 | |
| 133 | // check which ones are available; add them to $all if not there already |
| 134 | $config = CRM_Core_Config::singleton(); |
| 135 | $codes = array(); |
| 136 | if (is_dir(CRM_Core_I18n::getResourceDir()) && $dir = opendir(CRM_Core_I18n::getResourceDir())) { |
| 137 | while ($filename = readdir($dir)) { |
| 138 | if (preg_match('/^[a-z][a-z]_[A-Z][A-Z]$/', $filename)) { |
| 139 | $codes[] = $filename; |
| 140 | if (!isset($all[$filename])) { |
| 141 | $all[$filename] = $filename; |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | closedir($dir); |
| 146 | } |
| 147 | |
| 148 | // drop the unavailable languages (except en_US) |
| 149 | foreach (array_keys($all) as $code) { |
| 150 | if ($code == 'en_US') { |
| 151 | continue; |
| 152 | } |
| 153 | if (!in_array($code, $codes)) { |
| 154 | unset($all[$code]); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | if ($enabled === NULL) { |
| 160 | $config = CRM_Core_Config::singleton(); |
| 161 | $enabled = array(); |
| 162 | if (isset($config->languageLimit) and $config->languageLimit) { |
| 163 | foreach ($all as $code => $name) { |
| 164 | if (in_array($code, array_keys($config->languageLimit))) { |
| 165 | $enabled[$code] = $name; |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | return $justEnabled ? $enabled : $all; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Replace arguments in a string with their values. Arguments are represented by % followed by their number. |
| 176 | * |
| 177 | * @param string $str |
| 178 | * source string. |
| 179 | * |
| 180 | * @return string |
| 181 | * modified string |
| 182 | */ |
| 183 | public function strarg($str) { |
| 184 | $tr = array(); |
| 185 | $p = 0; |
| 186 | for ($i = 1; $i < func_num_args(); $i++) { |
| 187 | $arg = func_get_arg($i); |
| 188 | if (is_array($arg)) { |
| 189 | foreach ($arg as $aarg) { |
| 190 | $tr['%' . ++$p] = $aarg; |
| 191 | } |
| 192 | } |
| 193 | else { |
| 194 | $tr['%' . ++$p] = $arg; |
| 195 | } |
| 196 | } |
| 197 | return strtr($str, $tr); |
| 198 | } |
| 199 | |
| 200 | public static function getResourceDir() { |
| 201 | static $dir = NULL; |
| 202 | if ($dir === NULL) { |
| 203 | $dir = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'l10n' . DIRECTORY_SEPARATOR; |
| 204 | } |
| 205 | return $dir; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Smarty block function, provides gettext support for smarty. |
| 210 | * |
| 211 | * The block content is the text that should be translated. |
| 212 | * |
| 213 | * Any parameter that is sent to the function will be represented as %n in the translation text, |
| 214 | * where n is 1 for the first parameter. The following parameters are reserved: |
| 215 | * - escape - sets escape mode: |
| 216 | * - 'html' for HTML escaping, this is the default. |
| 217 | * - 'js' for javascript escaping. |
| 218 | * - 'no'/'off'/0 - turns off escaping |
| 219 | * - plural - The plural version of the text (2nd parameter of ngettext()) |
| 220 | * - count - The item count for plural mode (3rd parameter of ngettext()) |
| 221 | * - context - gettext context of that string (for homonym handling) |
| 222 | * |
| 223 | * @param string $text |
| 224 | * the original string. |
| 225 | * @param array $params |
| 226 | * The params of the translation (if any). |
| 227 | * - domain: string|array a list of translation domains to search (in order) |
| 228 | * - context: string |
| 229 | * |
| 230 | * @return string |
| 231 | * the translated string |
| 232 | */ |
| 233 | public function crm_translate($text, $params = array()) { |
| 234 | if (isset($params['escape'])) { |
| 235 | $escape = $params['escape']; |
| 236 | unset($params['escape']); |
| 237 | } |
| 238 | |
| 239 | // sometimes we need to {ts}-tag a string, but don’t want to |
| 240 | // translate it in the template (like civicrm_navigation.tpl), |
| 241 | // because we handle the translation in a different way (CRM-6998) |
| 242 | // in such cases we return early, only doing SQL/JS escaping |
| 243 | if (isset($params['skip']) and $params['skip']) { |
| 244 | if (isset($escape) and ($escape == 'sql')) { |
| 245 | $text = CRM_Core_DAO::escapeString($text); |
| 246 | } |
| 247 | if (isset($escape) and ($escape == 'js')) { |
| 248 | $text = addcslashes($text, "'"); |
| 249 | } |
| 250 | return $text; |
| 251 | } |
| 252 | |
| 253 | $plural = $count = NULL; |
| 254 | if (isset($params['plural'])) { |
| 255 | $plural = $params['plural']; |
| 256 | unset($params['plural']); |
| 257 | if (isset($params['count'])) { |
| 258 | $count = $params['count']; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | if (isset($params['context'])) { |
| 263 | $context = $params['context']; |
| 264 | unset($params['context']); |
| 265 | } |
| 266 | else { |
| 267 | $context = NULL; |
| 268 | } |
| 269 | |
| 270 | if (isset($params['domain'])) { |
| 271 | $domain = $params['domain']; |
| 272 | unset($params['domain']); |
| 273 | } |
| 274 | else { |
| 275 | $domain = NULL; |
| 276 | } |
| 277 | |
| 278 | $raw = !empty($params['raw']); |
| 279 | unset($params['raw']); |
| 280 | |
| 281 | if (!empty($domain)) { |
| 282 | // It might be prettier to cast to an array, but this is high-traffic stuff. |
| 283 | if (is_array($domain)) { |
| 284 | foreach ($domain as $d) { |
| 285 | $candidate = $this->crm_translate_raw($text, $d, $count, $plural, $context); |
| 286 | if ($candidate != $text) { |
| 287 | $text = $candidate; |
| 288 | break; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | else { |
| 293 | $text = $this->crm_translate_raw($text, $domain, $count, $plural, $context); |
| 294 | } |
| 295 | } |
| 296 | else { |
| 297 | $text = $this->crm_translate_raw($text, NULL, $count, $plural, $context); |
| 298 | } |
| 299 | |
| 300 | // replace the numbered %1, %2, etc. params if present |
| 301 | if (count($params) && !$raw) { |
| 302 | $text = $this->strarg($text, $params); |
| 303 | } |
| 304 | |
| 305 | // escape SQL if we were asked for it |
| 306 | if (isset($escape) and ($escape == 'sql')) { |
| 307 | $text = CRM_Core_DAO::escapeString($text); |
| 308 | } |
| 309 | |
| 310 | // escape for JavaScript (if requested) |
| 311 | if (isset($escape) and ($escape == 'js')) { |
| 312 | $text = addcslashes($text, "'"); |
| 313 | } |
| 314 | |
| 315 | return $text; |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Lookup the raw translation of a string (without any extra escaping or interpolation). |
| 320 | * |
| 321 | * @param string $text |
| 322 | * @param string|NULL $domain |
| 323 | * @param int|NULL $count |
| 324 | * @param string $plural |
| 325 | * @param string $context |
| 326 | * |
| 327 | * @return string |
| 328 | */ |
| 329 | protected function crm_translate_raw($text, $domain, $count, $plural, $context) { |
| 330 | // gettext domain for extensions |
| 331 | $domain_changed = FALSE; |
| 332 | if (!empty($domain) && $this->_phpgettext) { |
| 333 | if ($this->setGettextDomain($domain)) { |
| 334 | $domain_changed = TRUE; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | // do all wildcard translations first |
| 339 | $config = CRM_Core_Config::singleton(); |
| 340 | $stringTable = CRM_Utils_Array::value( |
| 341 | $config->lcMessages, |
| 342 | $config->localeCustomStrings |
| 343 | ); |
| 344 | |
| 345 | $exactMatch = FALSE; |
| 346 | if (isset($stringTable['enabled']['exactMatch'])) { |
| 347 | foreach ($stringTable['enabled']['exactMatch'] as $search => $replace) { |
| 348 | if ($search === $text) { |
| 349 | $exactMatch = TRUE; |
| 350 | $text = $replace; |
| 351 | break; |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | if ( |
| 357 | !$exactMatch && |
| 358 | isset($stringTable['enabled']['wildcardMatch']) |
| 359 | ) { |
| 360 | $search = array_keys($stringTable['enabled']['wildcardMatch']); |
| 361 | $replace = array_values($stringTable['enabled']['wildcardMatch']); |
| 362 | $text = str_replace($search, $replace, $text); |
| 363 | } |
| 364 | |
| 365 | // dont translate if we've done exactMatch already |
| 366 | if (!$exactMatch) { |
| 367 | // use plural if required parameters are set |
| 368 | if (isset($count) && isset($plural)) { |
| 369 | |
| 370 | if ($this->_phpgettext) { |
| 371 | $text = $this->_phpgettext->ngettext($text, $plural, $count); |
| 372 | } |
| 373 | else { |
| 374 | // if the locale's not set, we do ngettext work by hand |
| 375 | // if $count == 1 then $text = $text, else $text = $plural |
| 376 | if ($count != 1) { |
| 377 | $text = $plural; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | // expand %count in translated string to $count |
| 382 | $text = strtr($text, array('%count' => $count)); |
| 383 | |
| 384 | // if not plural, but the locale's set, translate |
| 385 | } |
| 386 | elseif ($this->_phpgettext) { |
| 387 | if ($context) { |
| 388 | $text = $this->_phpgettext->pgettext($context, $text); |
| 389 | } |
| 390 | else { |
| 391 | $text = $this->_phpgettext->translate($text); |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | if ($domain_changed) { |
| 397 | $this->setGettextDomain('civicrm'); |
| 398 | } |
| 399 | |
| 400 | return $text; |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * Translate a string to the current locale. |
| 405 | * |
| 406 | * @param string $string |
| 407 | * this string should be translated. |
| 408 | * |
| 409 | * @return string |
| 410 | * the translated string |
| 411 | */ |
| 412 | public function translate($string) { |
| 413 | return ($this->_phpgettext) ? $this->_phpgettext->translate($string) : $string; |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Localize (destructively) array values. |
| 418 | * |
| 419 | * @param array $array |
| 420 | * the array for localization (in place). |
| 421 | * @param array $params |
| 422 | * an array of additional parameters. |
| 423 | */ |
| 424 | public function localizeArray( |
| 425 | &$array, |
| 426 | $params = array() |
| 427 | ) { |
| 428 | global $tsLocale; |
| 429 | |
| 430 | if ($tsLocale == 'en_US') { |
| 431 | return; |
| 432 | } |
| 433 | |
| 434 | foreach ($array as & $value) { |
| 435 | if ($value) { |
| 436 | $value = ts($value, $params); |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * Localize (destructively) array elements with keys of 'title'. |
| 443 | * |
| 444 | * @param array $array |
| 445 | * the array for localization (in place). |
| 446 | */ |
| 447 | public function localizeTitles(&$array) { |
| 448 | foreach ($array as $key => $value) { |
| 449 | if (is_array($value)) { |
| 450 | $this->localizeTitles($value); |
| 451 | $array[$key] = $value; |
| 452 | } |
| 453 | elseif ((string ) $key == 'title') { |
| 454 | $array[$key] = ts($value, array('context' => 'menu')); |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * Binds a gettext domain, wrapper over bindtextdomain(). |
| 461 | * |
| 462 | * @param $key |
| 463 | * Key of the extension (can be 'civicrm', or 'org.example.foo'). |
| 464 | * |
| 465 | * @return Bool |
| 466 | * True if the domain was changed for an extension. |
| 467 | */ |
| 468 | public function setGettextDomain($key) { |
| 469 | /* No domain changes for en_US */ |
| 470 | if (!$this->_phpgettext) { |
| 471 | return FALSE; |
| 472 | } |
| 473 | |
| 474 | // It's only necessary to find/bind once |
| 475 | if (!isset($this->_extensioncache[$key])) { |
| 476 | $config = CRM_Core_Config::singleton(); |
| 477 | |
| 478 | try { |
| 479 | $mapper = CRM_Extension_System::singleton()->getMapper(); |
| 480 | $path = $mapper->keyToBasePath($key); |
| 481 | $info = $mapper->keyToInfo($key); |
| 482 | $domain = $info->file; |
| 483 | |
| 484 | if ($this->_nativegettext) { |
| 485 | bindtextdomain($domain, $path . DIRECTORY_SEPARATOR . 'l10n'); |
| 486 | bind_textdomain_codeset($domain, 'UTF-8'); |
| 487 | $this->_extensioncache[$key] = $domain; |
| 488 | } |
| 489 | else { |
| 490 | // phpgettext |
| 491 | $mo_file = $path . DIRECTORY_SEPARATOR . 'l10n' . DIRECTORY_SEPARATOR . $config->lcMessages . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . $domain . '.mo'; |
| 492 | $streamer = new FileReader($mo_file); |
| 493 | $this->_extensioncache[$key] = new gettext_reader($streamer); |
| 494 | } |
| 495 | } |
| 496 | catch (CRM_Extension_Exception $e) { |
| 497 | // Intentionally not translating this string to avoid possible infinite loops |
| 498 | // Only developers should see this string, if they made a mistake in their ts() usage. |
| 499 | CRM_Core_Session::setStatus('Unknown extension key in a translation string: ' . $key, '', 'error'); |
| 500 | $this->_extensioncache[$key] = FALSE; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | if (isset($this->_extensioncache[$key]) && $this->_extensioncache[$key]) { |
| 505 | if ($this->_nativegettext) { |
| 506 | textdomain($this->_extensioncache[$key]); |
| 507 | } |
| 508 | else { |
| 509 | $this->_phpgettext = $this->_extensioncache[$key]; |
| 510 | } |
| 511 | |
| 512 | return TRUE; |
| 513 | } |
| 514 | |
| 515 | return FALSE; |
| 516 | } |
| 517 | |
| 518 | /** |
| 519 | * Static instance provider - return the instance for the current locale. |
| 520 | * |
| 521 | * @return CRM_Core_I18n |
| 522 | */ |
| 523 | public static function &singleton() { |
| 524 | static $singleton = array(); |
| 525 | |
| 526 | global $tsLocale; |
| 527 | if (!isset($singleton[$tsLocale])) { |
| 528 | $singleton[$tsLocale] = new CRM_Core_I18n($tsLocale); |
| 529 | } |
| 530 | |
| 531 | return $singleton[$tsLocale]; |
| 532 | } |
| 533 | |
| 534 | /** |
| 535 | * Set the LC_TIME locale if it's not set already (for a given language choice). |
| 536 | * |
| 537 | * @return string |
| 538 | * the final LC_TIME that got set |
| 539 | */ |
| 540 | public static function setLcTime() { |
| 541 | static $locales = array(); |
| 542 | |
| 543 | global $tsLocale; |
| 544 | if (!isset($locales[$tsLocale])) { |
| 545 | // with the config being set to pl_PL: try pl_PL.UTF-8, |
| 546 | // then pl_PL, if neither present fall back to C |
| 547 | $locales[$tsLocale] = setlocale(LC_TIME, $tsLocale . '.UTF-8', $tsLocale, 'C'); |
| 548 | } |
| 549 | |
| 550 | return $locales[$tsLocale]; |
| 551 | } |
| 552 | |
| 553 | /** |
| 554 | * Get the default language for contacts where no language is provided. |
| 555 | * |
| 556 | * Note that NULL is a valid option so be careful with checking for empty etc. |
| 557 | * |
| 558 | * NULL would mean 'we don't know & we don't want to hazard a guess'. |
| 559 | * |
| 560 | * @return string |
| 561 | */ |
| 562 | public static function getContactDefaultLanguage() { |
| 563 | $language = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME, 'contact_default_language'); |
| 564 | if ($language == 'undefined') { |
| 565 | return NULL; |
| 566 | } |
| 567 | if (empty($language) || $language === '*default*') { |
| 568 | $language = civicrm_api3('setting', 'getvalue', array( |
| 569 | 'name' => 'lcMessages', |
| 570 | 'group' => CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME, |
| 571 | )); |
| 572 | } |
| 573 | elseif ($language == 'current_site_language') { |
| 574 | global $tsLocale; |
| 575 | return $tsLocale; |
| 576 | } |
| 577 | |
| 578 | return $language; |
| 579 | } |
| 580 | |
| 581 | } |
| 582 | |
| 583 | /** |
| 584 | * Short-named function for string translation, defined in global scope so it's available everywhere. |
| 585 | * |
| 586 | * @param string $text |
| 587 | * String string for translating. |
| 588 | * @param array $params |
| 589 | * Array an array of additional parameters. |
| 590 | * |
| 591 | * @return string |
| 592 | * the translated string |
| 593 | */ |
| 594 | function ts($text, $params = array()) { |
| 595 | static $config = NULL; |
| 596 | static $locale = NULL; |
| 597 | static $i18n = NULL; |
| 598 | static $function = NULL; |
| 599 | |
| 600 | if ($text == '') { |
| 601 | return ''; |
| 602 | } |
| 603 | |
| 604 | if (!$config) { |
| 605 | $config = CRM_Core_Config::singleton(); |
| 606 | } |
| 607 | |
| 608 | global $tsLocale; |
| 609 | if (!$i18n or $locale != $tsLocale) { |
| 610 | $i18n = CRM_Core_I18n::singleton(); |
| 611 | $locale = $tsLocale; |
| 612 | if (isset($config->customTranslateFunction) and function_exists($config->customTranslateFunction)) { |
| 613 | $function = $config->customTranslateFunction; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | if ($function) { |
| 618 | return $function($text, $params); |
| 619 | } |
| 620 | else { |
| 621 | return $i18n->crm_translate($text, $params); |
| 622 | } |
| 623 | } |