3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 * A locale-based constructor that shouldn't be called from outside of this class (use singleton() instead).
51 * @param $locale string the base of this certain object's existence
53 * @return \CRM_Core_I18n
55 function __construct($locale) {
56 if ($locale != '' and $locale != 'en_US') {
57 $config = CRM_Core_Config
::singleton();
59 if (defined('CIVICRM_GETTEXT_NATIVE') && CIVICRM_GETTEXT_NATIVE
&& function_exists('gettext')) {
60 // Note: the file hierarchy for .po must be, for example: l10n/fr_FR/LC_MESSAGES/civicrm.mo
62 $this->_nativegettext
= TRUE;
65 putenv("LANG=$locale");
67 // CRM-11833 Avoid LC_ALL because of LC_NUMERIC and potential DB error.
68 setlocale(LC_TIME
, $locale);
69 setlocale(LC_MESSAGES
, $locale);
70 setlocale(LC_CTYPE
, $locale);
72 bindtextdomain('civicrm', $config->gettextResourceDir
);
73 bind_textdomain_codeset('civicrm', 'UTF-8');
74 textdomain('civicrm');
76 $this->_phpgettext
= new CRM_Core_I18n_NativeGettext();
80 // Otherwise, use PHP-gettext
81 // we support both the old file hierarchy format and the new:
82 // pre-4.5: civicrm/l10n/xx_XX/civicrm.mo
83 // post-4.5: civicrm/l10n/xx_XX/LC_MESSAGES/civicrm.mo
84 require_once 'PHPgettext/streams.php';
85 require_once 'PHPgettext/gettext.php';
87 $mo_file = $config->gettextResourceDir
. $locale . DIRECTORY_SEPARATOR
. 'LC_MESSAGES' . DIRECTORY_SEPARATOR
. 'civicrm.mo';
89 if (! file_exists($mo_file)) {
90 // fallback to pre-4.5 mode
91 $mo_file = $config->gettextResourceDir
. $locale . DIRECTORY_SEPARATOR
. 'civicrm.mo';
94 $streamer = new FileReader($mo_file);
95 $this->_phpgettext
= new gettext_reader($streamer);
100 * Returns whether gettext is running natively or using PHP-Gettext.
102 * @return bool True if gettext is native
104 function isNative() {
105 return $this->_nativegettext
;
109 * Return languages available in this instance of CiviCRM.
111 * @param $justEnabled boolean whether to return all languages or just the enabled ones
113 * @return array of code/language name mappings
115 static function languages($justEnabled = FALSE) {
117 static $enabled = NULL;
120 $all = CRM_Contact_BAO_Contact
::buildOptions('preferred_language');
122 // check which ones are available; add them to $all if not there already
123 $config = CRM_Core_Config
::singleton();
125 if (is_dir($config->gettextResourceDir
)) {
126 $dir = opendir($config->gettextResourceDir
);
127 while ($filename = readdir($dir)) {
128 if (preg_match('/^[a-z][a-z]_[A-Z][A-Z]$/', $filename)) {
129 $codes[] = $filename;
130 if (!isset($all[$filename])) {
131 $all[$filename] = $filename;
138 // drop the unavailable languages (except en_US)
139 foreach (array_keys($all) as $code) {
140 if ($code == 'en_US') {
143 if (!in_array($code, $codes))unset($all[$code]);
147 if ($enabled === NULL) {
148 $config = CRM_Core_Config
::singleton();
150 if (isset($config->languageLimit
) and $config->languageLimit
) {
151 foreach ($all as $code => $name) {
152 if (in_array($code, array_keys($config->languageLimit
))) {
153 $enabled[$code] = $name;
159 return $justEnabled ?
$enabled : $all;
163 * Replace arguments in a string with their values. Arguments are represented by % followed by their number.
165 * @param $str string source string
166 * @param mixed arguments, can be passed in an array or through single variables
168 * @return string modified string
170 function strarg($str) {
173 for ($i = 1; $i < func_num_args(); $i++
) {
174 $arg = func_get_arg($i);
175 if (is_array($arg)) {
176 foreach ($arg as $aarg) {
177 $tr['%' . ++
$p] = $aarg;
181 $tr['%' . ++
$p] = $arg;
184 return strtr($str, $tr);
188 * Smarty block function, provides gettext support for smarty.
190 * The block content is the text that should be translated.
192 * Any parameter that is sent to the function will be represented as %n in the translation text,
193 * where n is 1 for the first parameter. The following parameters are reserved:
194 * - escape - sets escape mode:
195 * - 'html' for HTML escaping, this is the default.
196 * - 'js' for javascript escaping.
197 * - 'no'/'off'/0 - turns off escaping
198 * - plural - The plural version of the text (2nd parameter of ngettext())
199 * - count - The item count for plural mode (3rd parameter of ngettext())
200 * - context - gettext context of that string (for homonym handling)
202 * @param $text string the original string
203 * @param $params array the params of the translation (if any)
205 * @return string the translated string
207 function crm_translate($text, $params = array()) {
208 if (isset($params['escape'])) {
209 $escape = $params['escape'];
210 unset($params['escape']);
213 // sometimes we need to {ts}-tag a string, but don’t want to
214 // translate it in the template (like civicrm_navigation.tpl),
215 // because we handle the translation in a different way (CRM-6998)
216 // in such cases we return early, only doing SQL/JS escaping
217 if (isset($params['skip']) and $params['skip']) {
218 if (isset($escape) and ($escape == 'sql')) {
219 $text = mysql_escape_string($text);
221 if (isset($escape) and ($escape == 'js')) {
222 $text = addcslashes($text, "'");
227 if (isset($params['plural'])) {
228 $plural = $params['plural'];
229 unset($params['plural']);
230 if (isset($params['count'])) {
231 $count = $params['count'];
235 if (isset($params['context'])) {
236 $context = $params['context'];
237 unset($params['context']);
243 // gettext domain for extensions
244 $domain_changed = FALSE;
245 if (isset($params['domain'])) {
246 if ($this->setGettextDomain($params['domain'])) {
247 $domain_changed = TRUE;
251 // do all wildcard translations first
252 $config = CRM_Core_Config
::singleton();
253 $stringTable = CRM_Utils_Array
::value(
255 $config->localeCustomStrings
259 if (isset($stringTable['enabled']['exactMatch'])) {
260 foreach ($stringTable['enabled']['exactMatch'] as $search => $replace) {
261 if ($search === $text) {
271 isset($stringTable['enabled']['wildcardMatch'])
273 $search = array_keys($stringTable['enabled']['wildcardMatch']);
274 $replace = array_values($stringTable['enabled']['wildcardMatch']);
275 $text = str_replace($search, $replace, $text);
278 // dont translate if we've done exactMatch already
280 // use plural if required parameters are set
281 if (isset($count) && isset($plural)) {
283 if ($this->_phpgettext
) {
284 $text = $this->_phpgettext
->ngettext($text, $plural, $count);
287 // if the locale's not set, we do ngettext work by hand
288 // if $count == 1 then $text = $text, else $text = $plural
294 // expand %count in translated string to $count
295 $text = strtr($text, array('%count' => $count));
297 // if not plural, but the locale's set, translate
299 elseif ($this->_phpgettext
) {
301 $text = $this->_phpgettext
->pgettext($context, $text);
304 $text = $this->_phpgettext
->translate($text);
309 // replace the numbered %1, %2, etc. params if present
310 if (count($params)) {
311 $text = $this->strarg($text, $params);
314 // escape SQL if we were asked for it
315 if (isset($escape) and ($escape == 'sql')) {
316 $text = mysql_escape_string($text);
319 // escape for JavaScript (if requested)
320 if (isset($escape) and ($escape == 'js')) {
321 $text = addcslashes($text, "'");
324 if ($domain_changed) {
325 $this->setGettextDomain('civicrm');
332 * Translate a string to the current locale.
334 * @param $string string this string should be translated
336 * @return string the translated string
338 function translate($string) {
339 return ($this->_phpgettext
) ?
$this->_phpgettext
->translate($string) : $string;
343 * Localize (destructively) array values.
345 * @param $array array the array for localization (in place)
346 * @param $params array an array of additional parameters
350 function localizeArray(
356 if ($tsLocale == 'en_US') {
360 foreach ($array as & $value) {
362 $value = ts($value, $params);
368 * Localize (destructively) array elements with keys of 'title'.
370 * @param $array array the array for localization (in place)
374 function localizeTitles(&$array) {
375 foreach ($array as $key => $value) {
376 if (is_array($value)) {
377 $this->localizeTitles($value);
378 $array[$key] = $value;
380 elseif ((string ) $key == 'title') {
381 $array[$key] = ts($value, array('context' => 'menu'));
387 * Binds a gettext domain, wrapper over bindtextdomain().
389 * @param $key Key of the extension (can be 'civicrm', or 'org.example.foo').
393 function setGettextDomain($key) {
394 static $cache = array();
396 // It's only necessary to find once
397 if (! isset($cache[$key])) {
398 $config = CRM_Core_Config
::singleton();
401 $mapper = CRM_Extension_System
::singleton()->getMapper();
402 $path = $mapper->keyToBasePath($key);
403 $info = $mapper->keyToInfo($key);
404 $domain = $info->file
;
406 bindtextdomain($domain, $path . DIRECTORY_SEPARATOR
. 'l10n');
407 bind_textdomain_codeset($domain, 'UTF-8');
408 $cache[$key] = $domain;
410 catch (CRM_Extension_Exception
$e) {
411 // There's not much we can do at this point
412 $cache[$key] = FALSE;
416 if (isset($cache[$key]) && $cache[$key]) {
417 textdomain($cache[$key]);
422 * Static instance provider - return the instance for the current locale.
424 static function &singleton() {
425 static $singleton = array();
428 if (!isset($singleton[$tsLocale])) {
429 $singleton[$tsLocale] = new CRM_Core_I18n($tsLocale);
432 return $singleton[$tsLocale];
436 * Set the LC_TIME locale if it's not set already (for a given language choice).
438 * @return string the final LC_TIME that got set
440 static function setLcTime() {
441 static $locales = array();
444 if (!isset($locales[$tsLocale])) {
445 // with the config being set to pl_PL: try pl_PL.UTF-8,
446 // then pl_PL, if neither present fall back to C
447 $locales[$tsLocale] = setlocale(LC_TIME
, $tsLocale . '.UTF-8', $tsLocale, 'C');
450 return $locales[$tsLocale];
455 * Short-named function for string translation, defined in global scope so it's available everywhere.
457 * @param $text string string for translating
458 * @param $params array an array of additional parameters
460 * @return string the translated string
462 function ts($text, $params = array()) {
463 static $config = NULL;
464 static $locale = NULL;
466 static $function = NULL;
473 $config = CRM_Core_Config
::singleton();
477 if (!$i18n or $locale != $tsLocale) {
478 $i18n = CRM_Core_I18n
::singleton();
480 if (isset($config->customTranslateFunction
) and function_exists($config->customTranslateFunction
)) {
481 $function = $config->customTranslateFunction
;
486 return $function($text, $params);
489 return $i18n->crm_translate($text, $params);