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