gettext.php 6.1 KB

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