X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FCore%2FI18n.php;h=fee6c84dd2c2e094ca6e500f562a14cd1b5adb5f;hb=2a1b91d9d51fa0f993c625e31f08db52964876ea;hp=8fef7866cc29aee35257c8ea2018493b316595f6;hpb=5667521530926d61b370a4bb0806b7e5ad63ccbc;p=civicrm-core.git diff --git a/CRM/Core/I18n.php b/CRM/Core/I18n.php index 8fef7866cc..fee6c84dd2 100644 --- a/CRM/Core/I18n.php +++ b/CRM/Core/I18n.php @@ -23,7 +23,7 @@ | GNU Affero General Public License or the licensing of CiviCRM, | | see the CiviCRM license FAQ at http://civicrm.org/licensing | +--------------------------------------------------------------------+ -*/ + */ /** * @@ -55,7 +55,8 @@ class CRM_Core_I18n { /** * A locale-based constructor that shouldn't be called from outside of this class (use singleton() instead). * - * @param $locale string the base of this certain object's existence + * @param string $locale + * the base of this certain object's existence. * * @return \CRM_Core_I18n */ @@ -94,7 +95,7 @@ class CRM_Core_I18n { $mo_file = $config->gettextResourceDir . $locale . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . 'civicrm.mo'; - if (! file_exists($mo_file)) { + if (!file_exists($mo_file)) { // fallback to pre-4.5 mode $mo_file = $config->gettextResourceDir . $locale . DIRECTORY_SEPARATOR . 'civicrm.mo'; } @@ -108,7 +109,8 @@ class CRM_Core_I18n { /** * Returns whether gettext is running natively or using PHP-Gettext. * - * @return bool True if gettext is native + * @return bool + * True if gettext is native */ public function isNative() { return $this->_nativegettext; @@ -117,9 +119,11 @@ class CRM_Core_I18n { /** * Return languages available in this instance of CiviCRM. * - * @param $justEnabled boolean whether to return all languages or just the enabled ones + * @param bool $justEnabled + * whether to return all languages or just the enabled ones. * - * @return array of code/language name mappings + * @return array + * Array of code/language name mappings */ public static function languages($justEnabled = FALSE) { static $all = NULL; @@ -148,7 +152,9 @@ class CRM_Core_I18n { if ($code == 'en_US') { continue; } - if (!in_array($code, $codes))unset($all[$code]); + if (!in_array($code, $codes)) { + unset($all[$code]); + } } } @@ -170,10 +176,11 @@ class CRM_Core_I18n { /** * Replace arguments in a string with their values. Arguments are represented by % followed by their number. * - * @param $str string source string - * @param mixed arguments, can be passed in an array or through single variables + * @param string $str + * source string. * - * @return string modified string + * @return string + * modified string */ public function strarg($str) { $tr = array(); @@ -207,10 +214,14 @@ class CRM_Core_I18n { * - count - The item count for plural mode (3rd parameter of ngettext()) * - context - gettext context of that string (for homonym handling) * - * @param $text string the original string - * @param $params array the params of the translation (if any) - * - * @return string the translated string + * @param string $text + * the original string. + * @param array $params + * The params of the translation (if any). + * - domain: string|array a list of translation domains to search (in order) + * - context: string + * @return string + * the translated string */ public function crm_translate($text, $params = array()) { if (isset($params['escape'])) { @@ -232,6 +243,7 @@ class CRM_Core_I18n { return $text; } + $plural = $count = NULL; if (isset($params['plural'])) { $plural = $params['plural']; unset($params['plural']); @@ -248,10 +260,69 @@ class CRM_Core_I18n { $context = NULL; } + if (isset($params['domain'])) { + $domain = $params['domain']; + unset($params['domain']); + } + else { + $domain = NULL; + } + + $raw = !empty($params['raw']); + unset($params['raw']); + + if (!empty($domain)) { + // It might be prettier to cast to an array, but this is high-traffic stuff. + if (is_array($domain)) { + foreach ($domain as $d) { + $candidate = $this->crm_translate_raw($text, $d, $count, $plural, $context); + if ($candidate != $text) { + $text = $candidate; + break; + } + } + } + else { + $text = $this->crm_translate_raw($text, $domain, $count, $plural, $context); + } + } + else { + $text = $this->crm_translate_raw($text, NULL, $count, $plural, $context); + } + + // replace the numbered %1, %2, etc. params if present + if (count($params) && !$raw) { + $text = $this->strarg($text, $params); + } + + // escape SQL if we were asked for it + if (isset($escape) and ($escape == 'sql')) { + $text = CRM_Core_DAO::escapeString($text); + } + + // escape for JavaScript (if requested) + if (isset($escape) and ($escape == 'js')) { + $text = addcslashes($text, "'"); + } + + return $text; + } + + /** + * Lookup the raw translation of a string (without any extra escaping or interpolation). + * + * @param string $text + * @param string|NULL $domain + * @param int|NULL $count + * @param string $plural + * @param string $context + * @return mixed|string|translated + */ + protected function crm_translate_raw($text, $domain, $count, $plural, $context) { // gettext domain for extensions $domain_changed = FALSE; - if (! empty($params['domain']) && $this->_phpgettext) { - if ($this->setGettextDomain($params['domain'])) { + if (!empty($domain) && $this->_phpgettext) { + if ($this->setGettextDomain($domain)) { $domain_changed = TRUE; } } @@ -278,9 +349,9 @@ class CRM_Core_I18n { !$exactMatch && isset($stringTable['enabled']['wildcardMatch']) ) { - $search = array_keys($stringTable['enabled']['wildcardMatch']); + $search = array_keys($stringTable['enabled']['wildcardMatch']); $replace = array_values($stringTable['enabled']['wildcardMatch']); - $text = str_replace($search, $replace, $text); + $text = str_replace($search, $replace, $text); } // dont translate if we've done exactMatch already @@ -314,21 +385,6 @@ class CRM_Core_I18n { } } - // replace the numbered %1, %2, etc. params if present - if (count($params)) { - $text = $this->strarg($text, $params); - } - - // escape SQL if we were asked for it - if (isset($escape) and ($escape == 'sql')) { - $text = CRM_Core_DAO::escapeString($text); - } - - // escape for JavaScript (if requested) - if (isset($escape) and ($escape == 'js')) { - $text = addcslashes($text, "'"); - } - if ($domain_changed) { $this->setGettextDomain('civicrm'); } @@ -339,9 +395,11 @@ class CRM_Core_I18n { /** * Translate a string to the current locale. * - * @param $string string this string should be translated + * @param string $string + * this string should be translated. * - * @return string the translated string + * @return string + * the translated string */ public function translate($string) { return ($this->_phpgettext) ? $this->_phpgettext->translate($string) : $string; @@ -350,12 +408,14 @@ class CRM_Core_I18n { /** * Localize (destructively) array values. * - * @param $array array the array for localization (in place) - * @param $params array an array of additional parameters + * @param array $array + * the array for localization (in place). + * @param array $params + * an array of additional parameters. * - * @return void + * @return void */ - function localizeArray( + public function localizeArray( &$array, $params = array() ) { @@ -375,9 +435,10 @@ class CRM_Core_I18n { /** * Localize (destructively) array elements with keys of 'title'. * - * @param $array array the array for localization (in place) + * @param array $array + * the array for localization (in place). * - * @return void + * @return void */ public function localizeTitles(&$array) { foreach ($array as $key => $value) { @@ -394,18 +455,20 @@ class CRM_Core_I18n { /** * Binds a gettext domain, wrapper over bindtextdomain(). * - * @param $key Key of the extension (can be 'civicrm', or 'org.example.foo'). + * @param $key + * Key of the extension (can be 'civicrm', or 'org.example.foo'). * - * @return Boolean True if the domain was changed for an extension. + * @return Bool + * True if the domain was changed for an extension. */ public function setGettextDomain($key) { /* No domain changes for en_US */ - if (! $this->_phpgettext) { + if (!$this->_phpgettext) { return FALSE; } // It's only necessary to find/bind once - if (! isset($this->_extensioncache[$key])) { + if (!isset($this->_extensioncache[$key])) { $config = CRM_Core_Config::singleton(); try { @@ -450,6 +513,8 @@ class CRM_Core_I18n { /** * Static instance provider - return the instance for the current locale. + * + * @return CRM_Core_I18n */ public static function &singleton() { static $singleton = array(); @@ -465,7 +530,8 @@ class CRM_Core_I18n { /** * Set the LC_TIME locale if it's not set already (for a given language choice). * - * @return string the final LC_TIME that got set + * @return string + * the final LC_TIME that got set */ public static function setLcTime() { static $locales = array(); @@ -479,15 +545,19 @@ class CRM_Core_I18n { return $locales[$tsLocale]; } + } /** * Short-named function for string translation, defined in global scope so it's available everywhere. * - * @param $text string string for translating - * @param $params array an array of additional parameters + * @param $text + * String string for translating. + * @param array $params + * Array an array of additional parameters. * - * @return string the translated string + * @return string + * the translated string */ function ts($text, $params = array()) { static $config = NULL;