gettext.php 6.8 KB

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