gettext.php 7.8 KB

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