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