Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
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 | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | class CRM_Core_I18n { | |
34 | ||
07844ccf SV |
35 | /** |
36 | * Constants for communication preferences. | |
37 | * | |
38 | * @var int | |
39 | */ | |
40 | const NONE = 'none', AUTO = 'auto'; | |
41 | ||
2c4bba53 | 42 | /** |
e97c66ff | 43 | * @var callable|null |
2c4bba53 TO |
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 | } | |
07844ccf | 70 | |
6a488035 | 71 | /** |
2ba175b6 DL |
72 | * A PHP-gettext instance for string translation; |
73 | * should stay null if the strings are not to be translated (en_US). | |
518fa0ee | 74 | * @var object |
6a488035 TO |
75 | */ |
76 | private $_phpgettext = NULL; | |
77 | ||
78 | /** | |
79 | * Whether we are using native gettext or not. | |
518fa0ee | 80 | * @var bool |
6a488035 TO |
81 | */ |
82 | private $_nativegettext = FALSE; | |
83 | ||
74eb462f ML |
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. | |
518fa0ee | 88 | * @var array |
74eb462f | 89 | */ |
be2fb01f | 90 | private $_extensioncache = []; |
74eb462f | 91 | |
0d096477 TO |
92 | /** |
93 | * @var string | |
94 | */ | |
95 | private $locale; | |
96 | ||
6a488035 TO |
97 | /** |
98 | * A locale-based constructor that shouldn't be called from outside of this class (use singleton() instead). | |
99 | * | |
5a4f6742 CW |
100 | * @param string $locale |
101 | * the base of this certain object's existence. | |
6a488035 | 102 | * |
77b97be7 | 103 | * @return \CRM_Core_I18n |
6a488035 | 104 | */ |
00be9182 | 105 | public function __construct($locale) { |
0d096477 | 106 | $this->locale = $locale; |
6a488035 | 107 | if ($locale != '' and $locale != 'en_US') { |
6a488035 TO |
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; | |
c86241f4 | 112 | $this->setNativeGettextLocale($locale); |
6a488035 TO |
113 | return; |
114 | } | |
115 | ||
116 | // Otherwise, use PHP-gettext | |
c86241f4 | 117 | $this->setPhpGettextLocale($locale); |
6a488035 TO |
118 | } |
119 | } | |
120 | ||
121 | /** | |
122 | * Returns whether gettext is running natively or using PHP-Gettext. | |
123 | * | |
a6c01b45 CW |
124 | * @return bool |
125 | * True if gettext is native | |
6a488035 | 126 | */ |
00be9182 | 127 | public function isNative() { |
6a488035 TO |
128 | return $this->_nativegettext; |
129 | } | |
130 | ||
f2ac86d1 | 131 | /** |
132 | * Set native locale for getText. | |
133 | * | |
134 | * @param string $locale | |
135 | */ | |
c86241f4 SV |
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 | ||
f2ac86d1 | 155 | /** |
156 | * Set getText locale. | |
157 | * | |
158 | * @param string $locale | |
159 | */ | |
c86241f4 SV |
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 | ||
6a488035 TO |
181 | /** |
182 | * Return languages available in this instance of CiviCRM. | |
183 | * | |
5a4f6742 CW |
184 | * @param bool $justEnabled |
185 | * whether to return all languages or just the enabled ones. | |
6a488035 | 186 | * |
a6c01b45 | 187 | * @return array |
16b10e64 | 188 | * Array of code/language name mappings |
6a488035 | 189 | */ |
00be9182 | 190 | public static function languages($justEnabled = FALSE) { |
6a488035 TO |
191 | static $all = NULL; |
192 | static $enabled = NULL; | |
193 | ||
194 | if (!$all) { | |
c0c9cd82 | 195 | $all = CRM_Contact_BAO_Contact::buildOptions('preferred_language'); |
6a488035 | 196 | |
ece6501c | 197 | // get labels |
be2fb01f CW |
198 | $rows = []; |
199 | $labels = []; | |
200 | CRM_Core_OptionValue::getValues(['name' => 'languages'], $rows); | |
ece6501c ML |
201 | foreach ($rows as $id => $row) { |
202 | $labels[$row['name']] = $row['label']; | |
203 | } | |
204 | ||
6a488035 | 205 | // check which ones are available; add them to $all if not there already |
be2fb01f | 206 | $codes = []; |
4d8ce7f0 | 207 | if (is_dir(CRM_Core_I18n::getResourceDir()) && $dir = opendir(CRM_Core_I18n::getResourceDir())) { |
6a488035 TO |
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])) { | |
ece6501c | 212 | $all[$filename] = $labels[$filename]; |
6a488035 TO |
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 | } | |
408b79bf | 224 | if (!in_array($code, $codes)) { |
225 | unset($all[$code]); | |
226 | } | |
6a488035 | 227 | } |
ece6501c | 228 | |
921ed8ae | 229 | asort($all); |
6a488035 TO |
230 | } |
231 | ||
232 | if ($enabled === NULL) { | |
cc493f5e | 233 | $languageLimit = Civi::settings()->get('languageLimit'); |
be2fb01f | 234 | $enabled = []; |
cc493f5e | 235 | if ($languageLimit) { |
6a488035 | 236 | foreach ($all as $code => $name) { |
cc493f5e | 237 | if (array_key_exists($code, $languageLimit)) { |
6a488035 TO |
238 | $enabled[$code] = $name; |
239 | } | |
240 | } | |
241 | } | |
242 | } | |
243 | ||
244 | return $justEnabled ? $enabled : $all; | |
245 | } | |
246 | ||
921ed8ae AS |
247 | /** |
248 | * Return the available UI languages | |
518fa0ee SL |
249 | * @return array|string |
250 | * array(string languageCode => string languageName) if !$justCodes | |
921ed8ae AS |
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 | ||
6a488035 TO |
269 | /** |
270 | * Replace arguments in a string with their values. Arguments are represented by % followed by their number. | |
271 | * | |
5a4f6742 CW |
272 | * @param string $str |
273 | * source string. | |
6a488035 | 274 | * |
a6c01b45 CW |
275 | * @return string |
276 | * modified string | |
6a488035 | 277 | */ |
00be9182 | 278 | public function strarg($str) { |
be2fb01f | 279 | $tr = []; |
6a488035 TO |
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 | ||
f2ac86d1 | 295 | /** |
296 | * Get the directory for l10n resources. | |
297 | * | |
298 | * @return string | |
299 | */ | |
4d8ce7f0 TO |
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 | ||
6a488035 TO |
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 | * | |
5a4f6742 CW |
323 | * @param string $text |
324 | * the original string. | |
325 | * @param array $params | |
3f3bba82 TO |
326 | * The params of the translation (if any). |
327 | * - domain: string|array a list of translation domains to search (in order) | |
328 | * - context: string | |
69e812be | 329 | * - skip_translation: flag (do only escape/replacement, skip the actual translation) |
e3b7fc11 | 330 | * |
a6c01b45 CW |
331 | * @return string |
332 | * the translated string | |
6a488035 | 333 | */ |
be2fb01f | 334 | public function crm_translate($text, $params = []) { |
6a488035 TO |
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')) { | |
2c4bba53 | 346 | $text = self::escapeSql($text); |
6a488035 TO |
347 | } |
348 | if (isset($escape) and ($escape == 'js')) { | |
349 | $text = addcslashes($text, "'"); | |
350 | } | |
351 | return $text; | |
352 | } | |
353 | ||
3f3bba82 | 354 | $plural = $count = NULL; |
6a488035 TO |
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 | ||
3f3bba82 TO |
371 | if (isset($params['domain'])) { |
372 | $domain = $params['domain']; | |
373 | unset($params['domain']); | |
374 | } | |
375 | else { | |
376 | $domain = NULL; | |
377 | } | |
378 | ||
1b4710da TO |
379 | $raw = !empty($params['raw']); |
380 | unset($params['raw']); | |
381 | ||
69e812be | 382 | if (!isset($params['skip_translation'])) { |
2f98cf00 E |
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 | } | |
3f3bba82 TO |
392 | } |
393 | } | |
2f98cf00 E |
394 | else { |
395 | $text = $this->crm_translate_raw($text, $domain, $count, $plural, $context); | |
396 | } | |
3f3bba82 TO |
397 | } |
398 | else { | |
2f98cf00 | 399 | $text = $this->crm_translate_raw($text, NULL, $count, $plural, $context); |
3f3bba82 TO |
400 | } |
401 | } | |
3f3bba82 TO |
402 | |
403 | // replace the numbered %1, %2, etc. params if present | |
1b4710da | 404 | if (count($params) && !$raw) { |
3f3bba82 TO |
405 | $text = $this->strarg($text, $params); |
406 | } | |
407 | ||
408 | // escape SQL if we were asked for it | |
409 | if (isset($escape) and ($escape == 'sql')) { | |
2c4bba53 | 410 | $text = self::escapeSql($text); |
3f3bba82 TO |
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 | |
e97c66ff | 425 | * @param string|null $domain |
426 | * @param int|null $count | |
3f3bba82 TO |
427 | * @param string $plural |
428 | * @param string $context | |
e3b7fc11 | 429 | * |
430 | * @return string | |
3f3bba82 TO |
431 | */ |
432 | protected function crm_translate_raw($text, $domain, $count, $plural, $context) { | |
1a3cba0e ML |
433 | // gettext domain for extensions |
434 | $domain_changed = FALSE; | |
3f3bba82 TO |
435 | if (!empty($domain) && $this->_phpgettext) { |
436 | if ($this->setGettextDomain($domain)) { | |
1a3cba0e ML |
437 | $domain_changed = TRUE; |
438 | } | |
439 | } | |
440 | ||
6a488035 | 441 | // do all wildcard translations first |
a565a9b6 | 442 | |
7def74fa TO |
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__])) { | |
0d096477 | 446 | if (defined('CIVICRM_DSN') && !CRM_Core_Config::isUpgradeMode()) { |
7def74fa | 447 | Civi::$statics[__CLASS__][$replacementsLocale] = CRM_Core_BAO_WordReplacement::getLocaleCustomStrings($replacementsLocale); |
234d8f09 TO |
448 | } |
449 | else { | |
be2fb01f | 450 | Civi::$statics[__CLASS__][$replacementsLocale] = []; |
234d8f09 TO |
451 | } |
452 | } | |
7def74fa | 453 | $stringTable = Civi::$statics[__CLASS__][$replacementsLocale]; |
6a488035 TO |
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 | ||
6cf5bb6f DL |
466 | if ( |
467 | !$exactMatch && | |
6a488035 TO |
468 | isset($stringTable['enabled']['wildcardMatch']) |
469 | ) { | |
353ffa53 | 470 | $search = array_keys($stringTable['enabled']['wildcardMatch']); |
6a488035 | 471 | $replace = array_values($stringTable['enabled']['wildcardMatch']); |
353ffa53 | 472 | $text = str_replace($search, $replace, $text); |
6a488035 TO |
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 | |
be2fb01f | 492 | $text = strtr($text, ['%count' => $count]); |
6a488035 TO |
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 | ||
1a3cba0e ML |
506 | if ($domain_changed) { |
507 | $this->setGettextDomain('civicrm'); | |
508 | } | |
509 | ||
6a488035 TO |
510 | return $text; |
511 | } | |
512 | ||
513 | /** | |
514 | * Translate a string to the current locale. | |
515 | * | |
5a4f6742 CW |
516 | * @param string $string |
517 | * this string should be translated. | |
6a488035 | 518 | * |
a6c01b45 CW |
519 | * @return string |
520 | * the translated string | |
6a488035 | 521 | */ |
00be9182 | 522 | public function translate($string) { |
6a488035 TO |
523 | return ($this->_phpgettext) ? $this->_phpgettext->translate($string) : $string; |
524 | } | |
525 | ||
526 | /** | |
527 | * Localize (destructively) array values. | |
528 | * | |
5a4f6742 CW |
529 | * @param array $array |
530 | * the array for localization (in place). | |
531 | * @param array $params | |
532 | * an array of additional parameters. | |
6a488035 | 533 | */ |
408b79bf | 534 | public function localizeArray( |
6a488035 | 535 | &$array, |
be2fb01f | 536 | $params = [] |
6a488035 | 537 | ) { |
98466ff9 | 538 | $tsLocale = CRM_Core_I18n::getLocale(); |
6a488035 TO |
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 | * | |
5a4f6742 CW |
554 | * @param array $array |
555 | * the array for localization (in place). | |
6a488035 | 556 | */ |
00be9182 | 557 | public function localizeTitles(&$array) { |
6a488035 TO |
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') { | |
be2fb01f | 564 | $array[$key] = ts($value, ['context' => 'menu']); |
6a488035 TO |
565 | } |
566 | } | |
567 | } | |
568 | ||
1a3cba0e ML |
569 | /** |
570 | * Binds a gettext domain, wrapper over bindtextdomain(). | |
571 | * | |
6a0b768e TO |
572 | * @param $key |
573 | * Key of the extension (can be 'civicrm', or 'org.example.foo'). | |
1a3cba0e | 574 | * |
408b79bf | 575 | * @return Bool |
a6c01b45 | 576 | * True if the domain was changed for an extension. |
1a3cba0e | 577 | */ |
00be9182 | 578 | public function setGettextDomain($key) { |
82bcff63 | 579 | /* No domain changes for en_US */ |
353ffa53 | 580 | if (!$this->_phpgettext) { |
82bcff63 ML |
581 | return FALSE; |
582 | } | |
1a3cba0e | 583 | |
74eb462f | 584 | // It's only necessary to find/bind once |
353ffa53 | 585 | if (!isset($this->_extensioncache[$key])) { |
1a3cba0e ML |
586 | try { |
587 | $mapper = CRM_Extension_System::singleton()->getMapper(); | |
588 | $path = $mapper->keyToBasePath($key); | |
589 | $info = $mapper->keyToInfo($key); | |
590 | $domain = $info->file; | |
591 | ||
74eb462f ML |
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 | |
b52647df | 599 | $mo_file = $path . DIRECTORY_SEPARATOR . 'l10n' . DIRECTORY_SEPARATOR . $this->locale . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . $domain . '.mo'; |
74eb462f ML |
600 | $streamer = new FileReader($mo_file); |
601 | $this->_extensioncache[$key] = new gettext_reader($streamer); | |
602 | } | |
1a3cba0e ML |
603 | } |
604 | catch (CRM_Extension_Exception $e) { | |
b44e3f84 | 605 | // Intentionally not translating this string to avoid possible infinite loops |
74eb462f ML |
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; | |
1a3cba0e ML |
609 | } |
610 | } | |
611 | ||
74eb462f ML |
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; | |
1a3cba0e | 621 | } |
74eb462f ML |
622 | |
623 | return FALSE; | |
1a3cba0e ML |
624 | } |
625 | ||
07844ccf SV |
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 | ||
3a55aa6b ML |
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 | } | |
07844ccf SV |
653 | |
654 | /** | |
655 | * Change the processing language without changing the current user language | |
656 | * | |
c86241f4 SV |
657 | * @param $locale |
658 | * Locale (for example 'en_US', or 'fr_CA'). | |
07844ccf SV |
659 | * True if the domain was changed for an extension. |
660 | */ | |
c86241f4 | 661 | public function setLocale($locale) { |
fd1f3a26 SV |
662 | |
663 | // Change the language of the CMS as well, for URLs. | |
c86241f4 | 664 | CRM_Utils_System::setUFLocale($locale); |
fd1f3a26 SV |
665 | |
666 | // change the gettext ressources | |
07844ccf | 667 | if ($this->_nativegettext) { |
c86241f4 | 668 | $this->setNativeGettextLocale($locale); |
07844ccf SV |
669 | } |
670 | else { | |
671 | // phpgettext | |
c86241f4 | 672 | $this->setPhpGettextLocale($locale); |
07844ccf SV |
673 | } |
674 | ||
6b084142 | 675 | // For sql queries, if running in DB multi-lingual mode. |
53d46849 | 676 | global $dbLocale; |
6b084142 ML |
677 | |
678 | if ($dbLocale) { | |
679 | $dbLocale = "_{$locale}"; | |
680 | } | |
07844ccf | 681 | |
96cb9196 ML |
682 | // For self::getLocale() |
683 | global $tsLocale; | |
684 | $tsLocale = $locale; | |
07844ccf SV |
685 | } |
686 | ||
6a488035 TO |
687 | /** |
688 | * Static instance provider - return the instance for the current locale. | |
c4c311c1 CW |
689 | * |
690 | * @return CRM_Core_I18n | |
6a488035 | 691 | */ |
00be9182 | 692 | public static function &singleton() { |
9d65201d | 693 | if (!isset(Civi::$statics[__CLASS__]['singleton'])) { |
be2fb01f | 694 | Civi::$statics[__CLASS__]['singleton'] = []; |
9d65201d | 695 | } |
98466ff9 | 696 | $tsLocale = CRM_Core_I18n::getLocale(); |
9d65201d SL |
697 | if (!isset(Civi::$statics[__CLASS__]['singleton'][$tsLocale])) { |
698 | Civi::$statics[__CLASS__]['singleton'][$tsLocale] = new CRM_Core_I18n($tsLocale); | |
6a488035 TO |
699 | } |
700 | ||
9d65201d | 701 | return Civi::$statics[__CLASS__]['singleton'][$tsLocale]; |
6a488035 TO |
702 | } |
703 | ||
704 | /** | |
705 | * Set the LC_TIME locale if it's not set already (for a given language choice). | |
706 | * | |
a6c01b45 CW |
707 | * @return string |
708 | * the final LC_TIME that got set | |
6a488035 | 709 | */ |
00be9182 | 710 | public static function setLcTime() { |
be2fb01f | 711 | static $locales = []; |
6a488035 | 712 | |
98466ff9 | 713 | $tsLocale = CRM_Core_I18n::getLocale(); |
6a488035 TO |
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 | } | |
96025800 | 722 | |
9747df8a | 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() { | |
aaffa79f | 733 | $language = Civi::settings()->get('contact_default_language'); |
9747df8a | 734 | if ($language == 'undefined') { |
735 | return NULL; | |
736 | } | |
737 | if (empty($language) || $language === '*default*') { | |
be2fb01f | 738 | $language = civicrm_api3('setting', 'getvalue', [ |
9747df8a | 739 | 'name' => 'lcMessages', |
740 | 'group' => CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME, | |
be2fb01f | 741 | ]); |
9747df8a | 742 | } |
743 | elseif ($language == 'current_site_language') { | |
98466ff9 | 744 | return CRM_Core_I18n::getLocale(); |
9747df8a | 745 | } |
746 | ||
747 | return $language; | |
748 | } | |
749 | ||
186e53b3 | 750 | /** |
751 | * Get the current locale | |
752 | * | |
753 | * @return string | |
754 | */ | |
755 | public static function getLocale() { | |
756 | global $tsLocale; | |
338ff411 | 757 | return $tsLocale ? $tsLocale : 'en_US'; |
186e53b3 | 758 | } |
759 | ||
6a488035 TO |
760 | } |
761 | ||
762 | /** | |
763 | * Short-named function for string translation, defined in global scope so it's available everywhere. | |
764 | * | |
e3b7fc11 | 765 | * @param string $text |
6a0b768e | 766 | * String string for translating. |
16b10e64 | 767 | * @param array $params |
6a0b768e | 768 | * Array an array of additional parameters. |
6a488035 | 769 | * |
a6c01b45 | 770 | * @return string |
353ffa53 | 771 | * the translated string |
6a488035 | 772 | */ |
be2fb01f | 773 | function ts($text, $params = []) { |
852de6cf | 774 | static $bootstrapReady = FALSE; |
4954910a | 775 | static $lastLocale = NULL; |
6a488035 TO |
776 | static $i18n = NULL; |
777 | static $function = NULL; | |
778 | ||
779 | if ($text == '') { | |
780 | return ''; | |
781 | } | |
782 | ||
4954910a | 783 | // When the settings become available, lookup customTranslateFunction. |
852de6cf E |
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 | |
4954910a TO |
788 | $config = CRM_Core_Config::singleton(); |
789 | if (isset($config->customTranslateFunction) and function_exists($config->customTranslateFunction)) { | |
790 | $function = $config->customTranslateFunction; | |
791 | } | |
792 | } | |
852de6cf | 793 | else { |
2f98cf00 E |
794 | // don't _translate_ anything until bootstrap has progressed enough |
795 | $params['skip_translation'] = 1; | |
852de6cf | 796 | } |
6a488035 TO |
797 | } |
798 | ||
4954910a TO |
799 | $activeLocale = CRM_Core_I18n::getLocale(); |
800 | if (!$i18n or $lastLocale != $activeLocale) { | |
6a488035 | 801 | $i18n = CRM_Core_I18n::singleton(); |
4954910a | 802 | $lastLocale = $activeLocale; |
6a488035 TO |
803 | } |
804 | ||
805 | if ($function) { | |
806 | return $function($text, $params); | |
807 | } | |
808 | else { | |
809 | return $i18n->crm_translate($text, $params); | |
810 | } | |
811 | } |