Merge pull request #5449 from pratikshad/fixedWebtest4.6
[civicrm-core.git] / CRM / Core / I18n.php
index 825b00ca72c5da7babe66dfe4c288550fd37d188..fee6c84dd2c2e094ca6e500f562a14cd1b5adb5f 100644 (file)
@@ -23,7 +23,7 @@
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
@@ -123,7 +123,7 @@ class CRM_Core_I18n {
    *   whether to return all languages or just the enabled ones.
    *
    * @return array
-   *   of code/language name mappings
+   *   Array of code/language name mappings
    */
   public static function languages($justEnabled = FALSE) {
     static $all = NULL;
@@ -152,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]);
+        }
       }
     }
 
@@ -176,7 +178,6 @@ class CRM_Core_I18n {
    *
    * @param string $str
    *   source string.
-   * @param mixed arguments, can be passed in an array or through single variables
    *
    * @return string
    *   modified string
@@ -216,8 +217,9 @@ class CRM_Core_I18n {
    * @param string $text
    *   the original string.
    * @param array $params
-   *   the params of the translation (if any).
-   *
+   *   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
    */
@@ -241,6 +243,7 @@ class CRM_Core_I18n {
       return $text;
     }
 
+    $plural = $count = NULL;
     if (isset($params['plural'])) {
       $plural = $params['plural'];
       unset($params['plural']);
@@ -257,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;
       }
     }
@@ -323,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');
     }
@@ -368,7 +415,7 @@ class CRM_Core_I18n {
    *
    * @return void
    */
-  function localizeArray(
+  public function localizeArray(
     &$array,
     $params = array()
   ) {
@@ -411,7 +458,7 @@ class CRM_Core_I18n {
    * @param $key
    *   Key of the extension (can be 'civicrm', or 'org.example.foo').
    *
-   * @return Boolean
+   * @return Bool
    *   True if the domain was changed for an extension.
    */
   public function setGettextDomain($key) {
@@ -466,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();
@@ -496,6 +545,7 @@ class CRM_Core_I18n {
 
     return $locales[$tsLocale];
   }
+
 }
 
 /**
@@ -503,7 +553,7 @@ class CRM_Core_I18n {
  *
  * @param $text
  *   String string for translating.
- * @param $params
+ * @param array $params
  *   Array an array of additional parameters.
  *
  * @return string