gettext.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. if (ereg('^msgid "(.*)"$', $line, $match)) {
  77. if ($match[1] == '') {
  78. /*
  79. * Potential multi-line
  80. * msgid ""
  81. * "string string "
  82. * "string string"
  83. */
  84. $key = '';
  85. $line = trim(fgets($file, 4096));
  86. while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
  87. $key .= $match[1];
  88. $line = trim(fgets($file, 4096));
  89. }
  90. $SkipRead = true;
  91. } else {
  92. /* msgid "string string" */
  93. $key = $match[1];
  94. }
  95. } elseif (ereg('^msgstr "(.*)"$', $line, $match)) {
  96. if ($match[1] == '') {
  97. /*
  98. * Potential multi-line
  99. * msgstr ""
  100. * "string string "
  101. * "string string"
  102. */
  103. $gettext_php_translateStrings[$key] = '';
  104. $line = trim(fgets($file, 4096));
  105. while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
  106. $gettext_php_translateStrings[$key] .= $match[1];
  107. $line = trim(fgets($file, 4096));
  108. }
  109. $SkipRead = true;
  110. } else {
  111. /* msgstr "string string" */
  112. $gettext_php_translateStrings[$key] = $match[1];
  113. }
  114. $gettext_php_translateStrings[$key] =
  115. stripslashes($gettext_php_translateStrings[$key]);
  116. /* If there is no translation, just use the untranslated string */
  117. if ($gettext_php_translateStrings[$key] == '') {
  118. $gettext_php_translateStrings[$key] = $key;
  119. }
  120. $key = '';
  121. }
  122. }
  123. fclose($file);
  124. $gettext_php_loaded = true;
  125. $gettext_php_loaded_language = $squirrelmail_language;
  126. }
  127. function _($str) {
  128. global $gettext_php_loaded, $gettext_php_translateStrings,
  129. $squirrelmail_language, $gettext_php_loaded_language,
  130. $gettext_php_short_circuit;
  131. if (! $gettext_php_loaded ||
  132. $gettext_php_loaded_language != $squirrelmail_language) {
  133. gettext_php_load_strings();
  134. }
  135. /* Try finding the exact string */
  136. if (isset($gettext_php_translateStrings[$str])) {
  137. return $gettext_php_translateStrings[$str];
  138. }
  139. /* See if we should short-circuit */
  140. if ($gettext_php_short_circuit) {
  141. $gettext_php_translateStrings[$str] = $str;
  142. return $str;
  143. }
  144. /* Look for a string that is very close to the one we want
  145. Very computationally expensive */
  146. $oldPercent = 0;
  147. $oldStr = '';
  148. $newPercent = 0;
  149. foreach ($gettext_php_translateStrings as $k => $v) {
  150. similar_text($str, $k, $newPercent);
  151. if ($newPercent > $oldPercent) {
  152. $oldStr = $v;
  153. $oldPercent = $newPercent;
  154. }
  155. }
  156. /* Require 80% match or better
  157. Adjust to suit your needs */
  158. if ($oldPercent > 80) {
  159. /* Remember this so we don't need to search again */
  160. $gettext_php_translateStrings[$str] = $oldStr;
  161. return $oldStr;
  162. }
  163. /* Remember this so we don't need to search again */
  164. $gettext_php_translateStrings[$str] = $str;
  165. return $str;
  166. }
  167. function bindtextdomain($name, $dir) {
  168. global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded;
  169. if ($gettext_php_domain != $name) {
  170. $gettext_php_domain = $name;
  171. $gettext_php_loaded = false;
  172. }
  173. if ($gettext_php_dir != $dir) {
  174. $gettext_php_dir = $dir;
  175. $gettext_php_loaded = false;
  176. }
  177. return $dir;
  178. }
  179. function textdomain($name = false) {
  180. global $gettext_php_domain, $gettext_php_loaded;
  181. if ($name != false && $gettext_php_domain != $name) {
  182. $gettext_php_domain = $name;
  183. $gettext_php_loaded = false;
  184. }
  185. return $gettext_php_domain;
  186. }
  187. ?>