gettext.php 6.2 KB

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