gettext.php 6.5 KB

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