Happy New Year
[squirrelmail.git] / functions / gettext.php
index ba44942b9a2d685c8b9135f5b561b02d541d101d..8faf07b7282544025e56c8a3cdf221f67fa0395e 100644 (file)
-<?PHP
+<?php
 
-   /* Alternate to the system's built-in gettext.
-    * relies on .po files (can't read .mo easily).
-    * Uses the session for caching (speed increase)
-    * Possible use in other PHP scripts?  The only SM-specific thing is
-    *   $sm_language, I think
-    */
-     
-   if (defined('gettext_php'))
-      return;
-   define('gettext_php', true);
-   
-   global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
-      $gettext_php_translateStrings;
-   
-   if (! isset($gettext_php_loaded)) {
-      $gettext_php_loaded = false;
-      session_register('gettext_php_loaded');
-   }
-   if (! isset($gettext_php_domain)) {
-      $gettext_php_domain = '';
-      session_register('gettext_php_translateStrings');
-   }
-   if (! isset($gettext_php_dir)) {
-      $gettext_php_dir = '';
-      session_register('gettext_php_translateStrings');
-   }
-   if (! isset($gettext_php_translateStrings)) {
-      $gettext_php_translateStrings = array();
-      session_register('gettext_php_translateStrings');
-   }
+/**
+ * SquirrelMail internal gettext functions
+ *
+ * Since 1.5.1 uses php-gettext classes.
+ * Original implementation was done by Tyler Akins (fidian)
+ *
+ * @link http://www.php.net/gettext Original php gettext manual
+ * @link http://savannah.nongnu.org/projects/php-gettext php-gettext classes
+ * @copyright 1999-2020 The SquirrelMail Project Team
+ * @license http://opensource.org/licenses/gpl-license.php GNU Public License
+ * @version $Id$
+ * @since 1.1.2
+ * @package squirrelmail
+ * @subpackage i18n
+ */
 
-   function gettext_php_load_strings() {
-      global $sm_language, $gettext_php_translateStrings,
-         $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded;
-      
-      // $sm_language gives 'en' for English, 'de' for German, etc.
-      // I didn't wanna use getenv or similar, but you easily could change
-      // my code to do that.
-      
-      $gettext_php_translateStrings = array();
-      
-      $filename = $gettext_php_dir;
-      if (substr($filename, -1) != '/')
-         $filename .= '/';
-      $filename .= $sm_language . '/LC_MESSAGES/' . $gettext_php_domain . '.po';
-      
-      $file = fopen($filename, 'r');
-      if ($file === false)
-         // Uh-ho
-         return;
-         
-      $key = '';
-      $SkipRead = false;
-      while (! feof($file)) {
-         if (! $SkipRead)
-            $line = trim(fgets($file, 4096));
-        else
-           $SkipRead = false;
-            
-         if (ereg('^msgid "(.*)"$', $line, $match)) {
-           if ($match[1] == '') {
-              // Potential multi-line
-              // msgid ""
-              // "string string "
-              // "string string"
-              $key = '';
-               $line = trim(fgets($file, 4096));
-              while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
-                 $key .= $match[1];
-                 $line = trim(fgets($file, 4096));
-              }
-           } else {
-              // msgid "string string"
-              $key = $match[1];
-           }
-        } elseif (ereg('^msgstr "(.*)"$', $line, $match)) {
-           if ($match[1] == '') {
-              // Potential multi-line
-              // msgstr ""
-              // "string string "
-              // "string string"
-              $gettext_php_translateStrings[$key] = '';
-              $line = trim(fgets($file, 4096));
-              while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
-                 $gettext_php_translateStrings[$key] .= $match[1];
-                 $line = trim(fgets($file, 4096));
-              }
-           } else {
-              // msgstr "string string"
-              $gettext_php_translateStrings[$key] = $match[1];
-           }
-           $key = '';
-        }
-      }
-      fclose($file);
-      
-      $gettext_php_loaded = true;
-   }
-   
-   function _($str) {
-      global $gettext_php_loaded;
-        
-      if (! $gettext_php_loaded)
-         gettext_php_load_strings();
-      
-      if (isset($gettext_php_translateStrings[$str]))
-         return $gettext_php_translateStrings[$str];
-      
-      return $str;
-   }
-   
-   function bindtextdomain($name, $dir) {
-      global $gettext_php_domain;
-      
-      $gettext_php_domain = $name;
-      $gettext_php_dir = $dir;
-      $gettext_php_loaded = false;
-      
-      return $dir;
-   }
-   
-   function textdomain($name = false) {
-      global $gettext_php_domain;
-      
-      if ($name != false)
-      {
-         $gettext_php_domain = $name;
-        $gettext_php_loaded = false;
-      }
-      return $gettext_php_domain;
-   }
-   
+
+/** Load classes and other functions */
+include_once(SM_PATH . 'class/l10n.class.php');
+include_once(SM_PATH . 'functions/ngettext.php');
+
+/**
+ * Alternative php gettext function (short form)
+ *
+ * @link http://www.php.net/function.gettext
+ *
+ * @param string $str English string
+ * @return string translated string
+ * @since 1.1.2
+ */
+function _($str) {
+    global $l10n, $gettext_domain;
+    if (! isset($l10n[$gettext_domain]) ||
+        ! is_object($l10n[$gettext_domain]) ||
+        $l10n[$gettext_domain]->error==1)
+        return $str;
+    return $l10n[$gettext_domain]->translate($str);
+}
+
+/**
+ * Alternative php bindtextdomain function
+ *
+ * Sets path to directory containing domain translations
+ *
+ * @link http://www.php.net/function.bindtextdomain
+ * @param string $domain gettext domain name
+ * @param string $dir directory that contains all translations
+ * @return string path to translation directory
+ * @since 1.1.2
+ */
+function bindtextdomain($domain, $dir) {
+    global $l10n, $sm_notAlias;
+    if (substr($dir, -1) != '/') $dir .= '/';
+    $mofile=$dir . $sm_notAlias . '/LC_MESSAGES/' . $domain . '.mo';
+
+    $input = new FileReader($mofile);
+    $l10n[$domain] = new gettext_reader($input);
+
+    return $dir;
+}
+
+/**
+ * Alternative php textdomain function
+ *
+ * Sets default domain name. Before 1.5.1 command required
+ * bindtextdomain() call for each gettext domain change.
+ *
+ * @link http://www.php.net/function.textdomain
+ * @param string $name gettext domain name
+ * @return string gettext domain name
+ * @since 1.1.2
+ */
+function textdomain($name = false) {
+    global $gettext_domain;
+    if ($name) $gettext_domain=$name;
+    return $gettext_domain;
+}
+
+/**
+ * Safety check.
+ * Setup where three standard gettext functions don't exist and dgettext() exists.
+ */
+if (! function_exists('dgettext')) {
+    /**
+     * Alternative php dgettext function
+     *
+     * @link http://www.php.net/function.dgettext
+     * @param string $domain Gettext domain
+     * @param string $str English string
+     * @return string translated string
+     * @since 1.5.1
+     */
+    function dgettext($domain, $str) {
+        global $l10n;
+        if (! isset($l10n[$domain]) ||
+            ! is_object($l10n[$domain]) ||
+            $l10n[$domain]->error==1)
+            return $str;
+        return $l10n[$domain]->translate($str);
+    }
+}