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