3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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; should stay null if the strings are not to be translated (en_US).
40 private $_phpgettext = NULL;
43 * Whether we are using native gettext or not.
45 private $_nativegettext = FALSE;
48 * A locale-based constructor that shouldn't be called from outside of this class (use singleton() instead).
50 * @param $locale string the base of this certain object's existence
54 function __construct($locale) {
55 if ($locale != '' and $locale != 'en_US') {
56 $config = CRM_Core_Config
::singleton();
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
61 $this->_nativegettext
= TRUE;
64 putenv("LANG=$locale");
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);
71 bindtextdomain('civicrm', $config->gettextResourceDir
);
72 bind_textdomain_codeset('civicrm', 'UTF-8');
73 textdomain('civicrm');
75 $this->_phpgettext
= new CRM_Core_I18n_NativeGettext();
79 // Otherwise, use PHP-gettext
80 require_once 'PHPgettext/streams.php';
81 require_once 'PHPgettext/gettext.php';
83 $streamer = new FileReader($config->gettextResourceDir
. $locale . DIRECTORY_SEPARATOR
. 'civicrm.mo');
84 $this->_phpgettext
= new gettext_reader($streamer);
89 * Returns whether gettext is running natively or using PHP-Gettext.
91 * @return bool True if gettext is native
94 return $this->_nativegettext
;
98 * Return languages available in this instance of CiviCRM.
100 * @param $justEnabled boolean whether to return all languages or just the enabled ones
102 * @return array of code/language name mappings
104 static function languages($justEnabled = FALSE) {
106 static $enabled = NULL;
109 $all = CRM_Core_I18n_PseudoConstant
::languages();
111 // check which ones are available; add them to $all if not there already
112 $config = CRM_Core_Config
::singleton();
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;
127 // drop the unavailable languages (except en_US)
128 foreach (array_keys($all) as $code) {
129 if ($code == 'en_US') {
132 if (!in_array($code, $codes))unset($all[$code]);
136 if ($enabled === NULL) {
137 $config = CRM_Core_Config
::singleton();
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;
148 return $justEnabled ?
$enabled : $all;
152 * Replace arguments in a string with their values. Arguments are represented by % followed by their number.
154 * @param $str string source string
155 * @param mixed arguments, can be passed in an array or through single variables
157 * @return string modified string
159 function strarg($str) {
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;
170 $tr['%' . ++
$p] = $arg;
173 return strtr($str, $tr);
177 * Smarty block function, provides gettext support for smarty.
179 * The block content is the text that should be translated.
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)
191 * @param $text string the original string
192 * @param $params array the params of the translation (if any)
194 * @return string the translated string
196 function crm_translate($text, $params = array()) {
197 if (isset($params['escape'])) {
198 $escape = $params['escape'];
199 unset($params['escape']);
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);
210 if (isset($escape) and ($escape == 'js')) {
211 $text = addcslashes($text, "'");
216 if (isset($params['plural'])) {
217 $plural = $params['plural'];
218 unset($params['plural']);
219 if (isset($params['count'])) {
220 $count = $params['count'];
224 if (isset($params['context'])) {
225 $context = $params['context'];
226 unset($params['context']);
232 // do all wildcard translations first
233 $config = CRM_Core_Config
::singleton();
234 $stringTable = CRM_Utils_Array
::value($config->lcMessages
,
235 $config->localeCustomStrings
239 if (isset($stringTable['enabled']['exactMatch'])) {
240 foreach ($stringTable['enabled']['exactMatch'] as $search => $replace) {
241 if ($search === $text) {
250 isset($stringTable['enabled']['wildcardMatch'])
252 $search = array_keys($stringTable['enabled']['wildcardMatch']);
253 $replace = array_values($stringTable['enabled']['wildcardMatch']);
254 $text = str_replace($search,
260 // dont translate if we've done exactMatch already
262 // use plural if required parameters are set
263 if (isset($count) && isset($plural)) {
265 if ($this->_phpgettext
) {
266 $text = $this->_phpgettext
->ngettext($text, $plural, $count);
269 // if the locale's not set, we do ngettext work by hand
270 // if $count == 1 then $text = $text, else $text = $plural
276 // expand %count in translated string to $count
277 $text = strtr($text, array('%count' => $count));
279 // if not plural, but the locale's set, translate
281 elseif ($this->_phpgettext
) {
283 $text = $this->_phpgettext
->pgettext($context, $text);
286 $text = $this->_phpgettext
->translate($text);
291 // replace the numbered %1, %2, etc. params if present
292 if (count($params)) {
293 $text = $this->strarg($text, $params);
296 // escape SQL if we were asked for it
297 if (isset($escape) and ($escape == 'sql')) {
298 $text = mysql_escape_string($text);
301 // escape for JavaScript (if requested)
302 if (isset($escape) and ($escape == 'js')) {
303 $text = addcslashes($text, "'");
310 * Translate a string to the current locale.
312 * @param $string string this string should be translated
314 * @return string the translated string
316 function translate($string) {
317 return ($this->_phpgettext
) ?
$this->_phpgettext
->translate($string) : $string;
321 * Localize (destructively) array values.
323 * @param $array array the array for localization (in place)
324 * @param $params array an array of additional parameters
328 function localizeArray(
334 if ($tsLocale == 'en_US') {
338 foreach ($array as & $value) {
340 $value = ts($value, $params);
346 * Localize (destructively) array elements with keys of 'title'.
348 * @param $array array the array for localization (in place)
352 function localizeTitles(&$array) {
353 foreach ($array as $key => $value) {
354 if (is_array($value)) {
355 $this->localizeTitles($value);
356 $array[$key] = $value;
358 elseif ((string ) $key == 'title') {
359 $array[$key] = ts($value, array('context' => 'menu'));
365 * Static instance provider - return the instance for the current locale.
367 static function &singleton() {
368 static $singleton = array();
371 if (!isset($singleton[$tsLocale])) {
372 $singleton[$tsLocale] = new CRM_Core_I18n($tsLocale);
375 return $singleton[$tsLocale];
379 * Set the LC_TIME locale if it's not set already (for a given language choice).
381 * @return string the final LC_TIME that got set
383 static function setLcTime() {
384 static $locales = array();
387 if (!isset($locales[$tsLocale])) {
388 // with the config being set to pl_PL: try pl_PL.UTF-8,
389 // then pl_PL, if neither present fall back to C
390 $locales[$tsLocale] = setlocale(LC_TIME
, $tsLocale . '.UTF-8', $tsLocale, 'C');
393 return $locales[$tsLocale];
398 * Short-named function for string translation, defined in global scope so it's available everywhere.
400 * @param $text string string for translating
401 * @param $params array an array of additional parameters
403 * @return string the translated string
405 function ts($text, $params = array()) {
406 static $config = NULL;
407 static $locale = NULL;
409 static $function = NULL;
416 $config = CRM_Core_Config
::singleton();
420 if (!$i18n or $locale != $tsLocale) {
421 $i18n = CRM_Core_I18n
::singleton();
423 if (isset($config->customTranslateFunction
) and function_exists($config->customTranslateFunction
)) {
424 $function = $config->customTranslateFunction
;
429 return $function($text, $params);
432 return $i18n->crm_translate($text, $params);