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