gettext.php 5.5 KB

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