Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Core_I18n { | |
36 | ||
37 | /** | |
2ba175b6 DL |
38 | * A PHP-gettext instance for string translation; |
39 | * should stay null if the strings are not to be translated (en_US). | |
6a488035 TO |
40 | */ |
41 | private $_phpgettext = NULL; | |
42 | ||
43 | /** | |
44 | * Whether we are using native gettext or not. | |
45 | */ | |
46 | private $_nativegettext = FALSE; | |
47 | ||
74eb462f ML |
48 | /** |
49 | * Gettext cache for extension domains/streamers, depending on if native or phpgettext. | |
50 | * - native gettext: we cache the value for textdomain() | |
51 | * - phpgettext: we cache the file streamer. | |
52 | */ | |
53 | private $_extensioncache = array(); | |
54 | ||
6a488035 TO |
55 | /** |
56 | * A locale-based constructor that shouldn't be called from outside of this class (use singleton() instead). | |
57 | * | |
5a4f6742 CW |
58 | * @param string $locale |
59 | * the base of this certain object's existence. | |
6a488035 | 60 | * |
77b97be7 | 61 | * @return \CRM_Core_I18n |
6a488035 | 62 | */ |
00be9182 | 63 | public function __construct($locale) { |
6a488035 TO |
64 | if ($locale != '' and $locale != 'en_US') { |
65 | $config = CRM_Core_Config::singleton(); | |
66 | ||
67 | if (defined('CIVICRM_GETTEXT_NATIVE') && CIVICRM_GETTEXT_NATIVE && function_exists('gettext')) { | |
68 | // Note: the file hierarchy for .po must be, for example: l10n/fr_FR/LC_MESSAGES/civicrm.mo | |
69 | ||
70 | $this->_nativegettext = TRUE; | |
71 | ||
72 | $locale .= '.utf8'; | |
73 | putenv("LANG=$locale"); | |
74 | ||
75 | // CRM-11833 Avoid LC_ALL because of LC_NUMERIC and potential DB error. | |
76 | setlocale(LC_TIME, $locale); | |
77 | setlocale(LC_MESSAGES, $locale); | |
78 | setlocale(LC_CTYPE, $locale); | |
79 | ||
80 | bindtextdomain('civicrm', $config->gettextResourceDir); | |
81 | bind_textdomain_codeset('civicrm', 'UTF-8'); | |
82 | textdomain('civicrm'); | |
83 | ||
84 | $this->_phpgettext = new CRM_Core_I18n_NativeGettext(); | |
74eb462f | 85 | $this->_extensioncache['civicrm'] = 'civicrm'; |
6a488035 TO |
86 | return; |
87 | } | |
88 | ||
89 | // Otherwise, use PHP-gettext | |
b395e3c0 ML |
90 | // we support both the old file hierarchy format and the new: |
91 | // pre-4.5: civicrm/l10n/xx_XX/civicrm.mo | |
92 | // post-4.5: civicrm/l10n/xx_XX/LC_MESSAGES/civicrm.mo | |
6a488035 TO |
93 | require_once 'PHPgettext/streams.php'; |
94 | require_once 'PHPgettext/gettext.php'; | |
95 | ||
b395e3c0 ML |
96 | $mo_file = $config->gettextResourceDir . $locale . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . 'civicrm.mo'; |
97 | ||
353ffa53 | 98 | if (!file_exists($mo_file)) { |
b395e3c0 ML |
99 | // fallback to pre-4.5 mode |
100 | $mo_file = $config->gettextResourceDir . $locale . DIRECTORY_SEPARATOR . 'civicrm.mo'; | |
101 | } | |
102 | ||
103 | $streamer = new FileReader($mo_file); | |
6a488035 | 104 | $this->_phpgettext = new gettext_reader($streamer); |
74eb462f | 105 | $this->_extensioncache['civicrm'] = $this->_phpgettext; |
6a488035 TO |
106 | } |
107 | } | |
108 | ||
109 | /** | |
110 | * Returns whether gettext is running natively or using PHP-Gettext. | |
111 | * | |
a6c01b45 CW |
112 | * @return bool |
113 | * True if gettext is native | |
6a488035 | 114 | */ |
00be9182 | 115 | public function isNative() { |
6a488035 TO |
116 | return $this->_nativegettext; |
117 | } | |
118 | ||
119 | /** | |
120 | * Return languages available in this instance of CiviCRM. | |
121 | * | |
5a4f6742 CW |
122 | * @param bool $justEnabled |
123 | * whether to return all languages or just the enabled ones. | |
6a488035 | 124 | * |
a6c01b45 | 125 | * @return array |
16b10e64 | 126 | * Array of code/language name mappings |
6a488035 | 127 | */ |
00be9182 | 128 | public static function languages($justEnabled = FALSE) { |
6a488035 TO |
129 | static $all = NULL; |
130 | static $enabled = NULL; | |
131 | ||
132 | if (!$all) { | |
c0c9cd82 | 133 | $all = CRM_Contact_BAO_Contact::buildOptions('preferred_language'); |
6a488035 TO |
134 | |
135 | // check which ones are available; add them to $all if not there already | |
136 | $config = CRM_Core_Config::singleton(); | |
137 | $codes = array(); | |
948d11bf | 138 | if (is_dir($config->gettextResourceDir) && $dir = opendir($config->gettextResourceDir)) { |
6a488035 TO |
139 | while ($filename = readdir($dir)) { |
140 | if (preg_match('/^[a-z][a-z]_[A-Z][A-Z]$/', $filename)) { | |
141 | $codes[] = $filename; | |
142 | if (!isset($all[$filename])) { | |
143 | $all[$filename] = $filename; | |
144 | } | |
145 | } | |
146 | } | |
147 | closedir($dir); | |
148 | } | |
149 | ||
150 | // drop the unavailable languages (except en_US) | |
151 | foreach (array_keys($all) as $code) { | |
152 | if ($code == 'en_US') { | |
153 | continue; | |
154 | } | |
408b79bf | 155 | if (!in_array($code, $codes)) { |
156 | unset($all[$code]); | |
157 | } | |
6a488035 TO |
158 | } |
159 | } | |
160 | ||
161 | if ($enabled === NULL) { | |
162 | $config = CRM_Core_Config::singleton(); | |
163 | $enabled = array(); | |
164 | if (isset($config->languageLimit) and $config->languageLimit) { | |
165 | foreach ($all as $code => $name) { | |
166 | if (in_array($code, array_keys($config->languageLimit))) { | |
167 | $enabled[$code] = $name; | |
168 | } | |
169 | } | |
170 | } | |
171 | } | |
172 | ||
173 | return $justEnabled ? $enabled : $all; | |
174 | } | |
175 | ||
176 | /** | |
177 | * Replace arguments in a string with their values. Arguments are represented by % followed by their number. | |
178 | * | |
5a4f6742 CW |
179 | * @param string $str |
180 | * source string. | |
6a488035 | 181 | * |
a6c01b45 CW |
182 | * @return string |
183 | * modified string | |
6a488035 | 184 | */ |
00be9182 | 185 | public function strarg($str) { |
6a488035 TO |
186 | $tr = array(); |
187 | $p = 0; | |
188 | for ($i = 1; $i < func_num_args(); $i++) { | |
189 | $arg = func_get_arg($i); | |
190 | if (is_array($arg)) { | |
191 | foreach ($arg as $aarg) { | |
192 | $tr['%' . ++$p] = $aarg; | |
193 | } | |
194 | } | |
195 | else { | |
196 | $tr['%' . ++$p] = $arg; | |
197 | } | |
198 | } | |
199 | return strtr($str, $tr); | |
200 | } | |
201 | ||
202 | /** | |
203 | * Smarty block function, provides gettext support for smarty. | |
204 | * | |
205 | * The block content is the text that should be translated. | |
206 | * | |
207 | * Any parameter that is sent to the function will be represented as %n in the translation text, | |
208 | * where n is 1 for the first parameter. The following parameters are reserved: | |
209 | * - escape - sets escape mode: | |
210 | * - 'html' for HTML escaping, this is the default. | |
211 | * - 'js' for javascript escaping. | |
212 | * - 'no'/'off'/0 - turns off escaping | |
213 | * - plural - The plural version of the text (2nd parameter of ngettext()) | |
214 | * - count - The item count for plural mode (3rd parameter of ngettext()) | |
215 | * - context - gettext context of that string (for homonym handling) | |
216 | * | |
5a4f6742 CW |
217 | * @param string $text |
218 | * the original string. | |
219 | * @param array $params | |
220 | * the params of the translation (if any). | |
6a488035 | 221 | * |
a6c01b45 CW |
222 | * @return string |
223 | * the translated string | |
6a488035 | 224 | */ |
00be9182 | 225 | public function crm_translate($text, $params = array()) { |
6a488035 TO |
226 | if (isset($params['escape'])) { |
227 | $escape = $params['escape']; | |
228 | unset($params['escape']); | |
229 | } | |
230 | ||
231 | // sometimes we need to {ts}-tag a string, but don’t want to | |
232 | // translate it in the template (like civicrm_navigation.tpl), | |
233 | // because we handle the translation in a different way (CRM-6998) | |
234 | // in such cases we return early, only doing SQL/JS escaping | |
235 | if (isset($params['skip']) and $params['skip']) { | |
236 | if (isset($escape) and ($escape == 'sql')) { | |
74032946 | 237 | $text = CRM_Core_DAO::escapeString($text); |
6a488035 TO |
238 | } |
239 | if (isset($escape) and ($escape == 'js')) { | |
240 | $text = addcslashes($text, "'"); | |
241 | } | |
242 | return $text; | |
243 | } | |
244 | ||
245 | if (isset($params['plural'])) { | |
246 | $plural = $params['plural']; | |
247 | unset($params['plural']); | |
248 | if (isset($params['count'])) { | |
249 | $count = $params['count']; | |
250 | } | |
251 | } | |
252 | ||
253 | if (isset($params['context'])) { | |
254 | $context = $params['context']; | |
255 | unset($params['context']); | |
256 | } | |
257 | else { | |
258 | $context = NULL; | |
259 | } | |
260 | ||
1a3cba0e ML |
261 | // gettext domain for extensions |
262 | $domain_changed = FALSE; | |
353ffa53 | 263 | if (!empty($params['domain']) && $this->_phpgettext) { |
1a3cba0e ML |
264 | if ($this->setGettextDomain($params['domain'])) { |
265 | $domain_changed = TRUE; | |
266 | } | |
267 | } | |
268 | ||
6a488035 TO |
269 | // do all wildcard translations first |
270 | $config = CRM_Core_Config::singleton(); | |
6cf5bb6f DL |
271 | $stringTable = CRM_Utils_Array::value( |
272 | $config->lcMessages, | |
6a488035 TO |
273 | $config->localeCustomStrings |
274 | ); | |
275 | ||
276 | $exactMatch = FALSE; | |
277 | if (isset($stringTable['enabled']['exactMatch'])) { | |
278 | foreach ($stringTable['enabled']['exactMatch'] as $search => $replace) { | |
279 | if ($search === $text) { | |
280 | $exactMatch = TRUE; | |
281 | $text = $replace; | |
282 | break; | |
283 | } | |
284 | } | |
285 | } | |
286 | ||
6cf5bb6f DL |
287 | if ( |
288 | !$exactMatch && | |
6a488035 TO |
289 | isset($stringTable['enabled']['wildcardMatch']) |
290 | ) { | |
353ffa53 | 291 | $search = array_keys($stringTable['enabled']['wildcardMatch']); |
6a488035 | 292 | $replace = array_values($stringTable['enabled']['wildcardMatch']); |
353ffa53 | 293 | $text = str_replace($search, $replace, $text); |
6a488035 TO |
294 | } |
295 | ||
296 | // dont translate if we've done exactMatch already | |
297 | if (!$exactMatch) { | |
298 | // use plural if required parameters are set | |
299 | if (isset($count) && isset($plural)) { | |
300 | ||
301 | if ($this->_phpgettext) { | |
302 | $text = $this->_phpgettext->ngettext($text, $plural, $count); | |
303 | } | |
304 | else { | |
305 | // if the locale's not set, we do ngettext work by hand | |
306 | // if $count == 1 then $text = $text, else $text = $plural | |
307 | if ($count != 1) { | |
308 | $text = $plural; | |
309 | } | |
310 | } | |
311 | ||
312 | // expand %count in translated string to $count | |
313 | $text = strtr($text, array('%count' => $count)); | |
314 | ||
315 | // if not plural, but the locale's set, translate | |
316 | } | |
317 | elseif ($this->_phpgettext) { | |
318 | if ($context) { | |
319 | $text = $this->_phpgettext->pgettext($context, $text); | |
320 | } | |
321 | else { | |
322 | $text = $this->_phpgettext->translate($text); | |
323 | } | |
324 | } | |
325 | } | |
326 | ||
327 | // replace the numbered %1, %2, etc. params if present | |
328 | if (count($params)) { | |
329 | $text = $this->strarg($text, $params); | |
330 | } | |
331 | ||
332 | // escape SQL if we were asked for it | |
333 | if (isset($escape) and ($escape == 'sql')) { | |
74032946 | 334 | $text = CRM_Core_DAO::escapeString($text); |
6a488035 TO |
335 | } |
336 | ||
337 | // escape for JavaScript (if requested) | |
338 | if (isset($escape) and ($escape == 'js')) { | |
339 | $text = addcslashes($text, "'"); | |
340 | } | |
341 | ||
1a3cba0e ML |
342 | if ($domain_changed) { |
343 | $this->setGettextDomain('civicrm'); | |
344 | } | |
345 | ||
6a488035 TO |
346 | return $text; |
347 | } | |
348 | ||
349 | /** | |
350 | * Translate a string to the current locale. | |
351 | * | |
5a4f6742 CW |
352 | * @param string $string |
353 | * this string should be translated. | |
6a488035 | 354 | * |
a6c01b45 CW |
355 | * @return string |
356 | * the translated string | |
6a488035 | 357 | */ |
00be9182 | 358 | public function translate($string) { |
6a488035 TO |
359 | return ($this->_phpgettext) ? $this->_phpgettext->translate($string) : $string; |
360 | } | |
361 | ||
362 | /** | |
363 | * Localize (destructively) array values. | |
364 | * | |
5a4f6742 CW |
365 | * @param array $array |
366 | * the array for localization (in place). | |
367 | * @param array $params | |
368 | * an array of additional parameters. | |
6a488035 | 369 | * |
2b37475d | 370 | * @return void |
6a488035 | 371 | */ |
408b79bf | 372 | public function localizeArray( |
6a488035 TO |
373 | &$array, |
374 | $params = array() | |
375 | ) { | |
376 | global $tsLocale; | |
377 | ||
378 | if ($tsLocale == 'en_US') { | |
379 | return; | |
380 | } | |
381 | ||
382 | foreach ($array as & $value) { | |
383 | if ($value) { | |
384 | $value = ts($value, $params); | |
385 | } | |
386 | } | |
387 | } | |
388 | ||
389 | /** | |
390 | * Localize (destructively) array elements with keys of 'title'. | |
391 | * | |
5a4f6742 CW |
392 | * @param array $array |
393 | * the array for localization (in place). | |
6a488035 | 394 | * |
2b37475d | 395 | * @return void |
6a488035 | 396 | */ |
00be9182 | 397 | public function localizeTitles(&$array) { |
6a488035 TO |
398 | foreach ($array as $key => $value) { |
399 | if (is_array($value)) { | |
400 | $this->localizeTitles($value); | |
401 | $array[$key] = $value; | |
402 | } | |
403 | elseif ((string ) $key == 'title') { | |
eb7d6f39 | 404 | $array[$key] = ts($value, array('context' => 'menu')); |
6a488035 TO |
405 | } |
406 | } | |
407 | } | |
408 | ||
1a3cba0e ML |
409 | /** |
410 | * Binds a gettext domain, wrapper over bindtextdomain(). | |
411 | * | |
6a0b768e TO |
412 | * @param $key |
413 | * Key of the extension (can be 'civicrm', or 'org.example.foo'). | |
1a3cba0e | 414 | * |
408b79bf | 415 | * @return Bool |
a6c01b45 | 416 | * True if the domain was changed for an extension. |
1a3cba0e | 417 | */ |
00be9182 | 418 | public function setGettextDomain($key) { |
82bcff63 | 419 | /* No domain changes for en_US */ |
353ffa53 | 420 | if (!$this->_phpgettext) { |
82bcff63 ML |
421 | return FALSE; |
422 | } | |
1a3cba0e | 423 | |
74eb462f | 424 | // It's only necessary to find/bind once |
353ffa53 | 425 | if (!isset($this->_extensioncache[$key])) { |
1a3cba0e ML |
426 | $config = CRM_Core_Config::singleton(); |
427 | ||
428 | try { | |
429 | $mapper = CRM_Extension_System::singleton()->getMapper(); | |
430 | $path = $mapper->keyToBasePath($key); | |
431 | $info = $mapper->keyToInfo($key); | |
432 | $domain = $info->file; | |
433 | ||
74eb462f ML |
434 | if ($this->_nativegettext) { |
435 | bindtextdomain($domain, $path . DIRECTORY_SEPARATOR . 'l10n'); | |
436 | bind_textdomain_codeset($domain, 'UTF-8'); | |
437 | $this->_extensioncache[$key] = $domain; | |
438 | } | |
439 | else { | |
440 | // phpgettext | |
441 | $mo_file = $path . DIRECTORY_SEPARATOR . 'l10n' . DIRECTORY_SEPARATOR . $config->lcMessages . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . $domain . '.mo'; | |
442 | $streamer = new FileReader($mo_file); | |
443 | $this->_extensioncache[$key] = new gettext_reader($streamer); | |
444 | } | |
1a3cba0e ML |
445 | } |
446 | catch (CRM_Extension_Exception $e) { | |
74eb462f ML |
447 | // Intentionally not translating this string to avoid possible infinit loops |
448 | // Only developers should see this string, if they made a mistake in their ts() usage. | |
449 | CRM_Core_Session::setStatus('Unknown extension key in a translation string: ' . $key, '', 'error'); | |
450 | $this->_extensioncache[$key] = FALSE; | |
1a3cba0e ML |
451 | } |
452 | } | |
453 | ||
74eb462f ML |
454 | if (isset($this->_extensioncache[$key]) && $this->_extensioncache[$key]) { |
455 | if ($this->_nativegettext) { | |
456 | textdomain($this->_extensioncache[$key]); | |
457 | } | |
458 | else { | |
459 | $this->_phpgettext = $this->_extensioncache[$key]; | |
460 | } | |
461 | ||
462 | return TRUE; | |
1a3cba0e | 463 | } |
74eb462f ML |
464 | |
465 | return FALSE; | |
1a3cba0e ML |
466 | } |
467 | ||
6a488035 TO |
468 | /** |
469 | * Static instance provider - return the instance for the current locale. | |
470 | */ | |
00be9182 | 471 | public static function &singleton() { |
6a488035 TO |
472 | static $singleton = array(); |
473 | ||
474 | global $tsLocale; | |
475 | if (!isset($singleton[$tsLocale])) { | |
476 | $singleton[$tsLocale] = new CRM_Core_I18n($tsLocale); | |
477 | } | |
478 | ||
479 | return $singleton[$tsLocale]; | |
480 | } | |
481 | ||
482 | /** | |
483 | * Set the LC_TIME locale if it's not set already (for a given language choice). | |
484 | * | |
a6c01b45 CW |
485 | * @return string |
486 | * the final LC_TIME that got set | |
6a488035 | 487 | */ |
00be9182 | 488 | public static function setLcTime() { |
6a488035 TO |
489 | static $locales = array(); |
490 | ||
491 | global $tsLocale; | |
492 | if (!isset($locales[$tsLocale])) { | |
493 | // with the config being set to pl_PL: try pl_PL.UTF-8, | |
494 | // then pl_PL, if neither present fall back to C | |
495 | $locales[$tsLocale] = setlocale(LC_TIME, $tsLocale . '.UTF-8', $tsLocale, 'C'); | |
496 | } | |
497 | ||
498 | return $locales[$tsLocale]; | |
499 | } | |
500 | } | |
501 | ||
502 | /** | |
503 | * Short-named function for string translation, defined in global scope so it's available everywhere. | |
504 | * | |
6a0b768e TO |
505 | * @param $text |
506 | * String string for translating. | |
16b10e64 | 507 | * @param array $params |
6a0b768e | 508 | * Array an array of additional parameters. |
6a488035 | 509 | * |
a6c01b45 | 510 | * @return string |
353ffa53 | 511 | * the translated string |
6a488035 TO |
512 | */ |
513 | function ts($text, $params = array()) { | |
514 | static $config = NULL; | |
515 | static $locale = NULL; | |
516 | static $i18n = NULL; | |
517 | static $function = NULL; | |
518 | ||
519 | if ($text == '') { | |
520 | return ''; | |
521 | } | |
522 | ||
523 | if (!$config) { | |
524 | $config = CRM_Core_Config::singleton(); | |
525 | } | |
526 | ||
527 | global $tsLocale; | |
528 | if (!$i18n or $locale != $tsLocale) { | |
529 | $i18n = CRM_Core_I18n::singleton(); | |
530 | $locale = $tsLocale; | |
531 | if (isset($config->customTranslateFunction) and function_exists($config->customTranslateFunction)) { | |
532 | $function = $config->customTranslateFunction; | |
533 | } | |
534 | } | |
535 | ||
536 | if ($function) { | |
537 | return $function($text, $params); | |
538 | } | |
539 | else { | |
540 | return $i18n->crm_translate($text, $params); | |
541 | } | |
542 | } |