3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
38 * A PHP-gettext instance for string translation;
39 * should stay null if the strings are not to be translated (en_US).
41 private $_phpgettext = NULL;
44 * Whether we are using native gettext or not.
46 private $_nativegettext = FALSE;
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.
53 private $_extensioncache = array();
56 * A locale-based constructor that shouldn't be called from outside of this class (use singleton() instead).
58 * @param $locale string the base of this certain object's existence
62 function __construct($locale) {
63 if ($locale != '' and $locale != 'en_US') {
64 $config = CRM_Core_Config
::singleton();
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
69 $this->_nativegettext
= TRUE;
72 putenv("LANG=$locale");
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);
79 bindtextdomain('civicrm', $config->gettextResourceDir
);
80 bind_textdomain_codeset('civicrm', 'UTF-8');
81 textdomain('civicrm');
83 $this->_phpgettext
= new CRM_Core_I18n_NativeGettext();
84 $this->_extensioncache
['civicrm'] = 'civicrm';
88 // Otherwise, use PHP-gettext
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
92 require_once 'PHPgettext/streams.php';
93 require_once 'PHPgettext/gettext.php';
95 $mo_file = $config->gettextResourceDir
. $locale . DIRECTORY_SEPARATOR
. 'LC_MESSAGES' . DIRECTORY_SEPARATOR
. 'civicrm.mo';
97 if (! file_exists($mo_file)) {
98 // fallback to pre-4.5 mode
99 $mo_file = $config->gettextResourceDir
. $locale . DIRECTORY_SEPARATOR
. 'civicrm.mo';
102 $streamer = new FileReader($mo_file);
103 $this->_phpgettext
= new gettext_reader($streamer);
104 $this->_extensioncache
['civicrm'] = $this->_phpgettext
;
109 * Returns whether gettext is running natively or using PHP-Gettext.
111 * @return bool True if gettext is native
113 function isNative() {
114 return $this->_nativegettext
;
118 * Return languages available in this instance of CiviCRM.
120 * @param $justEnabled boolean whether to return all languages or just the enabled ones
122 * @return array of code/language name mappings
124 static function languages($justEnabled = FALSE) {
126 static $enabled = NULL;
129 $all = CRM_Contact_BAO_Contact
::buildOptions('preferred_language');
131 // check which ones are available; add them to $all if not there already
132 $config = CRM_Core_Config
::singleton();
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;
147 // drop the unavailable languages (except en_US)
148 foreach (array_keys($all) as $code) {
149 if ($code == 'en_US') {
152 if (!in_array($code, $codes))unset($all[$code]);
156 if ($enabled === NULL) {
157 $config = CRM_Core_Config
::singleton();
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;
168 return $justEnabled ?
$enabled : $all;
172 * Replace arguments in a string with their values. Arguments are represented by % followed by their number.
174 * @param $str string source string
175 * @param mixed arguments, can be passed in an array or through single variables
177 * @return string modified string
179 function strarg($str) {
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;
190 $tr['%' . ++
$p] = $arg;
193 return strtr($str, $tr);
197 * Smarty block function, provides gettext support for smarty.
199 * The block content is the text that should be translated.
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)
211 * @param $text string the original string
212 * @param $params array the params of the translation (if any)
214 * @return string the translated string
216 function crm_translate($text, $params = array()) {
217 if (isset($params['escape'])) {
218 $escape = $params['escape'];
219 unset($params['escape']);
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')) {
228 $text = mysql_escape_string($text);
230 if (isset($escape) and ($escape == 'js')) {
231 $text = addcslashes($text, "'");
236 if (isset($params['plural'])) {
237 $plural = $params['plural'];
238 unset($params['plural']);
239 if (isset($params['count'])) {
240 $count = $params['count'];
244 if (isset($params['context'])) {
245 $context = $params['context'];
246 unset($params['context']);
252 // gettext domain for extensions
253 $domain_changed = FALSE;
254 if (! empty($params['domain']) && $this->_phpgettext
) {
255 if ($this->setGettextDomain($params['domain'])) {
256 $domain_changed = TRUE;
260 // do all wildcard translations first
261 $config = CRM_Core_Config
::singleton();
262 $stringTable = CRM_Utils_Array
::value(
264 $config->localeCustomStrings
268 if (isset($stringTable['enabled']['exactMatch'])) {
269 foreach ($stringTable['enabled']['exactMatch'] as $search => $replace) {
270 if ($search === $text) {
280 isset($stringTable['enabled']['wildcardMatch'])
282 $search = array_keys($stringTable['enabled']['wildcardMatch']);
283 $replace = array_values($stringTable['enabled']['wildcardMatch']);
284 $text = str_replace($search, $replace, $text);
287 // dont translate if we've done exactMatch already
289 // use plural if required parameters are set
290 if (isset($count) && isset($plural)) {
292 if ($this->_phpgettext
) {
293 $text = $this->_phpgettext
->ngettext($text, $plural, $count);
296 // if the locale's not set, we do ngettext work by hand
297 // if $count == 1 then $text = $text, else $text = $plural
303 // expand %count in translated string to $count
304 $text = strtr($text, array('%count' => $count));
306 // if not plural, but the locale's set, translate
308 elseif ($this->_phpgettext
) {
310 $text = $this->_phpgettext
->pgettext($context, $text);
313 $text = $this->_phpgettext
->translate($text);
318 // replace the numbered %1, %2, etc. params if present
319 if (count($params)) {
320 $text = $this->strarg($text, $params);
323 // escape SQL if we were asked for it
324 if (isset($escape) and ($escape == 'sql')) {
325 $text = mysql_escape_string($text);
328 // escape for JavaScript (if requested)
329 if (isset($escape) and ($escape == 'js')) {
330 $text = addcslashes($text, "'");
333 if ($domain_changed) {
334 $this->setGettextDomain('civicrm');
341 * Translate a string to the current locale.
343 * @param $string string this string should be translated
345 * @return string the translated string
347 function translate($string) {
348 return ($this->_phpgettext
) ?
$this->_phpgettext
->translate($string) : $string;
352 * Localize (destructively) array values.
354 * @param $array array the array for localization (in place)
355 * @param $params array an array of additional parameters
359 function localizeArray(
365 if ($tsLocale == 'en_US') {
369 foreach ($array as & $value) {
371 $value = ts($value, $params);
377 * Localize (destructively) array elements with keys of 'title'.
379 * @param $array array the array for localization (in place)
383 function localizeTitles(&$array) {
384 foreach ($array as $key => $value) {
385 if (is_array($value)) {
386 $this->localizeTitles($value);
387 $array[$key] = $value;
389 elseif ((string ) $key == 'title') {
390 $array[$key] = ts($value, array('context' => 'menu'));
396 * Binds a gettext domain, wrapper over bindtextdomain().
398 * @param $key Key of the extension (can be 'civicrm', or 'org.example.foo').
400 * @return Boolean True if the domain was changed for an extension.
402 function setGettextDomain($key) {
403 /* No domain changes for en_US */
404 if (! $this->_phpgettext
) {
408 // It's only necessary to find/bind once
409 if (! isset($this->_extensioncache
[$key])) {
410 $config = CRM_Core_Config
::singleton();
413 $mapper = CRM_Extension_System
::singleton()->getMapper();
414 $path = $mapper->keyToBasePath($key);
415 $info = $mapper->keyToInfo($key);
416 $domain = $info->file
;
418 if ($this->_nativegettext
) {
419 bindtextdomain($domain, $path . DIRECTORY_SEPARATOR
. 'l10n');
420 bind_textdomain_codeset($domain, 'UTF-8');
421 $this->_extensioncache
[$key] = $domain;
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);
430 catch (CRM_Extension_Exception
$e) {
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;
438 if (isset($this->_extensioncache
[$key]) && $this->_extensioncache
[$key]) {
439 if ($this->_nativegettext
) {
440 textdomain($this->_extensioncache
[$key]);
443 $this->_phpgettext
= $this->_extensioncache
[$key];
453 * Static instance provider - return the instance for the current locale.
455 static function &singleton() {
456 static $singleton = array();
459 if (!isset($singleton[$tsLocale])) {
460 $singleton[$tsLocale] = new CRM_Core_I18n($tsLocale);
463 return $singleton[$tsLocale];
467 * Set the LC_TIME locale if it's not set already (for a given language choice).
469 * @return string the final LC_TIME that got set
471 static function setLcTime() {
472 static $locales = array();
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');
481 return $locales[$tsLocale];
486 * Short-named function for string translation, defined in global scope so it's available everywhere.
488 * @param $text string string for translating
489 * @param $params array an array of additional parameters
491 * @return string the translated string
493 function ts($text, $params = array()) {
494 static $config = NULL;
495 static $locale = NULL;
497 static $function = NULL;
504 $config = CRM_Core_Config
::singleton();
508 if (!$i18n or $locale != $tsLocale) {
509 $i18n = CRM_Core_I18n
::singleton();
511 if (isset($config->customTranslateFunction
) and function_exists($config->customTranslateFunction
)) {
512 $function = $config->customTranslateFunction
;
517 return $function($text, $params);
520 return $i18n->crm_translate($text, $params);