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