gettext.php 6.2 KB

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