gettext.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?PHP
  2. /**
  3. * gettext.php
  4. *
  5. * Copyright (c) 1999-2001 The SquirrelMail Development 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. * $Id$
  15. */
  16. /*****************************************************************/
  17. /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
  18. /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
  19. /*** + Base level indent should begin at left margin, as ***/
  20. /*** the global definition below. ***/
  21. /*** + All identation should consist of four space blocks ***/
  22. /*** + Tab characters are evil. ***/
  23. /*** + all comments should use "slash-star ... star-slash" ***/
  24. /*** style -- no pound characters, no slash-slash style ***/
  25. /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
  26. /*** ALWAYS USE { AND } CHARACTERS!!! ***/
  27. /*** + Please use ' instead of ", when possible. Note " ***/
  28. /*** should always be used in _( ) function calls. ***/
  29. /*** Thank you for your help making the SM code more readable. ***/
  30. /*****************************************************************/
  31. global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
  32. $gettext_php_translateStrings, $gettext_php_loaded_language,
  33. $gettext_php_short_circuit;
  34. if (! isset($gettext_php_loaded)) {
  35. $gettext_php_loaded = false;
  36. session_register('gettext_php_loaded');
  37. }
  38. if (! isset($gettext_php_domain)) {
  39. $gettext_php_domain = '';
  40. session_register('gettext_php_domain');
  41. }
  42. if (! isset($gettext_php_dir)) {
  43. $gettext_php_dir = '';
  44. session_register('gettext_php_dir');
  45. }
  46. if (! isset($gettext_php_translateStrings)) {
  47. $gettext_php_translateStrings = array();
  48. session_register('gettext_php_translateStrings');
  49. }
  50. if (! isset($gettext_php_loaded_language)) {
  51. $gettext_php_loaded_language = '';
  52. session_register('gettext_php_loaded_language');
  53. }
  54. if (! isset($gettext_php_short_circuit)) {
  55. $gettext_php_short_circuit = false;
  56. session_register('gettext_php_short_circuit');
  57. }
  58. function gettext_php_load_strings() {
  59. global $squirrelmail_language, $gettext_php_translateStrings,
  60. $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
  61. $gettext_php_loaded_language, $gettext_php_short_circuit;
  62. // $squirrelmail_language gives 'en' for English, 'de' for German,
  63. // etc. I didn't wanna use getenv or similar, but you easily could
  64. // change my code to do that.
  65. $gettext_php_translateStrings = array();
  66. $gettext_php_short_circuit = false; // initialization
  67. $filename = $gettext_php_dir;
  68. if (substr($filename, -1) != '/')
  69. $filename .= '/';
  70. $filename .= $squirrelmail_language . '/LC_MESSAGES/' .
  71. $gettext_php_domain . '.po';
  72. $file = @fopen($filename, 'r');
  73. if ($file == false) {
  74. // Uh-ho -- we can't load the file. Just fake it. :-)
  75. // This is also for English, which doesn't use translations
  76. $gettext_php_loaded = true;
  77. $gettext_php_loaded_language = $squirrelmail_language;
  78. $gettext_php_short_circuit = true; // Avoid fuzzy matching when we
  79. // didn't load strings
  80. return;
  81. }
  82. $key = '';
  83. $SkipRead = false;
  84. while (! feof($file)) {
  85. if (! $SkipRead)
  86. $line = trim(fgets($file, 4096));
  87. else
  88. $SkipRead = false;
  89. if (ereg('^msgid "(.*)"$', $line, $match)) {
  90. if ($match[1] == '') {
  91. // Potential multi-line
  92. // msgid ""
  93. // "string string "
  94. // "string string"
  95. $key = '';
  96. $line = trim(fgets($file, 4096));
  97. while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
  98. $key .= $match[1];
  99. $line = trim(fgets($file, 4096));
  100. }
  101. $SkipRead = true;
  102. } else {
  103. // msgid "string string"
  104. $key = $match[1];
  105. }
  106. } elseif (ereg('^msgstr "(.*)"$', $line, $match)) {
  107. if ($match[1] == '') {
  108. // Potential multi-line
  109. // msgstr ""
  110. // "string string "
  111. // "string string"
  112. $gettext_php_translateStrings[$key] = '';
  113. $line = trim(fgets($file, 4096));
  114. while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
  115. $gettext_php_translateStrings[$key] .= $match[1];
  116. $line = trim(fgets($file, 4096));
  117. }
  118. $SkipRead = true;
  119. } else {
  120. // msgstr "string string"
  121. $gettext_php_translateStrings[$key] = $match[1];
  122. }
  123. $gettext_php_translateStrings[$key] =
  124. stripslashes($gettext_php_translateStrings[$key]);
  125. // If there is no translation, just use the untranslated string
  126. if ($gettext_php_translateStrings[$key] == '')
  127. $gettext_php_translateStrings[$key] = $key;
  128. $key = '';
  129. }
  130. }
  131. fclose($file);
  132. $gettext_php_loaded = true;
  133. $gettext_php_loaded_language = $squirrelmail_language;
  134. }
  135. function _($str) {
  136. global $gettext_php_loaded, $gettext_php_translateStrings,
  137. $squirrelmail_language, $gettext_php_loaded_language,
  138. $gettext_php_short_circuit;
  139. if (! $gettext_php_loaded ||
  140. $gettext_php_loaded_language != $squirrelmail_language)
  141. gettext_php_load_strings();
  142. // Try finding the exact string
  143. if (isset($gettext_php_translateStrings[$str]))
  144. return $gettext_php_translateStrings[$str];
  145. // See if we should short-circuit
  146. if ($gettext_php_short_circuit) {
  147. $gettext_php_translateStrings[$str] = $str;
  148. return $str;
  149. }
  150. // Look for a string that is very close to the one we want
  151. // Very computationally expensive
  152. $oldPercent = 0;
  153. $oldStr = '';
  154. $newPercent = 0;
  155. foreach ($gettext_php_translateStrings as $k => $v) {
  156. similar_text($str, $k, $newPercent);
  157. if ($newPercent > $oldPercent) {
  158. $oldStr = $v;
  159. $oldPercent = $newPercent;
  160. }
  161. }
  162. // Require 80% match or better
  163. // Adjust to suit your needs
  164. if ($oldPercent > 80) {
  165. // Remember this so we don't need to search again
  166. $gettext_php_translateStrings[$str] = $oldStr;
  167. return $oldStr;
  168. }
  169. // Remember this so we don't need to search again
  170. $gettext_php_translateStrings[$str] = $str;
  171. return $str;
  172. }
  173. function bindtextdomain($name, $dir) {
  174. global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded;
  175. if ($gettext_php_domain != $name) {
  176. $gettext_php_domain = $name;
  177. $gettext_php_loaded = false;
  178. }
  179. if ($gettext_php_dir != $dir) {
  180. $gettext_php_dir = $dir;
  181. $gettext_php_loaded = false;
  182. }
  183. return $dir;
  184. }
  185. function textdomain($name = false) {
  186. global $gettext_php_domain, $gettext_php_loaded;
  187. if ($name != false && $gettext_php_domain != $name) {
  188. $gettext_php_domain = $name;
  189. $gettext_php_loaded = false;
  190. }
  191. return $gettext_php_domain;
  192. }