Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
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 | |
8c9251b3 | 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
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 | */ | |
87 | private $_extensioncache = array(); | |
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 ML |
194 | // get labels |
195 | $rows = array(); | |
196 | $labels = array(); | |
197 | CRM_Core_OptionValue::getValues(array('name' => 'languages'), $rows); | |
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 |
6a488035 | 203 | $codes = array(); |
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 ML |
225 | |
226 | ksort($all); | |
6a488035 TO |
227 | } |
228 | ||
229 | if ($enabled === NULL) { | |
230 | $config = CRM_Core_Config::singleton(); | |
231 | $enabled = array(); | |
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 | ||
244 | /** | |
245 | * Replace arguments in a string with their values. Arguments are represented by % followed by their number. | |
246 | * | |
5a4f6742 CW |
247 | * @param string $str |
248 | * source string. | |
6a488035 | 249 | * |
a6c01b45 CW |
250 | * @return string |
251 | * modified string | |
6a488035 | 252 | */ |
00be9182 | 253 | public function strarg($str) { |
6a488035 TO |
254 | $tr = array(); |
255 | $p = 0; | |
256 | for ($i = 1; $i < func_num_args(); $i++) { | |
257 | $arg = func_get_arg($i); | |
258 | if (is_array($arg)) { | |
259 | foreach ($arg as $aarg) { | |
260 | $tr['%' . ++$p] = $aarg; | |
261 | } | |
262 | } | |
263 | else { | |
264 | $tr['%' . ++$p] = $arg; | |
265 | } | |
266 | } | |
267 | return strtr($str, $tr); | |
268 | } | |
269 | ||
f2ac86d1 | 270 | /** |
271 | * Get the directory for l10n resources. | |
272 | * | |
273 | * @return string | |
274 | */ | |
4d8ce7f0 TO |
275 | public static function getResourceDir() { |
276 | static $dir = NULL; | |
277 | if ($dir === NULL) { | |
278 | $dir = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'l10n' . DIRECTORY_SEPARATOR; | |
279 | } | |
280 | return $dir; | |
281 | } | |
282 | ||
6a488035 TO |
283 | /** |
284 | * Smarty block function, provides gettext support for smarty. | |
285 | * | |
286 | * The block content is the text that should be translated. | |
287 | * | |
288 | * Any parameter that is sent to the function will be represented as %n in the translation text, | |
289 | * where n is 1 for the first parameter. The following parameters are reserved: | |
290 | * - escape - sets escape mode: | |
291 | * - 'html' for HTML escaping, this is the default. | |
292 | * - 'js' for javascript escaping. | |
293 | * - 'no'/'off'/0 - turns off escaping | |
294 | * - plural - The plural version of the text (2nd parameter of ngettext()) | |
295 | * - count - The item count for plural mode (3rd parameter of ngettext()) | |
296 | * - context - gettext context of that string (for homonym handling) | |
297 | * | |
5a4f6742 CW |
298 | * @param string $text |
299 | * the original string. | |
300 | * @param array $params | |
3f3bba82 TO |
301 | * The params of the translation (if any). |
302 | * - domain: string|array a list of translation domains to search (in order) | |
303 | * - context: string | |
e3b7fc11 | 304 | * |
a6c01b45 CW |
305 | * @return string |
306 | * the translated string | |
6a488035 | 307 | */ |
00be9182 | 308 | public function crm_translate($text, $params = array()) { |
6a488035 TO |
309 | if (isset($params['escape'])) { |
310 | $escape = $params['escape']; | |
311 | unset($params['escape']); | |
312 | } | |
313 | ||
314 | // sometimes we need to {ts}-tag a string, but don’t want to | |
315 | // translate it in the template (like civicrm_navigation.tpl), | |
316 | // because we handle the translation in a different way (CRM-6998) | |
317 | // in such cases we return early, only doing SQL/JS escaping | |
318 | if (isset($params['skip']) and $params['skip']) { | |
319 | if (isset($escape) and ($escape == 'sql')) { | |
2c4bba53 | 320 | $text = self::escapeSql($text); |
6a488035 TO |
321 | } |
322 | if (isset($escape) and ($escape == 'js')) { | |
323 | $text = addcslashes($text, "'"); | |
324 | } | |
325 | return $text; | |
326 | } | |
327 | ||
3f3bba82 | 328 | $plural = $count = NULL; |
6a488035 TO |
329 | if (isset($params['plural'])) { |
330 | $plural = $params['plural']; | |
331 | unset($params['plural']); | |
332 | if (isset($params['count'])) { | |
333 | $count = $params['count']; | |
334 | } | |
335 | } | |
336 | ||
337 | if (isset($params['context'])) { | |
338 | $context = $params['context']; | |
339 | unset($params['context']); | |
340 | } | |
341 | else { | |
342 | $context = NULL; | |
343 | } | |
344 | ||
3f3bba82 TO |
345 | if (isset($params['domain'])) { |
346 | $domain = $params['domain']; | |
347 | unset($params['domain']); | |
348 | } | |
349 | else { | |
350 | $domain = NULL; | |
351 | } | |
352 | ||
1b4710da TO |
353 | $raw = !empty($params['raw']); |
354 | unset($params['raw']); | |
355 | ||
3f3bba82 TO |
356 | if (!empty($domain)) { |
357 | // It might be prettier to cast to an array, but this is high-traffic stuff. | |
358 | if (is_array($domain)) { | |
359 | foreach ($domain as $d) { | |
360 | $candidate = $this->crm_translate_raw($text, $d, $count, $plural, $context); | |
361 | if ($candidate != $text) { | |
362 | $text = $candidate; | |
363 | break; | |
364 | } | |
365 | } | |
366 | } | |
367 | else { | |
368 | $text = $this->crm_translate_raw($text, $domain, $count, $plural, $context); | |
369 | } | |
370 | } | |
371 | else { | |
372 | $text = $this->crm_translate_raw($text, NULL, $count, $plural, $context); | |
373 | } | |
374 | ||
375 | // replace the numbered %1, %2, etc. params if present | |
1b4710da | 376 | if (count($params) && !$raw) { |
3f3bba82 TO |
377 | $text = $this->strarg($text, $params); |
378 | } | |
379 | ||
380 | // escape SQL if we were asked for it | |
381 | if (isset($escape) and ($escape == 'sql')) { | |
2c4bba53 | 382 | $text = self::escapeSql($text); |
3f3bba82 TO |
383 | } |
384 | ||
385 | // escape for JavaScript (if requested) | |
386 | if (isset($escape) and ($escape == 'js')) { | |
387 | $text = addcslashes($text, "'"); | |
388 | } | |
389 | ||
390 | return $text; | |
391 | } | |
392 | ||
393 | /** | |
394 | * Lookup the raw translation of a string (without any extra escaping or interpolation). | |
395 | * | |
396 | * @param string $text | |
397 | * @param string|NULL $domain | |
398 | * @param int|NULL $count | |
399 | * @param string $plural | |
400 | * @param string $context | |
e3b7fc11 | 401 | * |
402 | * @return string | |
3f3bba82 TO |
403 | */ |
404 | protected function crm_translate_raw($text, $domain, $count, $plural, $context) { | |
1a3cba0e ML |
405 | // gettext domain for extensions |
406 | $domain_changed = FALSE; | |
3f3bba82 TO |
407 | if (!empty($domain) && $this->_phpgettext) { |
408 | if ($this->setGettextDomain($domain)) { | |
1a3cba0e ML |
409 | $domain_changed = TRUE; |
410 | } | |
411 | } | |
412 | ||
6a488035 | 413 | // do all wildcard translations first |
a565a9b6 | 414 | |
7def74fa TO |
415 | // FIXME: Is there a constant we can reference instead of hardcoding en_US? |
416 | $replacementsLocale = $this->locale ? $this->locale : 'en_US'; | |
417 | if (!isset(Civi::$statics[__CLASS__]) || !array_key_exists($replacementsLocale, Civi::$statics[__CLASS__])) { | |
0d096477 | 418 | if (defined('CIVICRM_DSN') && !CRM_Core_Config::isUpgradeMode()) { |
7def74fa | 419 | Civi::$statics[__CLASS__][$replacementsLocale] = CRM_Core_BAO_WordReplacement::getLocaleCustomStrings($replacementsLocale); |
234d8f09 TO |
420 | } |
421 | else { | |
7def74fa | 422 | Civi::$statics[__CLASS__][$replacementsLocale] = array(); |
234d8f09 TO |
423 | } |
424 | } | |
7def74fa | 425 | $stringTable = Civi::$statics[__CLASS__][$replacementsLocale]; |
6a488035 TO |
426 | |
427 | $exactMatch = FALSE; | |
428 | if (isset($stringTable['enabled']['exactMatch'])) { | |
429 | foreach ($stringTable['enabled']['exactMatch'] as $search => $replace) { | |
430 | if ($search === $text) { | |
431 | $exactMatch = TRUE; | |
432 | $text = $replace; | |
433 | break; | |
434 | } | |
435 | } | |
436 | } | |
437 | ||
6cf5bb6f DL |
438 | if ( |
439 | !$exactMatch && | |
6a488035 TO |
440 | isset($stringTable['enabled']['wildcardMatch']) |
441 | ) { | |
353ffa53 | 442 | $search = array_keys($stringTable['enabled']['wildcardMatch']); |
6a488035 | 443 | $replace = array_values($stringTable['enabled']['wildcardMatch']); |
353ffa53 | 444 | $text = str_replace($search, $replace, $text); |
6a488035 TO |
445 | } |
446 | ||
447 | // dont translate if we've done exactMatch already | |
448 | if (!$exactMatch) { | |
449 | // use plural if required parameters are set | |
450 | if (isset($count) && isset($plural)) { | |
451 | ||
452 | if ($this->_phpgettext) { | |
453 | $text = $this->_phpgettext->ngettext($text, $plural, $count); | |
454 | } | |
455 | else { | |
456 | // if the locale's not set, we do ngettext work by hand | |
457 | // if $count == 1 then $text = $text, else $text = $plural | |
458 | if ($count != 1) { | |
459 | $text = $plural; | |
460 | } | |
461 | } | |
462 | ||
463 | // expand %count in translated string to $count | |
464 | $text = strtr($text, array('%count' => $count)); | |
465 | ||
466 | // if not plural, but the locale's set, translate | |
467 | } | |
468 | elseif ($this->_phpgettext) { | |
469 | if ($context) { | |
470 | $text = $this->_phpgettext->pgettext($context, $text); | |
471 | } | |
472 | else { | |
473 | $text = $this->_phpgettext->translate($text); | |
474 | } | |
475 | } | |
476 | } | |
477 | ||
1a3cba0e ML |
478 | if ($domain_changed) { |
479 | $this->setGettextDomain('civicrm'); | |
480 | } | |
481 | ||
6a488035 TO |
482 | return $text; |
483 | } | |
484 | ||
485 | /** | |
486 | * Translate a string to the current locale. | |
487 | * | |
5a4f6742 CW |
488 | * @param string $string |
489 | * this string should be translated. | |
6a488035 | 490 | * |
a6c01b45 CW |
491 | * @return string |
492 | * the translated string | |
6a488035 | 493 | */ |
00be9182 | 494 | public function translate($string) { |
6a488035 TO |
495 | return ($this->_phpgettext) ? $this->_phpgettext->translate($string) : $string; |
496 | } | |
497 | ||
498 | /** | |
499 | * Localize (destructively) array values. | |
500 | * | |
5a4f6742 CW |
501 | * @param array $array |
502 | * the array for localization (in place). | |
503 | * @param array $params | |
504 | * an array of additional parameters. | |
6a488035 | 505 | */ |
408b79bf | 506 | public function localizeArray( |
6a488035 TO |
507 | &$array, |
508 | $params = array() | |
509 | ) { | |
98466ff9 | 510 | $tsLocale = CRM_Core_I18n::getLocale(); |
6a488035 TO |
511 | |
512 | if ($tsLocale == 'en_US') { | |
513 | return; | |
514 | } | |
515 | ||
516 | foreach ($array as & $value) { | |
517 | if ($value) { | |
518 | $value = ts($value, $params); | |
519 | } | |
520 | } | |
521 | } | |
522 | ||
523 | /** | |
524 | * Localize (destructively) array elements with keys of 'title'. | |
525 | * | |
5a4f6742 CW |
526 | * @param array $array |
527 | * the array for localization (in place). | |
6a488035 | 528 | */ |
00be9182 | 529 | public function localizeTitles(&$array) { |
6a488035 TO |
530 | foreach ($array as $key => $value) { |
531 | if (is_array($value)) { | |
532 | $this->localizeTitles($value); | |
533 | $array[$key] = $value; | |
534 | } | |
535 | elseif ((string ) $key == 'title') { | |
eb7d6f39 | 536 | $array[$key] = ts($value, array('context' => 'menu')); |
6a488035 TO |
537 | } |
538 | } | |
539 | } | |
540 | ||
1a3cba0e ML |
541 | /** |
542 | * Binds a gettext domain, wrapper over bindtextdomain(). | |
543 | * | |
6a0b768e TO |
544 | * @param $key |
545 | * Key of the extension (can be 'civicrm', or 'org.example.foo'). | |
1a3cba0e | 546 | * |
408b79bf | 547 | * @return Bool |
a6c01b45 | 548 | * True if the domain was changed for an extension. |
1a3cba0e | 549 | */ |
00be9182 | 550 | public function setGettextDomain($key) { |
82bcff63 | 551 | /* No domain changes for en_US */ |
353ffa53 | 552 | if (!$this->_phpgettext) { |
82bcff63 ML |
553 | return FALSE; |
554 | } | |
1a3cba0e | 555 | |
74eb462f | 556 | // It's only necessary to find/bind once |
353ffa53 | 557 | if (!isset($this->_extensioncache[$key])) { |
1a3cba0e ML |
558 | try { |
559 | $mapper = CRM_Extension_System::singleton()->getMapper(); | |
560 | $path = $mapper->keyToBasePath($key); | |
561 | $info = $mapper->keyToInfo($key); | |
562 | $domain = $info->file; | |
563 | ||
74eb462f ML |
564 | if ($this->_nativegettext) { |
565 | bindtextdomain($domain, $path . DIRECTORY_SEPARATOR . 'l10n'); | |
566 | bind_textdomain_codeset($domain, 'UTF-8'); | |
567 | $this->_extensioncache[$key] = $domain; | |
568 | } | |
569 | else { | |
570 | // phpgettext | |
b52647df | 571 | $mo_file = $path . DIRECTORY_SEPARATOR . 'l10n' . DIRECTORY_SEPARATOR . $this->locale . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . $domain . '.mo'; |
74eb462f ML |
572 | $streamer = new FileReader($mo_file); |
573 | $this->_extensioncache[$key] = new gettext_reader($streamer); | |
574 | } | |
1a3cba0e ML |
575 | } |
576 | catch (CRM_Extension_Exception $e) { | |
b44e3f84 | 577 | // Intentionally not translating this string to avoid possible infinite loops |
74eb462f ML |
578 | // Only developers should see this string, if they made a mistake in their ts() usage. |
579 | CRM_Core_Session::setStatus('Unknown extension key in a translation string: ' . $key, '', 'error'); | |
580 | $this->_extensioncache[$key] = FALSE; | |
1a3cba0e ML |
581 | } |
582 | } | |
583 | ||
74eb462f ML |
584 | if (isset($this->_extensioncache[$key]) && $this->_extensioncache[$key]) { |
585 | if ($this->_nativegettext) { | |
586 | textdomain($this->_extensioncache[$key]); | |
587 | } | |
588 | else { | |
589 | $this->_phpgettext = $this->_extensioncache[$key]; | |
590 | } | |
591 | ||
592 | return TRUE; | |
1a3cba0e | 593 | } |
74eb462f ML |
594 | |
595 | return FALSE; | |
1a3cba0e ML |
596 | } |
597 | ||
07844ccf SV |
598 | |
599 | /** | |
600 | * Is the CiviCRM in multilingual mode. | |
601 | * | |
602 | * @return Bool | |
603 | * True if CiviCRM is in multilingual mode. | |
604 | */ | |
605 | public static function isMultilingual() { | |
606 | $domain = new CRM_Core_DAO_Domain(); | |
607 | $domain->find(TRUE); | |
608 | return (bool) $domain->locales; | |
609 | } | |
610 | ||
3a55aa6b ML |
611 | /** |
612 | * Is the language written "right-to-left"? | |
613 | * | |
614 | * @param $language | |
615 | * Language (for example 'en_US', or 'fr_CA'). | |
616 | * | |
617 | * @return Bool | |
618 | * True if it is an RTL language. | |
619 | */ | |
620 | public static function isLanguageRTL($language) { | |
621 | $rtl = CRM_Core_I18n_PseudoConstant::getRTLlanguages(); | |
622 | $short = CRM_Core_I18n_PseudoConstant::shortForLong($language); | |
623 | ||
624 | return (in_array($short, $rtl)); | |
625 | } | |
07844ccf SV |
626 | |
627 | /** | |
628 | * Change the processing language without changing the current user language | |
629 | * | |
c86241f4 SV |
630 | * @param $locale |
631 | * Locale (for example 'en_US', or 'fr_CA'). | |
07844ccf SV |
632 | * True if the domain was changed for an extension. |
633 | */ | |
c86241f4 | 634 | public function setLocale($locale) { |
fd1f3a26 SV |
635 | |
636 | // Change the language of the CMS as well, for URLs. | |
c86241f4 | 637 | CRM_Utils_System::setUFLocale($locale); |
fd1f3a26 SV |
638 | |
639 | // change the gettext ressources | |
07844ccf | 640 | if ($this->_nativegettext) { |
c86241f4 | 641 | $this->setNativeGettextLocale($locale); |
07844ccf SV |
642 | } |
643 | else { | |
644 | // phpgettext | |
c86241f4 | 645 | $this->setPhpGettextLocale($locale); |
07844ccf SV |
646 | } |
647 | ||
648 | // for sql queries | |
53d46849 | 649 | global $dbLocale; |
c86241f4 | 650 | $dbLocale = "_{$locale}"; |
07844ccf SV |
651 | |
652 | } | |
653 | ||
6a488035 TO |
654 | /** |
655 | * Static instance provider - return the instance for the current locale. | |
c4c311c1 CW |
656 | * |
657 | * @return CRM_Core_I18n | |
6a488035 | 658 | */ |
00be9182 | 659 | public static function &singleton() { |
6a488035 TO |
660 | static $singleton = array(); |
661 | ||
98466ff9 | 662 | $tsLocale = CRM_Core_I18n::getLocale(); |
6a488035 TO |
663 | if (!isset($singleton[$tsLocale])) { |
664 | $singleton[$tsLocale] = new CRM_Core_I18n($tsLocale); | |
665 | } | |
666 | ||
667 | return $singleton[$tsLocale]; | |
668 | } | |
669 | ||
670 | /** | |
671 | * Set the LC_TIME locale if it's not set already (for a given language choice). | |
672 | * | |
a6c01b45 CW |
673 | * @return string |
674 | * the final LC_TIME that got set | |
6a488035 | 675 | */ |
00be9182 | 676 | public static function setLcTime() { |
6a488035 TO |
677 | static $locales = array(); |
678 | ||
98466ff9 | 679 | $tsLocale = CRM_Core_I18n::getLocale(); |
6a488035 TO |
680 | if (!isset($locales[$tsLocale])) { |
681 | // with the config being set to pl_PL: try pl_PL.UTF-8, | |
682 | // then pl_PL, if neither present fall back to C | |
683 | $locales[$tsLocale] = setlocale(LC_TIME, $tsLocale . '.UTF-8', $tsLocale, 'C'); | |
684 | } | |
685 | ||
686 | return $locales[$tsLocale]; | |
687 | } | |
96025800 | 688 | |
9747df8a | 689 | /** |
690 | * Get the default language for contacts where no language is provided. | |
691 | * | |
692 | * Note that NULL is a valid option so be careful with checking for empty etc. | |
693 | * | |
694 | * NULL would mean 'we don't know & we don't want to hazard a guess'. | |
695 | * | |
696 | * @return string | |
697 | */ | |
698 | public static function getContactDefaultLanguage() { | |
aaffa79f | 699 | $language = Civi::settings()->get('contact_default_language'); |
9747df8a | 700 | if ($language == 'undefined') { |
701 | return NULL; | |
702 | } | |
703 | if (empty($language) || $language === '*default*') { | |
704 | $language = civicrm_api3('setting', 'getvalue', array( | |
705 | 'name' => 'lcMessages', | |
706 | 'group' => CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME, | |
707 | )); | |
708 | } | |
709 | elseif ($language == 'current_site_language') { | |
98466ff9 | 710 | return CRM_Core_I18n::getLocale(); |
9747df8a | 711 | } |
712 | ||
713 | return $language; | |
714 | } | |
715 | ||
186e53b3 | 716 | /** |
717 | * Get the current locale | |
718 | * | |
719 | * @return string | |
720 | */ | |
721 | public static function getLocale() { | |
722 | global $tsLocale; | |
338ff411 | 723 | return $tsLocale ? $tsLocale : 'en_US'; |
186e53b3 | 724 | } |
725 | ||
6a488035 TO |
726 | } |
727 | ||
728 | /** | |
729 | * Short-named function for string translation, defined in global scope so it's available everywhere. | |
730 | * | |
e3b7fc11 | 731 | * @param string $text |
6a0b768e | 732 | * String string for translating. |
16b10e64 | 733 | * @param array $params |
6a0b768e | 734 | * Array an array of additional parameters. |
6a488035 | 735 | * |
a6c01b45 | 736 | * @return string |
353ffa53 | 737 | * the translated string |
6a488035 TO |
738 | */ |
739 | function ts($text, $params = array()) { | |
4954910a TO |
740 | static $areSettingsAvailable = FALSE; |
741 | static $lastLocale = NULL; | |
6a488035 TO |
742 | static $i18n = NULL; |
743 | static $function = NULL; | |
744 | ||
745 | if ($text == '') { | |
746 | return ''; | |
747 | } | |
748 | ||
4954910a TO |
749 | // When the settings become available, lookup customTranslateFunction. |
750 | if (!$areSettingsAvailable) { | |
751 | $areSettingsAvailable = (bool) \Civi\Core\Container::getBootService('settings_manager'); | |
752 | if ($areSettingsAvailable) { | |
753 | $config = CRM_Core_Config::singleton(); | |
754 | if (isset($config->customTranslateFunction) and function_exists($config->customTranslateFunction)) { | |
755 | $function = $config->customTranslateFunction; | |
756 | } | |
757 | } | |
6a488035 TO |
758 | } |
759 | ||
4954910a TO |
760 | $activeLocale = CRM_Core_I18n::getLocale(); |
761 | if (!$i18n or $lastLocale != $activeLocale) { | |
6a488035 | 762 | $i18n = CRM_Core_I18n::singleton(); |
4954910a | 763 | $lastLocale = $activeLocale; |
6a488035 TO |
764 | } |
765 | ||
766 | if ($function) { | |
767 | return $function($text, $params); | |
768 | } | |
769 | else { | |
770 | return $i18n->crm_translate($text, $params); | |
771 | } | |
772 | } |