Database errors should be sent to log instead of screen in debug mode.
[fa-stable.git] / includes / lang / gettext.php
index 9cedafc63ec21f5d435029785620f84646a07b78..72f246001c8ac6304f63f191a283d672c9ef8ec7 100644 (file)
@@ -26,19 +26,19 @@ define('GETTEXT_NATIVE', 1);
 define('GETTEXT_PHP', 2);
 
 function get_text_init($managerType = GETTEXT_NATIVE) {
-
-       if (!isset($_SESSION['get_text'])) {
+       global $GetText;
+       if (!isset($GetText)) {
 
         if ($managerType == GETTEXT_NATIVE) 
         {
             if (function_exists('gettext')) 
             {
-                $_SESSION['get_text'] = new gettext_native_support();
+                $GetText = new gettext_native_support();
                 return;
             }
         }
         // fail back to php support 
-               $_SESSION['get_text'] = new gettext_php_support();
+               $GetText = new gettext_php_support();
        }
 }
 
@@ -60,6 +60,7 @@ function is_error($err) {
 class gettext_native_support 
 {
     var $_interpolation_vars = array();
+    var $domain_path;
 
     /**
      * Set gettext language code.
@@ -80,13 +81,18 @@ class gettext_native_support
                $lshort = strtr($up, '-','');
                $ushort = strtr($low, '-','');
 
-        $set = setlocale(LC_ALL, $lang_code.".".$encoding, 
-                       $lang_code.".".$up, $lang_code.".".$low,
-                       $lang_code.".".$ushort, $lang_code.".".$lshort);
-                       
+               if ($lang_code == 'C')
+                       $set = setlocale(LC_ALL,'C');
+               else
+               $set = setlocale(LC_ALL, $lang_code.".".$encoding, 
+                               $lang_code.".".$up, $lang_code.".".$low,
+                               $lang_code.".".$ushort, $lang_code.".".$lshort);
+
         setlocale(LC_NUMERIC, 'C'); // important for numeric presentation etc.
         if ($set === false) 
         {
+                       if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') // don't do this test if server is WIN
+                               return 0;
             $str = sprintf('language code "%s", encoding "%s" not supported by your system',
                 $lang_code, $encoding);
             //$err = new GetText_Error($str);
@@ -100,7 +106,8 @@ class gettext_native_support
         */
        function check_support($lang_code, $encoding)
     {
-
+               if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') // don't do this test if server is WIN
+                       return true;
                $old = setlocale(LC_CTYPE, '0'); // LC_MESSAGES does not exist on Win
                $up = strtoupper($encoding);
                $low = strtolower($encoding);
@@ -122,8 +129,12 @@ class gettext_native_support
      */
     function add_domain($domain, $path=false, $version='')
     {
+        if ($path === false) 
+               $path = $this->domain_path;
         if ($path === false) 
                $path = "./locale";
+           if ($domain == "")
+               $domain = "?";
                if ($version) {
        // To avoid need for apache server restart after change of *.mo file
        // we have to include file version as part of filename.
@@ -179,7 +190,7 @@ class gettext_native_support
      */
     function gettext($key)
     {
-        $value = $this->_get_translation($key);
+       $value = $this->_get_translation($key);
         if ($value === false) {
             $str = sprintf('Unable to locate gettext key "%s"', $key);
             //$err = new GetText_Error($str);
@@ -288,8 +299,13 @@ class gettext_php_support extends gettext_native_support
      * @param string $path optional -- Repository path
      * @throws GetText_Error
      */
-    function add_domain($domain, $path = "./locale/", $version ='')
+    function add_domain($domain, $path = false, $version ='')
     {   
+        if ($path === false) 
+             $path = $this->domain_path;
+        if ($path === false) 
+               $path = "./locale";
+
        if ($version) {
                        $domain .= '-'.$version;
                }
@@ -304,7 +320,10 @@ class gettext_php_support extends gettext_native_support
             $this->_jobs[] = array($domain, $path); 
             return;
         }
-
+        // Don't fill the domains with false data, it increased the error.log
+               if (strpos($domain, $this->_lang_code) === false)
+               return;
         $err = $this->_load_domain($domain, $path);
         if ($err != 0) 
         {
@@ -341,7 +360,6 @@ class gettext_php_support extends gettext_native_support
         $d = new gettext_domain();
         $d->name = $domain;
         $d->path = $path;
-        
         if (!file_exists($php_domain) || (filemtime($php_domain) < filemtime($src_domain))) 
         {
             
@@ -458,6 +476,7 @@ class gettext_php_support_parser
      */
     function _parse_line($line, $nbr)
     {
+        $line = str_replace("\\\"", "'", $line); // Should be inside preg_match, but I couldn't find the solution. This works.
         if (preg_match('/^\s*?#/', $line)) { return; }
         if (preg_match('/^\s*?msgid \"(.*?)(?!<\\\)\"/', $line, $m)) {
             $this->_store_key();
@@ -529,9 +548,25 @@ class gettext_php_support_compiler
     }
 }
 
-/**
-* get_text related error.
+/*
+       Set current gettext domain path
 */
-//class GetText_Error extends PEAR_Error {}
+function set_ext_domain($path='') {
+       global $path_to_root, $GetText;
+       static $domain_stack = array('');
+
+       if ($path)      // save path on domain stack
+               array_unshift($domain_stack,  $path);
+       else
+       {
+               array_shift($domain_stack);
+               $path = $domain_stack[0];
+       }
 
+       $lang_path = $path_to_root . ($path ? '/' : '') .$path.'/lang';
+       // ignore change when extension does not provide translation structure and test for valid gettext.
+       if (file_exists($lang_path) && isset($GetText))
+               $GetText->add_domain($_SESSION['language']->code,
+                       $lang_path, $path ? '' : $_SESSION['language']->version);
+}
 ?>