Merge pull request #10540 from colemanw/CRM-20091
[civicrm-core.git] / CRM / Core / I18n.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
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 /**
44 * A PHP-gettext instance for string translation;
45 * should stay null if the strings are not to be translated (en_US).
46 */
47 private $_phpgettext = NULL;
48
49 /**
50 * Whether we are using native gettext or not.
51 */
52 private $_nativegettext = FALSE;
53
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
61 /**
62 * @var string
63 */
64 private $locale;
65
66 /**
67 * A locale-based constructor that shouldn't be called from outside of this class (use singleton() instead).
68 *
69 * @param string $locale
70 * the base of this certain object's existence.
71 *
72 * @return \CRM_Core_I18n
73 */
74 public function __construct($locale) {
75 $this->locale = $locale;
76 if ($locale != '' and $locale != 'en_US') {
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;
81 $this->setNativeGettextLocale($locale);
82 return;
83 }
84
85 // Otherwise, use PHP-gettext
86 $this->setPhpGettextLocale($locale);
87 }
88 }
89
90 /**
91 * Returns whether gettext is running natively or using PHP-Gettext.
92 *
93 * @return bool
94 * True if gettext is native
95 */
96 public function isNative() {
97 return $this->_nativegettext;
98 }
99
100 /**
101 * Set native locale for getText.
102 *
103 * @param string $locale
104 */
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
124 /**
125 * Set getText locale.
126 *
127 * @param string $locale
128 */
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
150 /**
151 * Return languages available in this instance of CiviCRM.
152 *
153 * @param bool $justEnabled
154 * whether to return all languages or just the enabled ones.
155 *
156 * @return array
157 * Array of code/language name mappings
158 */
159 public static function languages($justEnabled = FALSE) {
160 static $all = NULL;
161 static $enabled = NULL;
162
163 if (!$all) {
164 $all = CRM_Contact_BAO_Contact::buildOptions('preferred_language');
165
166 // check which ones are available; add them to $all if not there already
167 $codes = array();
168 if (is_dir(CRM_Core_I18n::getResourceDir()) && $dir = opendir(CRM_Core_I18n::getResourceDir())) {
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 }
185 if (!in_array($code, $codes)) {
186 unset($all[$code]);
187 }
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 *
209 * @param string $str
210 * source string.
211 *
212 * @return string
213 * modified string
214 */
215 public function strarg($str) {
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
232 /**
233 * Get the directory for l10n resources.
234 *
235 * @return string
236 */
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
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 *
260 * @param string $text
261 * the original string.
262 * @param array $params
263 * The params of the translation (if any).
264 * - domain: string|array a list of translation domains to search (in order)
265 * - context: string
266 *
267 * @return string
268 * the translated string
269 */
270 public function crm_translate($text, $params = array()) {
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')) {
282 $text = CRM_Core_DAO::escapeString($text);
283 }
284 if (isset($escape) and ($escape == 'js')) {
285 $text = addcslashes($text, "'");
286 }
287 return $text;
288 }
289
290 $plural = $count = NULL;
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
307 if (isset($params['domain'])) {
308 $domain = $params['domain'];
309 unset($params['domain']);
310 }
311 else {
312 $domain = NULL;
313 }
314
315 $raw = !empty($params['raw']);
316 unset($params['raw']);
317
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
338 if (count($params) && !$raw) {
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
363 *
364 * @return string
365 */
366 protected function crm_translate_raw($text, $domain, $count, $plural, $context) {
367 // gettext domain for extensions
368 $domain_changed = FALSE;
369 if (!empty($domain) && $this->_phpgettext) {
370 if ($this->setGettextDomain($domain)) {
371 $domain_changed = TRUE;
372 }
373 }
374
375 // do all wildcard translations first
376
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__])) {
380 if (defined('CIVICRM_DSN') && !CRM_Core_Config::isUpgradeMode()) {
381 Civi::$statics[__CLASS__][$replacementsLocale] = CRM_Core_BAO_WordReplacement::getLocaleCustomStrings($replacementsLocale);
382 }
383 else {
384 Civi::$statics[__CLASS__][$replacementsLocale] = array();
385 }
386 }
387 $stringTable = Civi::$statics[__CLASS__][$replacementsLocale];
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
400 if (
401 !$exactMatch &&
402 isset($stringTable['enabled']['wildcardMatch'])
403 ) {
404 $search = array_keys($stringTable['enabled']['wildcardMatch']);
405 $replace = array_values($stringTable['enabled']['wildcardMatch']);
406 $text = str_replace($search, $replace, $text);
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
440 if ($domain_changed) {
441 $this->setGettextDomain('civicrm');
442 }
443
444 return $text;
445 }
446
447 /**
448 * Translate a string to the current locale.
449 *
450 * @param string $string
451 * this string should be translated.
452 *
453 * @return string
454 * the translated string
455 */
456 public function translate($string) {
457 return ($this->_phpgettext) ? $this->_phpgettext->translate($string) : $string;
458 }
459
460 /**
461 * Localize (destructively) array values.
462 *
463 * @param array $array
464 * the array for localization (in place).
465 * @param array $params
466 * an array of additional parameters.
467 */
468 public function localizeArray(
469 &$array,
470 $params = array()
471 ) {
472 $tsLocale = CRM_Core_I18n::getLocale();
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 *
488 * @param array $array
489 * the array for localization (in place).
490 */
491 public function localizeTitles(&$array) {
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') {
498 $array[$key] = ts($value, array('context' => 'menu'));
499 }
500 }
501 }
502
503 /**
504 * Binds a gettext domain, wrapper over bindtextdomain().
505 *
506 * @param $key
507 * Key of the extension (can be 'civicrm', or 'org.example.foo').
508 *
509 * @return Bool
510 * True if the domain was changed for an extension.
511 */
512 public function setGettextDomain($key) {
513 /* No domain changes for en_US */
514 if (!$this->_phpgettext) {
515 return FALSE;
516 }
517
518 // It's only necessary to find/bind once
519 if (!isset($this->_extensioncache[$key])) {
520 try {
521 $mapper = CRM_Extension_System::singleton()->getMapper();
522 $path = $mapper->keyToBasePath($key);
523 $info = $mapper->keyToInfo($key);
524 $domain = $info->file;
525
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
533 $mo_file = $path . DIRECTORY_SEPARATOR . 'l10n' . DIRECTORY_SEPARATOR . $this->locale . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . $domain . '.mo';
534 $streamer = new FileReader($mo_file);
535 $this->_extensioncache[$key] = new gettext_reader($streamer);
536 }
537 }
538 catch (CRM_Extension_Exception $e) {
539 // Intentionally not translating this string to avoid possible infinite loops
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;
543 }
544 }
545
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;
555 }
556
557 return FALSE;
558 }
559
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
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 }
588
589 /**
590 * Change the processing language without changing the current user language
591 *
592 * @param $locale
593 * Locale (for example 'en_US', or 'fr_CA').
594 * True if the domain was changed for an extension.
595 */
596 public function setLocale($locale) {
597
598 // Change the language of the CMS as well, for URLs.
599 CRM_Utils_System::setUFLocale($locale);
600
601 // change the gettext ressources
602 if ($this->_nativegettext) {
603 $this->setNativeGettextLocale($locale);
604 }
605 else {
606 // phpgettext
607 $this->setPhpGettextLocale($locale);
608 }
609
610 // for sql queries
611 global $dbLocale;
612 $dbLocale = "_{$locale}";
613
614 }
615
616 /**
617 * Static instance provider - return the instance for the current locale.
618 *
619 * @return CRM_Core_I18n
620 */
621 public static function &singleton() {
622 static $singleton = array();
623
624 $tsLocale = CRM_Core_I18n::getLocale();
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 *
635 * @return string
636 * the final LC_TIME that got set
637 */
638 public static function setLcTime() {
639 static $locales = array();
640
641 $tsLocale = CRM_Core_I18n::getLocale();
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 }
650
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() {
661 $language = Civi::settings()->get('contact_default_language');
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') {
672 return CRM_Core_I18n::getLocale();
673 }
674
675 return $language;
676 }
677
678 /**
679 * Get the current locale
680 *
681 * @return string
682 */
683 public static function getLocale() {
684 global $tsLocale;
685 return $tsLocale ? $tsLocale : 'en_US';
686 }
687
688 }
689
690 /**
691 * Short-named function for string translation, defined in global scope so it's available everywhere.
692 *
693 * @param string $text
694 * String string for translating.
695 * @param array $params
696 * Array an array of additional parameters.
697 *
698 * @return string
699 * the translated string
700 */
701 function 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
715 $tsLocale = CRM_Core_I18n::getLocale();
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 }