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 | * | |
e144ca30 | 22 | * @var string |
07844ccf SV |
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 | /** | |
33c6d76d | 41 | * Escape a string if a mode is specified, otherwise return string unmodified. |
2c4bba53 TO |
42 | * |
43 | * @param string $text | |
33c6d76d | 44 | * @param string $mode |
2c4bba53 TO |
45 | * @return string |
46 | */ | |
33c6d76d CW |
47 | protected static function escape($text, $mode) { |
48 | switch ($mode) { | |
49 | case 'sql': | |
50 | if (self::$SQL_ESCAPER == NULL) { | |
51 | return CRM_Core_DAO::escapeString($text); | |
52 | } | |
53 | else { | |
54 | return call_user_func(self::$SQL_ESCAPER, $text); | |
55 | } | |
56 | ||
57 | case 'js': | |
58 | return substr(json_encode($text, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT), 1, -1); | |
2c4bba53 | 59 | } |
33c6d76d | 60 | return $text; |
2c4bba53 | 61 | } |
07844ccf | 62 | |
6a488035 | 63 | /** |
2ba175b6 DL |
64 | * A PHP-gettext instance for string translation; |
65 | * should stay null if the strings are not to be translated (en_US). | |
518fa0ee | 66 | * @var object |
6a488035 TO |
67 | */ |
68 | private $_phpgettext = NULL; | |
69 | ||
70 | /** | |
71 | * Whether we are using native gettext or not. | |
518fa0ee | 72 | * @var bool |
6a488035 TO |
73 | */ |
74 | private $_nativegettext = FALSE; | |
75 | ||
74eb462f ML |
76 | /** |
77 | * Gettext cache for extension domains/streamers, depending on if native or phpgettext. | |
78 | * - native gettext: we cache the value for textdomain() | |
79 | * - phpgettext: we cache the file streamer. | |
518fa0ee | 80 | * @var array |
74eb462f | 81 | */ |
be2fb01f | 82 | private $_extensioncache = []; |
74eb462f | 83 | |
0d096477 TO |
84 | /** |
85 | * @var string | |
86 | */ | |
87 | private $locale; | |
88 | ||
6a488035 TO |
89 | /** |
90 | * A locale-based constructor that shouldn't be called from outside of this class (use singleton() instead). | |
91 | * | |
5a4f6742 CW |
92 | * @param string $locale |
93 | * the base of this certain object's existence. | |
6a488035 | 94 | * |
77b97be7 | 95 | * @return \CRM_Core_I18n |
6a488035 | 96 | */ |
00be9182 | 97 | public function __construct($locale) { |
0d096477 | 98 | $this->locale = $locale; |
6a488035 | 99 | if ($locale != '' and $locale != 'en_US') { |
6a488035 TO |
100 | if (defined('CIVICRM_GETTEXT_NATIVE') && CIVICRM_GETTEXT_NATIVE && function_exists('gettext')) { |
101 | // Note: the file hierarchy for .po must be, for example: l10n/fr_FR/LC_MESSAGES/civicrm.mo | |
102 | ||
103 | $this->_nativegettext = TRUE; | |
c86241f4 | 104 | $this->setNativeGettextLocale($locale); |
6a488035 TO |
105 | return; |
106 | } | |
107 | ||
108 | // Otherwise, use PHP-gettext | |
c86241f4 | 109 | $this->setPhpGettextLocale($locale); |
6a488035 TO |
110 | } |
111 | } | |
112 | ||
113 | /** | |
114 | * Returns whether gettext is running natively or using PHP-Gettext. | |
115 | * | |
a6c01b45 CW |
116 | * @return bool |
117 | * True if gettext is native | |
6a488035 | 118 | */ |
00be9182 | 119 | public function isNative() { |
6a488035 TO |
120 | return $this->_nativegettext; |
121 | } | |
122 | ||
f2ac86d1 | 123 | /** |
124 | * Set native locale for getText. | |
125 | * | |
126 | * @param string $locale | |
127 | */ | |
c86241f4 | 128 | protected function setNativeGettextLocale($locale) { |
c86241f4 SV |
129 | $locale .= '.utf8'; |
130 | putenv("LANG=$locale"); | |
131 | ||
132 | // CRM-11833 Avoid LC_ALL because of LC_NUMERIC and potential DB error. | |
133 | setlocale(LC_TIME, $locale); | |
134 | setlocale(LC_MESSAGES, $locale); | |
135 | setlocale(LC_CTYPE, $locale); | |
136 | ||
137 | bindtextdomain('civicrm', CRM_Core_I18n::getResourceDir()); | |
138 | bind_textdomain_codeset('civicrm', 'UTF-8'); | |
139 | textdomain('civicrm'); | |
140 | ||
141 | $this->_phpgettext = new CRM_Core_I18n_NativeGettext(); | |
142 | $this->_extensioncache['civicrm'] = 'civicrm'; | |
c86241f4 SV |
143 | } |
144 | ||
f2ac86d1 | 145 | /** |
146 | * Set getText locale. | |
147 | * | |
6a0d01bb ML |
148 | * Since CiviCRM 4.5, expected dir structure is civicrm/l10n/xx_XX/LC_MESSAGES/civicrm.mo |
149 | * because that is what native gettext expects. Fallback support for the pre-4.5 structure | |
150 | * was removed in CiviCRM 5.51. | |
151 | * | |
152 | * CiviCRM 5.23 added support for the CIVICRM_L10N_BASEDIR constant (and [civicrm.l10n]) | |
153 | * so that mo files can be stored elsewhere (such as in a web-writable directory, to | |
154 | * support extensions sur as l10nupdate. | |
155 | * | |
f2ac86d1 | 156 | * @param string $locale |
157 | */ | |
c86241f4 | 158 | protected function setPhpGettextLocale($locale) { |
c86241f4 SV |
159 | require_once 'PHPgettext/streams.php'; |
160 | require_once 'PHPgettext/gettext.php'; | |
161 | ||
162 | $mo_file = CRM_Core_I18n::getResourceDir() . $locale . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . 'civicrm.mo'; | |
c86241f4 SV |
163 | $streamer = new FileReader($mo_file); |
164 | $this->_phpgettext = new gettext_reader($streamer); | |
165 | $this->_extensioncache['civicrm'] = $this->_phpgettext; | |
c86241f4 SV |
166 | } |
167 | ||
6a488035 TO |
168 | /** |
169 | * Return languages available in this instance of CiviCRM. | |
170 | * | |
5a4f6742 CW |
171 | * @param bool $justEnabled |
172 | * whether to return all languages or just the enabled ones. | |
6a488035 | 173 | * |
a6c01b45 | 174 | * @return array |
16b10e64 | 175 | * Array of code/language name mappings |
6a488035 | 176 | */ |
00be9182 | 177 | public static function languages($justEnabled = FALSE) { |
6a488035 TO |
178 | static $all = NULL; |
179 | static $enabled = NULL; | |
180 | ||
181 | if (!$all) { | |
8cab7b3d | 182 | // Use `getValues`, not `buildOptions` to bypass hook_civicrm_fieldOptions. See core#1132. |
183 | CRM_Core_OptionValue::getValues(['name' => 'languages'], $optionValues, 'weight', TRUE); | |
184 | $all = array_column($optionValues, 'label', 'name'); | |
6a488035 | 185 | |
8cab7b3d | 186 | // FIXME: How is this not duplicative of the lines above? |
ece6501c | 187 | // get labels |
be2fb01f CW |
188 | $rows = []; |
189 | $labels = []; | |
190 | CRM_Core_OptionValue::getValues(['name' => 'languages'], $rows); | |
ece6501c ML |
191 | foreach ($rows as $id => $row) { |
192 | $labels[$row['name']] = $row['label']; | |
193 | } | |
194 | ||
6a488035 | 195 | // check which ones are available; add them to $all if not there already |
be2fb01f | 196 | $codes = []; |
4d8ce7f0 | 197 | if (is_dir(CRM_Core_I18n::getResourceDir()) && $dir = opendir(CRM_Core_I18n::getResourceDir())) { |
6a488035 TO |
198 | while ($filename = readdir($dir)) { |
199 | if (preg_match('/^[a-z][a-z]_[A-Z][A-Z]$/', $filename)) { | |
200 | $codes[] = $filename; | |
201 | if (!isset($all[$filename])) { | |
8a0dba7b | 202 | $all[$filename] = $labels[$filename] ?? "($filename)"; |
6a488035 TO |
203 | } |
204 | } | |
205 | } | |
206 | closedir($dir); | |
207 | } | |
208 | ||
209 | // drop the unavailable languages (except en_US) | |
210 | foreach (array_keys($all) as $code) { | |
211 | if ($code == 'en_US') { | |
212 | continue; | |
213 | } | |
408b79bf | 214 | if (!in_array($code, $codes)) { |
215 | unset($all[$code]); | |
216 | } | |
6a488035 | 217 | } |
ece6501c | 218 | |
921ed8ae | 219 | asort($all); |
6a488035 TO |
220 | } |
221 | ||
222 | if ($enabled === NULL) { | |
cc493f5e | 223 | $languageLimit = Civi::settings()->get('languageLimit'); |
be2fb01f | 224 | $enabled = []; |
cc493f5e | 225 | if ($languageLimit) { |
6a488035 | 226 | foreach ($all as $code => $name) { |
cc493f5e | 227 | if (array_key_exists($code, $languageLimit)) { |
6a488035 TO |
228 | $enabled[$code] = $name; |
229 | } | |
230 | } | |
231 | } | |
232 | } | |
233 | ||
234 | return $justEnabled ? $enabled : $all; | |
235 | } | |
236 | ||
57eda25d EM |
237 | /** |
238 | * Get the options available for format locale. | |
239 | * | |
240 | * Note the pseudoconstant can't be used as the key is the name not the value. | |
241 | * | |
242 | * @return array | |
243 | */ | |
9d14b6f7 | 244 | public static function getFormatLocales(): array { |
57eda25d | 245 | $values = CRM_Core_OptionValue::getValues(['name' => 'languages'], $optionValues, 'label', TRUE); |
afd5a927 | 246 | $return = []; |
9d14b6f7 | 247 | $return[NULL] = ts('Inherit from language'); |
57eda25d EM |
248 | foreach ($values as $value) { |
249 | $return[$value['name']] = $value['label']; | |
250 | } | |
afd5a927 EM |
251 | // Sorry not sorry. |
252 | // Hacking in for now since the is probably the most important use-case for | |
253 | // money formatting in an English speaking non-US locale based on any reasonable | |
254 | // metric. | |
79f4e0bd | 255 | $return['en_NZ'] = ts('English (New Zealand)'); |
57eda25d EM |
256 | return $return; |
257 | } | |
258 | ||
921ed8ae AS |
259 | /** |
260 | * Return the available UI languages | |
518fa0ee SL |
261 | * @return array|string |
262 | * array(string languageCode => string languageName) if !$justCodes | |
921ed8ae AS |
263 | */ |
264 | public static function uiLanguages($justCodes = FALSE) { | |
265 | // In multilang we only allow the languages that are configured in db | |
266 | // Otherwise, the languages configured in uiLanguages | |
267 | $settings = Civi::settings(); | |
268 | if (CRM_Core_I18n::isMultiLingual()) { | |
269 | $codes = array_keys((array) $settings->get('languageLimit')); | |
270 | } | |
271 | else { | |
272 | $codes = $settings->get('uiLanguages'); | |
273 | if (!$codes) { | |
274 | $codes = [$settings->get('lcMessages')]; | |
275 | } | |
276 | } | |
277 | return $justCodes ? $codes | |
278 | : CRM_Utils_Array::subset(CRM_Core_I18n::languages(), $codes); | |
279 | } | |
280 | ||
6a488035 TO |
281 | /** |
282 | * Replace arguments in a string with their values. Arguments are represented by % followed by their number. | |
283 | * | |
5a4f6742 CW |
284 | * @param string $str |
285 | * source string. | |
6a488035 | 286 | * |
a6c01b45 CW |
287 | * @return string |
288 | * modified string | |
6a488035 | 289 | */ |
00be9182 | 290 | public function strarg($str) { |
be2fb01f | 291 | $tr = []; |
6a488035 TO |
292 | $p = 0; |
293 | for ($i = 1; $i < func_num_args(); $i++) { | |
294 | $arg = func_get_arg($i); | |
295 | if (is_array($arg)) { | |
296 | foreach ($arg as $aarg) { | |
297 | $tr['%' . ++$p] = $aarg; | |
298 | } | |
299 | } | |
300 | else { | |
301 | $tr['%' . ++$p] = $arg; | |
302 | } | |
303 | } | |
304 | return strtr($str, $tr); | |
305 | } | |
306 | ||
f2ac86d1 | 307 | /** |
308 | * Get the directory for l10n resources. | |
309 | * | |
310 | * @return string | |
311 | */ | |
4d8ce7f0 | 312 | public static function getResourceDir() { |
f64df9e7 | 313 | return CRM_Utils_File::addTrailingSlash(\Civi::paths()->getPath('[civicrm.l10n]/.')); |
4d8ce7f0 TO |
314 | } |
315 | ||
6a488035 TO |
316 | /** |
317 | * Smarty block function, provides gettext support for smarty. | |
318 | * | |
319 | * The block content is the text that should be translated. | |
320 | * | |
321 | * Any parameter that is sent to the function will be represented as %n in the translation text, | |
322 | * where n is 1 for the first parameter. The following parameters are reserved: | |
323 | * - escape - sets escape mode: | |
324 | * - 'html' for HTML escaping, this is the default. | |
325 | * - 'js' for javascript escaping. | |
326 | * - 'no'/'off'/0 - turns off escaping | |
327 | * - plural - The plural version of the text (2nd parameter of ngettext()) | |
328 | * - count - The item count for plural mode (3rd parameter of ngettext()) | |
329 | * - context - gettext context of that string (for homonym handling) | |
330 | * | |
5a4f6742 CW |
331 | * @param string $text |
332 | * the original string. | |
333 | * @param array $params | |
3f3bba82 TO |
334 | * The params of the translation (if any). |
335 | * - domain: string|array a list of translation domains to search (in order) | |
336 | * - context: string | |
69e812be | 337 | * - skip_translation: flag (do only escape/replacement, skip the actual translation) |
e3b7fc11 | 338 | * |
a6c01b45 CW |
339 | * @return string |
340 | * the translated string | |
6a488035 | 341 | */ |
be2fb01f | 342 | public function crm_translate($text, $params = []) { |
33c6d76d CW |
343 | $escape = $params['escape'] ?? NULL; |
344 | unset($params['escape']); | |
6a488035 TO |
345 | |
346 | // sometimes we need to {ts}-tag a string, but don’t want to | |
347 | // translate it in the template (like civicrm_navigation.tpl), | |
348 | // because we handle the translation in a different way (CRM-6998) | |
349 | // in such cases we return early, only doing SQL/JS escaping | |
350 | if (isset($params['skip']) and $params['skip']) { | |
33c6d76d | 351 | return self::escape($text, $escape); |
6a488035 TO |
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 | ||
33c6d76d | 408 | return self::escape($text, $escape); |
3f3bba82 TO |
409 | } |
410 | ||
411 | /** | |
412 | * Lookup the raw translation of a string (without any extra escaping or interpolation). | |
413 | * | |
414 | * @param string $text | |
e97c66ff | 415 | * @param string|null $domain |
416 | * @param int|null $count | |
3f3bba82 TO |
417 | * @param string $plural |
418 | * @param string $context | |
e3b7fc11 | 419 | * |
420 | * @return string | |
3f3bba82 TO |
421 | */ |
422 | protected function crm_translate_raw($text, $domain, $count, $plural, $context) { | |
1a3cba0e ML |
423 | // gettext domain for extensions |
424 | $domain_changed = FALSE; | |
3f3bba82 TO |
425 | if (!empty($domain) && $this->_phpgettext) { |
426 | if ($this->setGettextDomain($domain)) { | |
1a3cba0e ML |
427 | $domain_changed = TRUE; |
428 | } | |
429 | } | |
430 | ||
6a488035 | 431 | // do all wildcard translations first |
a565a9b6 | 432 | |
9d114ab6 | 433 | $stringTable = $this->getWordReplacements(); |
6a488035 TO |
434 | |
435 | $exactMatch = FALSE; | |
436 | if (isset($stringTable['enabled']['exactMatch'])) { | |
437 | foreach ($stringTable['enabled']['exactMatch'] as $search => $replace) { | |
438 | if ($search === $text) { | |
439 | $exactMatch = TRUE; | |
440 | $text = $replace; | |
441 | break; | |
442 | } | |
443 | } | |
444 | } | |
445 | ||
6cf5bb6f DL |
446 | if ( |
447 | !$exactMatch && | |
6a488035 TO |
448 | isset($stringTable['enabled']['wildcardMatch']) |
449 | ) { | |
353ffa53 | 450 | $search = array_keys($stringTable['enabled']['wildcardMatch']); |
6a488035 | 451 | $replace = array_values($stringTable['enabled']['wildcardMatch']); |
353ffa53 | 452 | $text = str_replace($search, $replace, $text); |
6a488035 TO |
453 | } |
454 | ||
455 | // dont translate if we've done exactMatch already | |
456 | if (!$exactMatch) { | |
457 | // use plural if required parameters are set | |
458 | if (isset($count) && isset($plural)) { | |
459 | ||
460 | if ($this->_phpgettext) { | |
26ff2e24 | 461 | $text = $this->_phpgettext->ngettext($text, $plural, (int) $count); |
6a488035 TO |
462 | } |
463 | else { | |
464 | // if the locale's not set, we do ngettext work by hand | |
465 | // if $count == 1 then $text = $text, else $text = $plural | |
466 | if ($count != 1) { | |
467 | $text = $plural; | |
468 | } | |
469 | } | |
470 | ||
471 | // expand %count in translated string to $count | |
be2fb01f | 472 | $text = strtr($text, ['%count' => $count]); |
6a488035 TO |
473 | |
474 | // if not plural, but the locale's set, translate | |
475 | } | |
476 | elseif ($this->_phpgettext) { | |
477 | if ($context) { | |
478 | $text = $this->_phpgettext->pgettext($context, $text); | |
479 | } | |
480 | else { | |
481 | $text = $this->_phpgettext->translate($text); | |
482 | } | |
483 | } | |
484 | } | |
485 | ||
1a3cba0e ML |
486 | if ($domain_changed) { |
487 | $this->setGettextDomain('civicrm'); | |
488 | } | |
489 | ||
6a488035 TO |
490 | return $text; |
491 | } | |
492 | ||
493 | /** | |
494 | * Translate a string to the current locale. | |
495 | * | |
5a4f6742 CW |
496 | * @param string $string |
497 | * this string should be translated. | |
6a488035 | 498 | * |
a6c01b45 CW |
499 | * @return string |
500 | * the translated string | |
6a488035 | 501 | */ |
00be9182 | 502 | public function translate($string) { |
6a488035 TO |
503 | return ($this->_phpgettext) ? $this->_phpgettext->translate($string) : $string; |
504 | } | |
505 | ||
506 | /** | |
507 | * Localize (destructively) array values. | |
508 | * | |
5a4f6742 CW |
509 | * @param array $array |
510 | * the array for localization (in place). | |
511 | * @param array $params | |
512 | * an array of additional parameters. | |
6a488035 | 513 | */ |
408b79bf | 514 | public function localizeArray( |
6a488035 | 515 | &$array, |
be2fb01f | 516 | $params = [] |
6a488035 | 517 | ) { |
98466ff9 | 518 | $tsLocale = CRM_Core_I18n::getLocale(); |
6a488035 TO |
519 | |
520 | if ($tsLocale == 'en_US') { | |
521 | return; | |
522 | } | |
523 | ||
524 | foreach ($array as & $value) { | |
525 | if ($value) { | |
526 | $value = ts($value, $params); | |
527 | } | |
528 | } | |
529 | } | |
530 | ||
531 | /** | |
532 | * Localize (destructively) array elements with keys of 'title'. | |
533 | * | |
5a4f6742 CW |
534 | * @param array $array |
535 | * the array for localization (in place). | |
6a488035 | 536 | */ |
00be9182 | 537 | public function localizeTitles(&$array) { |
6a488035 TO |
538 | foreach ($array as $key => $value) { |
539 | if (is_array($value)) { | |
540 | $this->localizeTitles($value); | |
541 | $array[$key] = $value; | |
542 | } | |
543 | elseif ((string ) $key == 'title') { | |
be2fb01f | 544 | $array[$key] = ts($value, ['context' => 'menu']); |
6a488035 TO |
545 | } |
546 | } | |
547 | } | |
548 | ||
1a3cba0e ML |
549 | /** |
550 | * Binds a gettext domain, wrapper over bindtextdomain(). | |
551 | * | |
3fd42bb5 | 552 | * @param string $key |
6a0b768e | 553 | * Key of the extension (can be 'civicrm', or 'org.example.foo'). |
1a3cba0e | 554 | * |
408b79bf | 555 | * @return Bool |
a6c01b45 | 556 | * True if the domain was changed for an extension. |
1a3cba0e | 557 | */ |
00be9182 | 558 | public function setGettextDomain($key) { |
82bcff63 | 559 | /* No domain changes for en_US */ |
353ffa53 | 560 | if (!$this->_phpgettext) { |
82bcff63 ML |
561 | return FALSE; |
562 | } | |
1a3cba0e | 563 | |
74eb462f | 564 | // It's only necessary to find/bind once |
353ffa53 | 565 | if (!isset($this->_extensioncache[$key])) { |
1a3cba0e ML |
566 | try { |
567 | $mapper = CRM_Extension_System::singleton()->getMapper(); | |
568 | $path = $mapper->keyToBasePath($key); | |
569 | $info = $mapper->keyToInfo($key); | |
570 | $domain = $info->file; | |
571 | ||
74eb462f ML |
572 | if ($this->_nativegettext) { |
573 | bindtextdomain($domain, $path . DIRECTORY_SEPARATOR . 'l10n'); | |
574 | bind_textdomain_codeset($domain, 'UTF-8'); | |
575 | $this->_extensioncache[$key] = $domain; | |
576 | } | |
577 | else { | |
578 | // phpgettext | |
b52647df | 579 | $mo_file = $path . DIRECTORY_SEPARATOR . 'l10n' . DIRECTORY_SEPARATOR . $this->locale . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . $domain . '.mo'; |
74eb462f | 580 | $streamer = new FileReader($mo_file); |
c282ea69 | 581 | $this->_extensioncache[$key] = $streamer->length() ? new gettext_reader($streamer) : NULL; |
74eb462f | 582 | } |
1a3cba0e ML |
583 | } |
584 | catch (CRM_Extension_Exception $e) { | |
b44e3f84 | 585 | // Intentionally not translating this string to avoid possible infinite loops |
74eb462f ML |
586 | // Only developers should see this string, if they made a mistake in their ts() usage. |
587 | CRM_Core_Session::setStatus('Unknown extension key in a translation string: ' . $key, '', 'error'); | |
588 | $this->_extensioncache[$key] = FALSE; | |
1a3cba0e ML |
589 | } |
590 | } | |
591 | ||
74eb462f ML |
592 | if (isset($this->_extensioncache[$key]) && $this->_extensioncache[$key]) { |
593 | if ($this->_nativegettext) { | |
594 | textdomain($this->_extensioncache[$key]); | |
595 | } | |
596 | else { | |
597 | $this->_phpgettext = $this->_extensioncache[$key]; | |
598 | } | |
599 | ||
600 | return TRUE; | |
1a3cba0e | 601 | } |
74eb462f ML |
602 | |
603 | return FALSE; | |
1a3cba0e ML |
604 | } |
605 | ||
07844ccf | 606 | /** |
864c22c3 | 607 | * Is the current CiviCRM domain in multilingual mode. |
07844ccf | 608 | * |
394d18d3 | 609 | * @return bool |
07844ccf SV |
610 | * True if CiviCRM is in multilingual mode. |
611 | */ | |
612 | public static function isMultilingual() { | |
d357f225 CW |
613 | $domain = CRM_Core_BAO_Domain::getDomain(); |
614 | return (bool) $domain->locales; | |
394d18d3 CW |
615 | } |
616 | ||
617 | /** | |
618 | * Returns languages if domain is in multilingual mode. | |
619 | * | |
620 | * @return array|bool | |
621 | */ | |
622 | public static function getMultilingual() { | |
d357f225 CW |
623 | $domain = CRM_Core_BAO_Domain::getDomain(); |
624 | return $domain->locales ? CRM_Core_DAO::unSerializeField($domain->locales, CRM_Core_DAO::SERIALIZE_SEPARATOR_TRIMMED) : FALSE; | |
07844ccf SV |
625 | } |
626 | ||
3a55aa6b ML |
627 | /** |
628 | * Is the language written "right-to-left"? | |
629 | * | |
3fd42bb5 | 630 | * @param string $language |
3a55aa6b ML |
631 | * Language (for example 'en_US', or 'fr_CA'). |
632 | * | |
394d18d3 | 633 | * @return bool |
3a55aa6b ML |
634 | * True if it is an RTL language. |
635 | */ | |
636 | public static function isLanguageRTL($language) { | |
637 | $rtl = CRM_Core_I18n_PseudoConstant::getRTLlanguages(); | |
638 | $short = CRM_Core_I18n_PseudoConstant::shortForLong($language); | |
639 | ||
640 | return (in_array($short, $rtl)); | |
641 | } | |
07844ccf | 642 | |
8731b5ab TO |
643 | /** |
644 | * If you switch back/forth between locales/drivers, it may be necessary | |
645 | * to reset some options. | |
646 | */ | |
647 | protected function reactivate() { | |
648 | if ($this->_nativegettext) { | |
649 | $this->setNativeGettextLocale($this->locale); | |
650 | } | |
651 | ||
652 | } | |
653 | ||
07844ccf SV |
654 | /** |
655 | * Change the processing language without changing the current user language | |
656 | * | |
3fd42bb5 | 657 | * @param string $locale |
c86241f4 | 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 | 665 | |
6b084142 | 666 | // For sql queries, if running in DB multi-lingual mode. |
53d46849 | 667 | global $dbLocale; |
6b084142 ML |
668 | |
669 | if ($dbLocale) { | |
670 | $dbLocale = "_{$locale}"; | |
671 | } | |
07844ccf | 672 | |
96cb9196 ML |
673 | // For self::getLocale() |
674 | global $tsLocale; | |
675 | $tsLocale = $locale; | |
8731b5ab TO |
676 | |
677 | CRM_Core_I18n::singleton()->reactivate(); | |
07844ccf SV |
678 | } |
679 | ||
6a488035 TO |
680 | /** |
681 | * Static instance provider - return the instance for the current locale. | |
c4c311c1 CW |
682 | * |
683 | * @return CRM_Core_I18n | |
6a488035 | 684 | */ |
00be9182 | 685 | public static function &singleton() { |
9d65201d | 686 | if (!isset(Civi::$statics[__CLASS__]['singleton'])) { |
be2fb01f | 687 | Civi::$statics[__CLASS__]['singleton'] = []; |
9d65201d | 688 | } |
98466ff9 | 689 | $tsLocale = CRM_Core_I18n::getLocale(); |
9d65201d SL |
690 | if (!isset(Civi::$statics[__CLASS__]['singleton'][$tsLocale])) { |
691 | Civi::$statics[__CLASS__]['singleton'][$tsLocale] = new CRM_Core_I18n($tsLocale); | |
6a488035 TO |
692 | } |
693 | ||
9d65201d | 694 | return Civi::$statics[__CLASS__]['singleton'][$tsLocale]; |
6a488035 TO |
695 | } |
696 | ||
697 | /** | |
698 | * Set the LC_TIME locale if it's not set already (for a given language choice). | |
699 | * | |
a6c01b45 CW |
700 | * @return string |
701 | * the final LC_TIME that got set | |
6a488035 | 702 | */ |
00be9182 | 703 | public static function setLcTime() { |
be2fb01f | 704 | static $locales = []; |
6a488035 | 705 | |
98466ff9 | 706 | $tsLocale = CRM_Core_I18n::getLocale(); |
6a488035 TO |
707 | if (!isset($locales[$tsLocale])) { |
708 | // with the config being set to pl_PL: try pl_PL.UTF-8, | |
709 | // then pl_PL, if neither present fall back to C | |
710 | $locales[$tsLocale] = setlocale(LC_TIME, $tsLocale . '.UTF-8', $tsLocale, 'C'); | |
711 | } | |
712 | ||
713 | return $locales[$tsLocale]; | |
714 | } | |
96025800 | 715 | |
9747df8a | 716 | /** |
717 | * Get the default language for contacts where no language is provided. | |
718 | * | |
719 | * Note that NULL is a valid option so be careful with checking for empty etc. | |
720 | * | |
721 | * NULL would mean 'we don't know & we don't want to hazard a guess'. | |
722 | * | |
723 | * @return string | |
724 | */ | |
725 | public static function getContactDefaultLanguage() { | |
aaffa79f | 726 | $language = Civi::settings()->get('contact_default_language'); |
9747df8a | 727 | if ($language == 'undefined') { |
728 | return NULL; | |
729 | } | |
730 | if (empty($language) || $language === '*default*') { | |
be2fb01f | 731 | $language = civicrm_api3('setting', 'getvalue', [ |
9747df8a | 732 | 'name' => 'lcMessages', |
733 | 'group' => CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME, | |
be2fb01f | 734 | ]); |
9747df8a | 735 | } |
736 | elseif ($language == 'current_site_language') { | |
98466ff9 | 737 | return CRM_Core_I18n::getLocale(); |
9747df8a | 738 | } |
739 | ||
740 | return $language; | |
741 | } | |
742 | ||
186e53b3 | 743 | /** |
744 | * Get the current locale | |
745 | * | |
746 | * @return string | |
747 | */ | |
748 | public static function getLocale() { | |
749 | global $tsLocale; | |
338ff411 | 750 | return $tsLocale ? $tsLocale : 'en_US'; |
186e53b3 | 751 | } |
752 | ||
9d114ab6 TO |
753 | /** |
754 | * @return array | |
755 | * Ex: $stringTable['enabled']['wildcardMatch']['foo'] = 'bar'; | |
756 | */ | |
757 | private function getWordReplacements() { | |
3f0d4748 | 758 | if (isset(Civi\Test::$statics['testPreInstall'])) { |
9d114ab6 TO |
759 | return []; |
760 | } | |
761 | ||
762 | // FIXME: Is there a constant we can reference instead of hardcoding en_US? | |
763 | $replacementsLocale = $this->locale ? $this->locale : 'en_US'; | |
764 | if ((!isset(Civi::$statics[__CLASS__]) || !array_key_exists($replacementsLocale, Civi::$statics[__CLASS__]))) { | |
765 | if (defined('CIVICRM_DSN') && !CRM_Core_Config::isUpgradeMode()) { | |
766 | Civi::$statics[__CLASS__][$replacementsLocale] = CRM_Core_BAO_WordReplacement::getLocaleCustomStrings($replacementsLocale); | |
767 | } | |
768 | else { | |
769 | Civi::$statics[__CLASS__][$replacementsLocale] = []; | |
770 | } | |
771 | } | |
772 | return Civi::$statics[__CLASS__][$replacementsLocale]; | |
773 | } | |
774 | ||
6a488035 TO |
775 | } |
776 | ||
777 | /** | |
778 | * Short-named function for string translation, defined in global scope so it's available everywhere. | |
779 | * | |
e3b7fc11 | 780 | * @param string $text |
6a0b768e | 781 | * String string for translating. |
16b10e64 | 782 | * @param array $params |
6a0b768e | 783 | * Array an array of additional parameters. |
6a488035 | 784 | * |
a6c01b45 | 785 | * @return string |
353ffa53 | 786 | * the translated string |
6a488035 | 787 | */ |
be2fb01f | 788 | function ts($text, $params = []) { |
852de6cf | 789 | static $bootstrapReady = FALSE; |
4954910a | 790 | static $lastLocale = NULL; |
6a488035 TO |
791 | static $i18n = NULL; |
792 | static $function = NULL; | |
793 | ||
794 | if ($text == '') { | |
795 | return ''; | |
796 | } | |
797 | ||
4954910a | 798 | // When the settings become available, lookup customTranslateFunction. |
852de6cf E |
799 | if (!$bootstrapReady) { |
800 | $bootstrapReady = (bool) \Civi\Core\Container::isContainerBooted(); | |
801 | if ($bootstrapReady) { | |
802 | // just got ready: determine whether there is a working custom translation function | |
4954910a | 803 | $config = CRM_Core_Config::singleton(); |
fc651f91 | 804 | if (!empty($config->customTranslateFunction) && function_exists($config->customTranslateFunction)) { |
4954910a TO |
805 | $function = $config->customTranslateFunction; |
806 | } | |
807 | } | |
6a488035 TO |
808 | } |
809 | ||
4954910a TO |
810 | $activeLocale = CRM_Core_I18n::getLocale(); |
811 | if (!$i18n or $lastLocale != $activeLocale) { | |
6a488035 | 812 | $i18n = CRM_Core_I18n::singleton(); |
4954910a | 813 | $lastLocale = $activeLocale; |
6a488035 TO |
814 | } |
815 | ||
816 | if ($function) { | |
817 | return $function($text, $params); | |
818 | } | |
819 | else { | |
820 | return $i18n->crm_translate($text, $params); | |
821 | } | |
822 | } |