gettext.php 6.8 KB

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