Source for file gettext.php

Documentation is available at gettext.php

  1. <?php
  2.  
  3. /**
  4.  * SquirrelMail internal gettext functions
  5.  *
  6.  * Alternate to the system's built-in gettext. Relies on .po files (can't read
  7.  * .mo easily). Uses the session for caching (speed increase). Possible use in
  8.  * other PHP scripts? The only SM-specific thing is $sm_language, I think.
  9.  *
  10.  * @link http://www.php.net/gettext Original php gettext manual
  11.  * @copyright &copy; 1999-2006 The SquirrelMail Project Team
  12.  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  13.  * @version $Id: gettext.php,v 1.19.2.6 2006/04/14 22:27:07 jervfors Exp $
  14.  * @since 1.1.2
  15.  * @package squirrelmail
  16.  * @subpackage i18n
  17.  */
  18.  
  19. /** Almost everything requires global.php... */
  20. require_once(SM_PATH . 'functions/global.php');
  21.  
  22. global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
  23.  $gettext_php_translateStrings, $gettext_php_loaded_language,
  24.  $gettext_php_short_circuit;
  25.  
  26. if (! isset($gettext_php_loaded)) {
  27.     $gettext_php_loaded = false;
  28.     sqsession_register($gettext_php_loaded, 'gettext_php_loaded');
  29. }
  30. if (! isset($gettext_php_domain)) {
  31.     $gettext_php_domain = '';
  32.     sqsession_register($gettext_php_domain, 'gettext_php_domain');
  33. }
  34. if (! isset($gettext_php_dir)) {
  35.     $gettext_php_dir = '';
  36.     sqsession_register($gettext_php_dir, 'gettext_php_dir');
  37. }
  38. if (! isset($gettext_php_translateStrings)) {
  39.     $gettext_php_translateStrings = array();
  40.     sqsession_register($gettext_php_translateStrings, 'gettext_php_translateStrings');
  41. }
  42. if (! isset($gettext_php_loaded_language)) {
  43.     $gettext_php_loaded_language = '';
  44.     sqsession_register($gettext_php_loaded_language, 'gettext_php_loaded_language');
  45. }
  46. if (! isset($gettext_php_short_circuit)) {
  47.     $gettext_php_short_circuit = false;
  48.     sqsession_register($gettext_php_short_circuit, 'gettext_php_short_circuit');
  49. }
  50.  
  51. /**
  52.  * Converts .po file into array and stores it in session.
  53.  *
  54.  * Used internally by _($str) function
  55.  *
  56.  * @internal function is used internally by functions/gettext.php code
  57.  */
  58.     global $squirrelmail_language, $gettext_php_translateStrings,
  59.         $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
  60.         $gettext_php_loaded_language, $gettext_php_short_circuit;
  61.     
  62.     /*
  63.      * $squirrelmail_language gives 'en' for English, 'de' for German,
  64.      * etc.  I didn't wanna use getenv or similar, but you easily could
  65.      * change my code to do that. 
  66.      */
  67.     
  68.     $gettext_php_translateStrings = array();
  69.     
  70.     $gettext_php_short_circuit = false;  /* initialization */
  71.     
  72.     $filename = $gettext_php_dir;
  73.     if (substr($filename, -1) != '/')
  74.         $filename .= '/';
  75.     $filename .= $squirrelmail_language . '/LC_MESSAGES/' . 
  76.         $gettext_php_domain . '.po';
  77.     
  78.     $file = @fopen($filename, 'r');
  79.     if ($file == false) {
  80.         /* Uh-ho -- we can't load the file.  Just fake it.  :-)
  81.            This is also for English, which doesn't use translations */
  82.         $gettext_php_loaded = true;
  83.         $gettext_php_loaded_language = $squirrelmail_language;
  84.         /* Avoid fuzzy matching when we didn't load strings */
  85.         $gettext_php_short_circuit = true;  
  86.         return;
  87.     }
  88.     
  89.     $key = '';
  90.     $SkipRead = false;
  91.     while (! feof($file)) {
  92.         if (! $SkipRead) {
  93.             $line = trim(fgets($file, 4096));
  94.         } else {
  95.             $SkipRead = false;
  96.         }
  97.         
  98.         if (ereg('^msgid "(.*)"$', $line, $match)) {
  99.             if ($match[1] == '') {
  100.                 /*
  101.                  * Potential multi-line
  102.                  * msgid ""
  103.                  * "string string "
  104.                  * "string string"
  105.                  */
  106.                 $key = '';
  107.                 $line = trim(fgets($file, 4096));
  108.                 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
  109.                     $key .= $match[1];
  110.                     $line = trim(fgets($file, 4096));
  111.                 }
  112.                 $SkipRead = true;
  113.             } else {
  114.                 /* msgid "string string" */
  115.                 $key = $match[1];
  116.             }
  117.         } elseif (ereg('^msgstr "(.*)"$', $line, $match)) {
  118.             if ($match[1] == '') {
  119.                 /*
  120.                  * Potential multi-line
  121.                  * msgstr ""
  122.                  * "string string "
  123.                  * "string string"
  124.                  */
  125.                 $gettext_php_translateStrings[$key] = '';
  126.                 $line = trim(fgets($file, 4096));
  127.                 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
  128.                     $gettext_php_translateStrings[$key] .= $match[1];
  129.                     $line = trim(fgets($file, 4096));
  130.                 }
  131.                 $SkipRead = true;
  132.             } else {
  133.                 /* msgstr "string string" */
  134.                 $gettext_php_translateStrings[$key] = $match[1];
  135.             }
  136.             $gettext_php_translateStrings[$key] =
  137.                 stripslashes($gettext_php_translateStrings[$key]);
  138.             /* If there is no translation, just use the untranslated string */
  139.             if ($gettext_php_translateStrings[$key] == '') {
  140.                 $gettext_php_translateStrings[$key] = $key;
  141.             }
  142.             $key = '';
  143.         }
  144.     }
  145.     fclose($file);
  146.     
  147.     $gettext_php_loaded = true;
  148.     $gettext_php_loaded_language = $squirrelmail_language;
  149. }
  150.  
  151. /**
  152.  * Alternative php gettext function (short form)
  153.  *
  154.  * @link http://www.php.net/function.gettext
  155.  *
  156.  * @param string $str English string
  157.  * @return string translated string
  158.  */
  159. function _($str) {
  160.     global $gettext_php_loaded, $gettext_php_translateStrings, 
  161.         $squirrelmail_language, $gettext_php_loaded_language,
  162.         $gettext_php_short_circuit;
  163.     
  164.     if (! $gettext_php_loaded || 
  165.         $gettext_php_loaded_language != $squirrelmail_language) {
  166.         gettext_php_load_strings();
  167.     }
  168.     
  169.     /* Try finding the exact string */
  170.     if (isset($gettext_php_translateStrings[$str])) {
  171.         return $gettext_php_translateStrings[$str];
  172.     }
  173.     
  174.     /* See if we should short-circuit */
  175.     if ($gettext_php_short_circuit) {
  176.         $gettext_php_translateStrings[$str] = $str;
  177.         return $str;
  178.     }
  179.  
  180.     /* don't do fuzzy matching for strings with sprintf() formating */
  181.     if (! preg_match('/\%[\%bcdeufFosxX]/',$str)) {
  182.         /* Look for a string that is very close to the one we want
  183.          * Very computationally expensive */
  184.         $oldPercent = 0;
  185.         $oldStr = '';
  186.         $newPercent = 0;
  187.         foreach ($gettext_php_translateStrings as $k => $v) {
  188.             similar_text($str, $k, $newPercent);
  189.             if ($newPercent > $oldPercent) {
  190.                 $oldStr = $v;
  191.                 $oldPercent = $newPercent;
  192.             }
  193.         }
  194.         /* Require 80% match or better
  195.          * Adjust to suit your needs */
  196.         if ($oldPercent > 80) {
  197.             /* Remember this so we don't need to search again */
  198.             $gettext_php_translateStrings[$str] = $oldStr;
  199.             return $oldStr;
  200.         }
  201.     }
  202.     /* Remember this so we don't need to search again */
  203.     $gettext_php_translateStrings[$str] = $str;
  204.     return $str;
  205. }
  206.  
  207. /**
  208.  * Alternative php bindtextdomain function
  209.  *
  210.  * Sets path to directory containing domain translations
  211.  *
  212.  * @link http://www.php.net/function.bindtextdomain
  213.  * @param string $name gettext domain name
  214.  * @param string $dir directory that contains all translations
  215.  * @return string path to translation directory
  216.  */
  217. function bindtextdomain($name, $dir) {
  218.     global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded;
  219.     
  220.     if ($gettext_php_domain != $name) {
  221.         $gettext_php_domain = $name;
  222.         $gettext_php_loaded = false;
  223.     }
  224.     if ($gettext_php_dir != $dir) {
  225.         $gettext_php_dir = $dir;
  226.         $gettext_php_loaded = false;
  227.     }
  228.     
  229.     return $dir;
  230. }
  231.  
  232. /**
  233.  * Alternative php textdomain function
  234.  *
  235.  * Sets default domain name
  236.  *
  237.  * @link http://www.php.net/function.textdomain
  238.  * @param string $name gettext domain name
  239.  * @return string gettext domain name
  240.  */
  241. function textdomain($name = false) {
  242.     global $gettext_php_domain, $gettext_php_loaded;
  243.     
  244.     if ($name != false && $gettext_php_domain != $name) {
  245.         $gettext_php_domain = $name;
  246.         $gettext_php_loaded = false;
  247.     }
  248.  
  249.     return $gettext_php_domain;
  250. }
  251. ?>

Documentation generated on Sat, 07 Oct 2006 16:31:25 +0300 by phpDocumentor 1.3.0RC6