* It works. :-)
authorfidian <fidian@7612ce4b-ef26-0410-bec9-ea0150e637f0>
Fri, 27 Apr 2001 03:28:02 +0000 (03:28 +0000)
committerfidian <fidian@7612ce4b-ef26-0410-bec9-ea0150e637f0>
Fri, 27 Apr 2001 03:28:02 +0000 (03:28 +0000)
* $Luke->set_hero(&$Tyler);
* It also searches for translations when the source string has been altered
  a tiny amount (80% match on the string or better)
* It also caches everything, so it gets faster.  It only searches for unknown
  strings once per session.
* Unfortunately, it requires the .po files, so it isn't the ideal solution,
  but it is easy and will automatically work on more systems now.

Special thanks to Konstantin Riabitsev for letting me use the mricon.com
server!!

git-svn-id: https://svn.code.sf.net/p/squirrelmail/code/trunk/squirrelmail@1320 7612ce4b-ef26-0410-bec9-ea0150e637f0

functions/gettext.php

index ba44942b9a2d685c8b9135f5b561b02d541d101d..852f4c346831ef099a935904c2f087451e26314f 100644 (file)
@@ -5,6 +5,9 @@
     * Uses the session for caching (speed increase)
     * Possible use in other PHP scripts?  The only SM-specific thing is
     *   $sm_language, I think
+    *
+    * Very special thanks to Konstantin Riabitsev for letting me use a
+    * server that didn't already have gettext on it!
     */
      
    if (defined('gettext_php'))
@@ -12,7 +15,7 @@
    define('gettext_php', true);
    
    global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
-      $gettext_php_translateStrings;
+      $gettext_php_translateStrings, $gettext_php_loaded_language;
    
    if (! isset($gettext_php_loaded)) {
       $gettext_php_loaded = false;
    }
    if (! isset($gettext_php_domain)) {
       $gettext_php_domain = '';
-      session_register('gettext_php_translateStrings');
+      session_register('gettext_php_domain');
    }
    if (! isset($gettext_php_dir)) {
       $gettext_php_dir = '';
-      session_register('gettext_php_translateStrings');
+      session_register('gettext_php_dir');
    }
    if (! isset($gettext_php_translateStrings)) {
       $gettext_php_translateStrings = array();
       session_register('gettext_php_translateStrings');
    }
+   if (! isset($gettext_php_loaded_language)) {
+      $gettext_php_loaded_language = '';
+      session_register('gettext_php_loaded_language');
+   }
 
    function gettext_php_load_strings() {
-      global $sm_language, $gettext_php_translateStrings,
-         $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded;
+      global $squirrelmail_language, $gettext_php_translateStrings,
+         $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
+         $gettext_php_loaded_language;
       
-      // $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.
+      // $squirrelmail_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';
+      $filename .= $squirrelmail_language . '/LC_MESSAGES/' . 
+         $gettext_php_domain . '.po';
       
-      $file = fopen($filename, 'r');
-      if ($file === false)
-         // Uh-ho
+      $file = @fopen($filename, 'r');
+      if ($file === false) {
+         // Uh-ho -- we can't load the file.
+         // Just fake it.  :-)
+         $gettext_php_loaded = true;
          return;
+      }
          
       $key = '';
       $SkipRead = false;
@@ -71,6 +83,7 @@
                  $key .= $match[1];
                  $line = trim(fgets($file, 4096));
               }
+               $SkipRead = true;
            } else {
               // msgid "string string"
               $key = $match[1];
                  $gettext_php_translateStrings[$key] .= $match[1];
                  $line = trim(fgets($file, 4096));
               }
+               $SkipRead = true;
            } else {
               // msgstr "string string"
               $gettext_php_translateStrings[$key] = $match[1];
            }
+            $gettext_php_translateStrings[$key] =
+               stripslashes($gettext_php_translateStrings[$key]);
            $key = '';
         }
       }
       fclose($file);
-      
+
       $gettext_php_loaded = true;
+      $gettext_php_loaded_language = $squirrelmail_language;
    }
-   
+
    function _($str) {
-      global $gettext_php_loaded;
+      global $gettext_php_loaded, $gettext_php_translateStrings, 
+         $squirrelmail_language, $gettext_php_loaded_language;
         
-      if (! $gettext_php_loaded)
+      if (! $gettext_php_loaded || 
+          $gettext_php_loaded_language != $squirrelmail_language)
          gettext_php_load_strings();
-      
+
+      // Try finding the exact string      
       if (isset($gettext_php_translateStrings[$str]))
          return $gettext_php_translateStrings[$str];
+
+      // Look for a string that is very close to the one we want
+      // Very computationally expensive
+      $oldPercent = 0;
+      $oldStr = '';
+      $newPercent = 0;
+      foreach ($gettext_php_translateStrings as $k => $v) {
+         similar_text($str, $k, &$newPercent);
+         if ($newPercent > $oldPercent) {
+            $oldStr = $v;
+            $oldPercent = $newPercent;
+         }
+      }
+      // Require 80% match or better
+      // Adjust to suit your needs
+      if ($oldPercent > 80) {
+         // Remember this so we don't need to search again
+         $gettext_php_translateStrings[$str] = $oldStr;
+         return $oldStr;
+      }
       
+      // Remember this so we don't need to search again
+      $gettext_php_translateStrings[$str] = $str;
       return $str;
    }
    
    function bindtextdomain($name, $dir) {
-      global $gettext_php_domain;
-      
-      $gettext_php_domain = $name;
-      $gettext_php_dir = $dir;
-      $gettext_php_loaded = false;
+      global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded;
+
+      if ($gettext_php_domain != $name) {
+         $gettext_php_domain = $name;
+         $gettext_php_loaded = false;
+      }
+      if ($gettext_php_dir != $dir) {
+         $gettext_php_dir = $dir;
+         $gettext_php_loaded = false;
+      }
       
       return $dir;
    }
    
    function textdomain($name = false) {
-      global $gettext_php_domain;
-      
-      if ($name != false)
-      {
+      global $gettext_php_domain, $gettext_php_loaded;
+
+      if ($name != false && $gettext_php_domain != $name) {
          $gettext_php_domain = $name;
         $gettext_php_loaded = false;
       }