Merge pull request #10540 from colemanw/CRM-20091
[civicrm-core.git] / CRM / Core / I18n.php
index f5d55ccc7e66abc73201e5cc4f94969194f6386c..ca7706cd9d5caf7f5c428b7b35fc1df71ebef29f 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.7                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2016                                |
+ | Copyright CiviCRM LLC (c) 2004-2017                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2016
+ * @copyright CiviCRM LLC (c) 2004-2017
  */
 class CRM_Core_I18n {
 
@@ -78,41 +78,12 @@ class CRM_Core_I18n {
         // Note: the file hierarchy for .po must be, for example: l10n/fr_FR/LC_MESSAGES/civicrm.mo
 
         $this->_nativegettext = TRUE;
-
-        $locale .= '.utf8';
-        putenv("LANG=$locale");
-
-        // CRM-11833 Avoid LC_ALL because of LC_NUMERIC and potential DB error.
-        setlocale(LC_TIME, $locale);
-        setlocale(LC_MESSAGES, $locale);
-        setlocale(LC_CTYPE, $locale);
-
-        bindtextdomain('civicrm', CRM_Core_I18n::getResourceDir());
-        bind_textdomain_codeset('civicrm', 'UTF-8');
-        textdomain('civicrm');
-
-        $this->_phpgettext = new CRM_Core_I18n_NativeGettext();
-        $this->_extensioncache['civicrm'] = 'civicrm';
+        $this->setNativeGettextLocale($locale);
         return;
       }
 
       // Otherwise, use PHP-gettext
-      // we support both the old file hierarchy format and the new:
-      // pre-4.5:  civicrm/l10n/xx_XX/civicrm.mo
-      // post-4.5: civicrm/l10n/xx_XX/LC_MESSAGES/civicrm.mo
-      require_once 'PHPgettext/streams.php';
-      require_once 'PHPgettext/gettext.php';
-
-      $mo_file = CRM_Core_I18n::getResourceDir() . $locale . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . 'civicrm.mo';
-
-      if (!file_exists($mo_file)) {
-        // fallback to pre-4.5 mode
-        $mo_file = CRM_Core_I18n::getResourceDir() . $locale . DIRECTORY_SEPARATOR . 'civicrm.mo';
-      }
-
-      $streamer = new FileReader($mo_file);
-      $this->_phpgettext = new gettext_reader($streamer);
-      $this->_extensioncache['civicrm'] = $this->_phpgettext;
+      $this->setPhpGettextLocale($locale);
     }
   }
 
@@ -126,6 +97,56 @@ class CRM_Core_I18n {
     return $this->_nativegettext;
   }
 
+  /**
+   * Set native locale for getText.
+   *
+   * @param string $locale
+   */
+  protected function setNativeGettextLocale($locale) {
+
+    $locale .= '.utf8';
+    putenv("LANG=$locale");
+
+    // CRM-11833 Avoid LC_ALL because of LC_NUMERIC and potential DB error.
+    setlocale(LC_TIME, $locale);
+    setlocale(LC_MESSAGES, $locale);
+    setlocale(LC_CTYPE, $locale);
+
+    bindtextdomain('civicrm', CRM_Core_I18n::getResourceDir());
+    bind_textdomain_codeset('civicrm', 'UTF-8');
+    textdomain('civicrm');
+
+    $this->_phpgettext = new CRM_Core_I18n_NativeGettext();
+    $this->_extensioncache['civicrm'] = 'civicrm';
+
+  }
+
+  /**
+   * Set getText locale.
+   *
+   * @param string $locale
+   */
+  protected function setPhpGettextLocale($locale) {
+
+    // we support both the old file hierarchy format and the new:
+    // pre-4.5:  civicrm/l10n/xx_XX/civicrm.mo
+    // post-4.5: civicrm/l10n/xx_XX/LC_MESSAGES/civicrm.mo
+    require_once 'PHPgettext/streams.php';
+    require_once 'PHPgettext/gettext.php';
+
+    $mo_file = CRM_Core_I18n::getResourceDir() . $locale . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . 'civicrm.mo';
+
+    if (!file_exists($mo_file)) {
+      // fallback to pre-4.5 mode
+      $mo_file = CRM_Core_I18n::getResourceDir() . $locale . DIRECTORY_SEPARATOR . 'civicrm.mo';
+    }
+
+    $streamer = new FileReader($mo_file);
+    $this->_phpgettext = new gettext_reader($streamer);
+    $this->_extensioncache['civicrm'] = $this->_phpgettext;
+
+  }
+
   /**
    * Return languages available in this instance of CiviCRM.
    *
@@ -208,6 +229,11 @@ class CRM_Core_I18n {
     return strtr($str, $tr);
   }
 
+  /**
+   * Get the directory for l10n resources.
+   *
+   * @return string
+   */
   public static function getResourceDir() {
     static $dir = NULL;
     if ($dir === NULL) {
@@ -563,53 +589,27 @@ class CRM_Core_I18n {
   /**
    * Change the processing language without changing the current user language
    *
-   * @param $language
-   *   Language (for example 'en_US', or 'fr_CA').
+   * @param $locale
+   *   Locale (for example 'en_US', or 'fr_CA').
    *   True if the domain was changed for an extension.
    */
-  public function setLocale($language) {
-
-    $config = CRM_Core_Config::singleton();
+  public function setLocale($locale) {
 
     // Change the language of the CMS as well, for URLs.
-    CRM_Utils_System::setUFLocale($language);
+    CRM_Utils_System::setUFLocale($locale);
 
     // change the gettext ressources
     if ($this->_nativegettext) {
-      $locale = $language . '.utf8';
-      putenv("LANG=$locale");
-
-      setlocale(LC_TIME, $locale);
-      setlocale(LC_MESSAGES, $locale);
-      setlocale(LC_CTYPE, $locale);
-
-      bindtextdomain('civicrm', CRM_Core_I18n::getResourceDir());
-      bind_textdomain_codeset('civicrm', 'UTF-8');
-      textdomain('civicrm');
-
-      $this->_phpgettext = new CRM_Core_I18n_NativeGettext();
-      $this->_extensioncache['civicrm'] = 'civicrm';
+      $this->setNativeGettextLocale($locale);
     }
     else {
       // phpgettext
-      require_once 'PHPgettext/streams.php';
-      require_once 'PHPgettext/gettext.php';
-
-      $mo_file = CRM_Core_I18n::getResourceDir() . $locale . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . 'civicrm.mo';
-      if (!file_exists($mo_file)) {
-        // fallback to pre-4.5 mode
-        $mo_file = CRM_Core_I18n::getResourceDir() . $locale . DIRECTORY_SEPARATOR . 'civicrm.mo';
-      }
-
-      $streamer = new FileReader($mo_file);
-      $this->_phpgettext = new gettext_reader($streamer);
-      $this->_extensioncache['civicrm'] = $this->_phpgettext;
-
+      $this->setPhpGettextLocale($locale);
     }
 
     // for sql queries
     global $dbLocale;
-    $dbLocale = "_{$language}";
+    $dbLocale = "_{$locale}";
 
   }
 
@@ -682,7 +682,7 @@ class CRM_Core_I18n {
    */
   public static function getLocale() {
     global $tsLocale;
-    return $tsLocale;
+    return $tsLocale ? $tsLocale : 'en_US';
   }
 
 }