3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 * Constants for communication preferences.
24 const NONE
= 'none', AUTO
= 'auto';
28 * A callback function which handles SQL string encoding.
29 * Set NULL to use the default, CRM_Core_DAO::escapeString().
30 * This is used by `ts(..., [escape=>sql])`.
32 * This option is not intended for general consumption. It is only intended
33 * for certain pre-boot/pre-install contexts.
35 * You might ask, "Why on Earth does string-translation have an opinion on
36 * SQL escaping?" Good question!
38 public static $SQL_ESCAPER = NULL;
41 * Encode a string for use in SQL.
46 protected static function escapeSql($text) {
47 if (self
::$SQL_ESCAPER == NULL) {
48 return CRM_Core_DAO
::escapeString($text);
51 return call_user_func(self
::$SQL_ESCAPER, $text);
56 * A PHP-gettext instance for string translation;
57 * should stay null if the strings are not to be translated (en_US).
60 private $_phpgettext = NULL;
63 * Whether we are using native gettext or not.
66 private $_nativegettext = FALSE;
69 * Gettext cache for extension domains/streamers, depending on if native or phpgettext.
70 * - native gettext: we cache the value for textdomain()
71 * - phpgettext: we cache the file streamer.
74 private $_extensioncache = [];
82 * A locale-based constructor that shouldn't be called from outside of this class (use singleton() instead).
84 * @param string $locale
85 * the base of this certain object's existence.
87 * @return \CRM_Core_I18n
89 public function __construct($locale) {
90 $this->locale
= $locale;
91 if ($locale != '' and $locale != 'en_US') {
92 if (defined('CIVICRM_GETTEXT_NATIVE') && CIVICRM_GETTEXT_NATIVE
&& function_exists('gettext')) {
93 // Note: the file hierarchy for .po must be, for example: l10n/fr_FR/LC_MESSAGES/civicrm.mo
95 $this->_nativegettext
= TRUE;
96 $this->setNativeGettextLocale($locale);
100 // Otherwise, use PHP-gettext
101 $this->setPhpGettextLocale($locale);
106 * Returns whether gettext is running natively or using PHP-Gettext.
109 * True if gettext is native
111 public function isNative() {
112 return $this->_nativegettext
;
116 * Set native locale for getText.
118 * @param string $locale
120 protected function setNativeGettextLocale($locale) {
123 putenv("LANG=$locale");
125 // CRM-11833 Avoid LC_ALL because of LC_NUMERIC and potential DB error.
126 setlocale(LC_TIME
, $locale);
127 setlocale(LC_MESSAGES
, $locale);
128 setlocale(LC_CTYPE
, $locale);
130 bindtextdomain('civicrm', CRM_Core_I18n
::getResourceDir());
131 bind_textdomain_codeset('civicrm', 'UTF-8');
132 textdomain('civicrm');
134 $this->_phpgettext
= new CRM_Core_I18n_NativeGettext();
135 $this->_extensioncache
['civicrm'] = 'civicrm';
140 * Set getText locale.
142 * @param string $locale
144 protected function setPhpGettextLocale($locale) {
146 // we support both the old file hierarchy format and the new:
147 // pre-4.5: civicrm/l10n/xx_XX/civicrm.mo
148 // post-4.5: civicrm/l10n/xx_XX/LC_MESSAGES/civicrm.mo
149 require_once 'PHPgettext/streams.php';
150 require_once 'PHPgettext/gettext.php';
152 $mo_file = CRM_Core_I18n
::getResourceDir() . $locale . DIRECTORY_SEPARATOR
. 'LC_MESSAGES' . DIRECTORY_SEPARATOR
. 'civicrm.mo';
154 if (!file_exists($mo_file)) {
155 // fallback to pre-4.5 mode
156 $mo_file = CRM_Core_I18n
::getResourceDir() . $locale . DIRECTORY_SEPARATOR
. 'civicrm.mo';
159 $streamer = new FileReader($mo_file);
160 $this->_phpgettext
= new gettext_reader($streamer);
161 $this->_extensioncache
['civicrm'] = $this->_phpgettext
;
166 * Return languages available in this instance of CiviCRM.
168 * @param bool $justEnabled
169 * whether to return all languages or just the enabled ones.
172 * Array of code/language name mappings
174 public static function languages($justEnabled = FALSE) {
176 static $enabled = NULL;
179 $all = CRM_Contact_BAO_Contact
::buildOptions('preferred_language');
184 CRM_Core_OptionValue
::getValues(['name' => 'languages'], $rows);
185 foreach ($rows as $id => $row) {
186 $labels[$row['name']] = $row['label'];
189 // check which ones are available; add them to $all if not there already
191 if (is_dir(CRM_Core_I18n
::getResourceDir()) && $dir = opendir(CRM_Core_I18n
::getResourceDir())) {
192 while ($filename = readdir($dir)) {
193 if (preg_match('/^[a-z][a-z]_[A-Z][A-Z]$/', $filename)) {
194 $codes[] = $filename;
195 if (!isset($all[$filename])) {
196 $all[$filename] = $labels[$filename];
203 // drop the unavailable languages (except en_US)
204 foreach (array_keys($all) as $code) {
205 if ($code == 'en_US') {
208 if (!in_array($code, $codes)) {
216 if ($enabled === NULL) {
217 $languageLimit = Civi
::settings()->get('languageLimit');
219 if ($languageLimit) {
220 foreach ($all as $code => $name) {
221 if (array_key_exists($code, $languageLimit)) {
222 $enabled[$code] = $name;
228 return $justEnabled ?
$enabled : $all;
232 * Return the available UI languages
233 * @return array|string
234 * array(string languageCode => string languageName) if !$justCodes
236 public static function uiLanguages($justCodes = FALSE) {
237 // In multilang we only allow the languages that are configured in db
238 // Otherwise, the languages configured in uiLanguages
239 $settings = Civi
::settings();
240 if (CRM_Core_I18n
::isMultiLingual()) {
241 $codes = array_keys((array) $settings->get('languageLimit'));
244 $codes = $settings->get('uiLanguages');
246 $codes = [$settings->get('lcMessages')];
249 return $justCodes ?
$codes
250 : CRM_Utils_Array
::subset(CRM_Core_I18n
::languages(), $codes);
254 * Replace arguments in a string with their values. Arguments are represented by % followed by their number.
262 public function strarg($str) {
265 for ($i = 1; $i < func_num_args(); $i++
) {
266 $arg = func_get_arg($i);
267 if (is_array($arg)) {
268 foreach ($arg as $aarg) {
269 $tr['%' . ++
$p] = $aarg;
273 $tr['%' . ++
$p] = $arg;
276 return strtr($str, $tr);
280 * Get the directory for l10n resources.
284 public static function getResourceDir() {
287 $dir = dirname(dirname(__DIR__
)) . DIRECTORY_SEPARATOR
. 'l10n' . DIRECTORY_SEPARATOR
;
293 * Smarty block function, provides gettext support for smarty.
295 * The block content is the text that should be translated.
297 * Any parameter that is sent to the function will be represented as %n in the translation text,
298 * where n is 1 for the first parameter. The following parameters are reserved:
299 * - escape - sets escape mode:
300 * - 'html' for HTML escaping, this is the default.
301 * - 'js' for javascript escaping.
302 * - 'no'/'off'/0 - turns off escaping
303 * - plural - The plural version of the text (2nd parameter of ngettext())
304 * - count - The item count for plural mode (3rd parameter of ngettext())
305 * - context - gettext context of that string (for homonym handling)
307 * @param string $text
308 * the original string.
309 * @param array $params
310 * The params of the translation (if any).
311 * - domain: string|array a list of translation domains to search (in order)
313 * - skip_translation: flag (do only escape/replacement, skip the actual translation)
316 * the translated string
318 public function crm_translate($text, $params = []) {
319 if (isset($params['escape'])) {
320 $escape = $params['escape'];
321 unset($params['escape']);
324 // sometimes we need to {ts}-tag a string, but don’t want to
325 // translate it in the template (like civicrm_navigation.tpl),
326 // because we handle the translation in a different way (CRM-6998)
327 // in such cases we return early, only doing SQL/JS escaping
328 if (isset($params['skip']) and $params['skip']) {
329 if (isset($escape) and ($escape == 'sql')) {
330 $text = self
::escapeSql($text);
332 if (isset($escape) and ($escape == 'js')) {
333 $text = addcslashes($text, "'");
338 $plural = $count = NULL;
339 if (isset($params['plural'])) {
340 $plural = $params['plural'];
341 unset($params['plural']);
342 if (isset($params['count'])) {
343 $count = $params['count'];
347 if (isset($params['context'])) {
348 $context = $params['context'];
349 unset($params['context']);
355 if (isset($params['domain'])) {
356 $domain = $params['domain'];
357 unset($params['domain']);
363 $raw = !empty($params['raw']);
364 unset($params['raw']);
366 if (!isset($params['skip_translation'])) {
367 if (!empty($domain)) {
368 // It might be prettier to cast to an array, but this is high-traffic stuff.
369 if (is_array($domain)) {
370 foreach ($domain as $d) {
371 $candidate = $this->crm_translate_raw($text, $d, $count, $plural, $context);
372 if ($candidate != $text) {
379 $text = $this->crm_translate_raw($text, $domain, $count, $plural, $context);
383 $text = $this->crm_translate_raw($text, NULL, $count, $plural, $context);
387 // replace the numbered %1, %2, etc. params if present
388 if (count($params) && !$raw) {
389 $text = $this->strarg($text, $params);
392 // escape SQL if we were asked for it
393 if (isset($escape) and ($escape == 'sql')) {
394 $text = self
::escapeSql($text);
397 // escape for JavaScript (if requested)
398 if (isset($escape) and ($escape == 'js')) {
399 $text = addcslashes($text, "'");
406 * Lookup the raw translation of a string (without any extra escaping or interpolation).
408 * @param string $text
409 * @param string|null $domain
410 * @param int|null $count
411 * @param string $plural
412 * @param string $context
416 protected function crm_translate_raw($text, $domain, $count, $plural, $context) {
417 // gettext domain for extensions
418 $domain_changed = FALSE;
419 if (!empty($domain) && $this->_phpgettext
) {
420 if ($this->setGettextDomain($domain)) {
421 $domain_changed = TRUE;
425 // do all wildcard translations first
427 // FIXME: Is there a constant we can reference instead of hardcoding en_US?
428 $replacementsLocale = $this->locale ?
$this->locale
: 'en_US';
429 if (!isset(Civi
::$statics[__CLASS__
]) ||
!array_key_exists($replacementsLocale, Civi
::$statics[__CLASS__
])) {
430 if (defined('CIVICRM_DSN') && !CRM_Core_Config
::isUpgradeMode()) {
431 Civi
::$statics[__CLASS__
][$replacementsLocale] = CRM_Core_BAO_WordReplacement
::getLocaleCustomStrings($replacementsLocale);
434 Civi
::$statics[__CLASS__
][$replacementsLocale] = [];
437 $stringTable = Civi
::$statics[__CLASS__
][$replacementsLocale];
440 if (isset($stringTable['enabled']['exactMatch'])) {
441 foreach ($stringTable['enabled']['exactMatch'] as $search => $replace) {
442 if ($search === $text) {
452 isset($stringTable['enabled']['wildcardMatch'])
454 $search = array_keys($stringTable['enabled']['wildcardMatch']);
455 $replace = array_values($stringTable['enabled']['wildcardMatch']);
456 $text = str_replace($search, $replace, $text);
459 // dont translate if we've done exactMatch already
461 // use plural if required parameters are set
462 if (isset($count) && isset($plural)) {
464 if ($this->_phpgettext
) {
465 $text = $this->_phpgettext
->ngettext($text, $plural, $count);
468 // if the locale's not set, we do ngettext work by hand
469 // if $count == 1 then $text = $text, else $text = $plural
475 // expand %count in translated string to $count
476 $text = strtr($text, ['%count' => $count]);
478 // if not plural, but the locale's set, translate
480 elseif ($this->_phpgettext
) {
482 $text = $this->_phpgettext
->pgettext($context, $text);
485 $text = $this->_phpgettext
->translate($text);
490 if ($domain_changed) {
491 $this->setGettextDomain('civicrm');
498 * Translate a string to the current locale.
500 * @param string $string
501 * this string should be translated.
504 * the translated string
506 public function translate($string) {
507 return ($this->_phpgettext
) ?
$this->_phpgettext
->translate($string) : $string;
511 * Localize (destructively) array values.
513 * @param array $array
514 * the array for localization (in place).
515 * @param array $params
516 * an array of additional parameters.
518 public function localizeArray(
522 $tsLocale = CRM_Core_I18n
::getLocale();
524 if ($tsLocale == 'en_US') {
528 foreach ($array as & $value) {
530 $value = ts($value, $params);
536 * Localize (destructively) array elements with keys of 'title'.
538 * @param array $array
539 * the array for localization (in place).
541 public function localizeTitles(&$array) {
542 foreach ($array as $key => $value) {
543 if (is_array($value)) {
544 $this->localizeTitles($value);
545 $array[$key] = $value;
547 elseif ((string ) $key == 'title') {
548 $array[$key] = ts($value, ['context' => 'menu']);
554 * Binds a gettext domain, wrapper over bindtextdomain().
557 * Key of the extension (can be 'civicrm', or 'org.example.foo').
560 * True if the domain was changed for an extension.
562 public function setGettextDomain($key) {
563 /* No domain changes for en_US */
564 if (!$this->_phpgettext
) {
568 // It's only necessary to find/bind once
569 if (!isset($this->_extensioncache
[$key])) {
571 $mapper = CRM_Extension_System
::singleton()->getMapper();
572 $path = $mapper->keyToBasePath($key);
573 $info = $mapper->keyToInfo($key);
574 $domain = $info->file
;
576 if ($this->_nativegettext
) {
577 bindtextdomain($domain, $path . DIRECTORY_SEPARATOR
. 'l10n');
578 bind_textdomain_codeset($domain, 'UTF-8');
579 $this->_extensioncache
[$key] = $domain;
583 $mo_file = $path . DIRECTORY_SEPARATOR
. 'l10n' . DIRECTORY_SEPARATOR
. $this->locale
. DIRECTORY_SEPARATOR
. 'LC_MESSAGES' . DIRECTORY_SEPARATOR
. $domain . '.mo';
584 $streamer = new FileReader($mo_file);
585 $this->_extensioncache
[$key] = new gettext_reader($streamer);
588 catch (CRM_Extension_Exception
$e) {
589 // Intentionally not translating this string to avoid possible infinite loops
590 // Only developers should see this string, if they made a mistake in their ts() usage.
591 CRM_Core_Session
::setStatus('Unknown extension key in a translation string: ' . $key, '', 'error');
592 $this->_extensioncache
[$key] = FALSE;
596 if (isset($this->_extensioncache
[$key]) && $this->_extensioncache
[$key]) {
597 if ($this->_nativegettext
) {
598 textdomain($this->_extensioncache
[$key]);
601 $this->_phpgettext
= $this->_extensioncache
[$key];
611 * Is the CiviCRM in multilingual mode.
614 * True if CiviCRM is in multilingual mode.
616 public static function isMultilingual() {
617 $domain = new CRM_Core_DAO_Domain();
619 return (bool) $domain->locales
;
623 * Is the language written "right-to-left"?
626 * Language (for example 'en_US', or 'fr_CA').
629 * True if it is an RTL language.
631 public static function isLanguageRTL($language) {
632 $rtl = CRM_Core_I18n_PseudoConstant
::getRTLlanguages();
633 $short = CRM_Core_I18n_PseudoConstant
::shortForLong($language);
635 return (in_array($short, $rtl));
639 * Change the processing language without changing the current user language
642 * Locale (for example 'en_US', or 'fr_CA').
643 * True if the domain was changed for an extension.
645 public function setLocale($locale) {
647 // Change the language of the CMS as well, for URLs.
648 CRM_Utils_System
::setUFLocale($locale);
650 // change the gettext ressources
651 if ($this->_nativegettext
) {
652 $this->setNativeGettextLocale($locale);
656 $this->setPhpGettextLocale($locale);
659 // For sql queries, if running in DB multi-lingual mode.
663 $dbLocale = "_{$locale}";
666 // For self::getLocale()
672 * Static instance provider - return the instance for the current locale.
674 * @return CRM_Core_I18n
676 public static function &singleton() {
677 if (!isset(Civi
::$statics[__CLASS__
]['singleton'])) {
678 Civi
::$statics[__CLASS__
]['singleton'] = [];
680 $tsLocale = CRM_Core_I18n
::getLocale();
681 if (!isset(Civi
::$statics[__CLASS__
]['singleton'][$tsLocale])) {
682 Civi
::$statics[__CLASS__
]['singleton'][$tsLocale] = new CRM_Core_I18n($tsLocale);
685 return Civi
::$statics[__CLASS__
]['singleton'][$tsLocale];
689 * Set the LC_TIME locale if it's not set already (for a given language choice).
692 * the final LC_TIME that got set
694 public static function setLcTime() {
695 static $locales = [];
697 $tsLocale = CRM_Core_I18n
::getLocale();
698 if (!isset($locales[$tsLocale])) {
699 // with the config being set to pl_PL: try pl_PL.UTF-8,
700 // then pl_PL, if neither present fall back to C
701 $locales[$tsLocale] = setlocale(LC_TIME
, $tsLocale . '.UTF-8', $tsLocale, 'C');
704 return $locales[$tsLocale];
708 * Get the default language for contacts where no language is provided.
710 * Note that NULL is a valid option so be careful with checking for empty etc.
712 * NULL would mean 'we don't know & we don't want to hazard a guess'.
716 public static function getContactDefaultLanguage() {
717 $language = Civi
::settings()->get('contact_default_language');
718 if ($language == 'undefined') {
721 if (empty($language) ||
$language === '*default*') {
722 $language = civicrm_api3('setting', 'getvalue', [
723 'name' => 'lcMessages',
724 'group' => CRM_Core_BAO_Setting
::LOCALIZATION_PREFERENCES_NAME
,
727 elseif ($language == 'current_site_language') {
728 return CRM_Core_I18n
::getLocale();
735 * Get the current locale
739 public static function getLocale() {
741 return $tsLocale ?
$tsLocale : 'en_US';
747 * Short-named function for string translation, defined in global scope so it's available everywhere.
749 * @param string $text
750 * String string for translating.
751 * @param array $params
752 * Array an array of additional parameters.
755 * the translated string
757 function ts($text, $params = []) {
758 static $bootstrapReady = FALSE;
759 static $lastLocale = NULL;
761 static $function = NULL;
767 // When the settings become available, lookup customTranslateFunction.
768 if (!$bootstrapReady) {
769 $bootstrapReady = (bool) \Civi\Core\Container
::isContainerBooted();
770 if ($bootstrapReady) {
771 // just got ready: determine whether there is a working custom translation function
772 $config = CRM_Core_Config
::singleton();
773 if (isset($config->customTranslateFunction
) and function_exists($config->customTranslateFunction
)) {
774 $function = $config->customTranslateFunction
;
778 // don't _translate_ anything until bootstrap has progressed enough
779 $params['skip_translation'] = 1;
783 $activeLocale = CRM_Core_I18n
::getLocale();
784 if (!$i18n or $lastLocale != $activeLocale) {
785 $i18n = CRM_Core_I18n
::singleton();
786 $lastLocale = $activeLocale;
790 return $function($text, $params);
793 return $i18n->crm_translate($text, $params);