/gui/tools/webmail/functions/gettext.php

https://github.com/BenBE/ispCP · PHP · 251 lines · 145 code · 24 blank · 82 comment · 40 complexity · bc3578fb7842a44557809570551e151d MD5 · raw file

  1. <?php
  2. /**
  3. * SquirrelMail internal gettext functions
  4. *
  5. * Alternate to the system's built-in gettext. Relies on .po files (can't read
  6. * .mo easily). Uses the session for caching (speed increase). Possible use in
  7. * other PHP scripts? The only SM-specific thing is $sm_language, I think.
  8. *
  9. * @link http://www.php.net/gettext Original php gettext manual
  10. * @copyright 1999-2011 The SquirrelMail Project Team
  11. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  12. * @version $Id: gettext.php 14084 2011-01-06 02:44:03Z pdontthink $
  13. * @since 1.1.2
  14. * @package squirrelmail
  15. * @subpackage i18n
  16. */
  17. /** Almost everything requires global.php... */
  18. require_once(SM_PATH . 'functions/global.php');
  19. global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
  20. $gettext_php_translateStrings, $gettext_php_loaded_language,
  21. $gettext_php_short_circuit;
  22. if (! isset($gettext_php_loaded)) {
  23. $gettext_php_loaded = false;
  24. sqsession_register($gettext_php_loaded, 'gettext_php_loaded');
  25. }
  26. if (! isset($gettext_php_domain)) {
  27. $gettext_php_domain = '';
  28. sqsession_register($gettext_php_domain, 'gettext_php_domain');
  29. }
  30. if (! isset($gettext_php_dir)) {
  31. $gettext_php_dir = '';
  32. sqsession_register($gettext_php_dir, 'gettext_php_dir');
  33. }
  34. if (! isset($gettext_php_translateStrings)) {
  35. $gettext_php_translateStrings = array();
  36. sqsession_register($gettext_php_translateStrings, 'gettext_php_translateStrings');
  37. }
  38. if (! isset($gettext_php_loaded_language)) {
  39. $gettext_php_loaded_language = '';
  40. sqsession_register($gettext_php_loaded_language, 'gettext_php_loaded_language');
  41. }
  42. if (! isset($gettext_php_short_circuit)) {
  43. $gettext_php_short_circuit = false;
  44. sqsession_register($gettext_php_short_circuit, 'gettext_php_short_circuit');
  45. }
  46. /**
  47. * Converts .po file into array and stores it in session.
  48. *
  49. * Used internally by _($str) function
  50. *
  51. * @internal function is used internally by functions/gettext.php code
  52. */
  53. function gettext_php_load_strings() {
  54. global $squirrelmail_language, $gettext_php_translateStrings,
  55. $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
  56. $gettext_php_loaded_language, $gettext_php_short_circuit;
  57. /*
  58. * $squirrelmail_language gives 'en' for English, 'de' for German,
  59. * etc. I didn't wanna use getenv or similar, but you easily could
  60. * change my code to do that.
  61. */
  62. $gettext_php_translateStrings = array();
  63. $gettext_php_short_circuit = false; /* initialization */
  64. $filename = $gettext_php_dir;
  65. if (substr($filename, -1) != '/')
  66. $filename .= '/';
  67. $filename .= $squirrelmail_language . '/LC_MESSAGES/' .
  68. $gettext_php_domain . '.po';
  69. $file = @fopen($filename, 'r');
  70. if ($file == false) {
  71. /* Uh-ho -- we can't load the file. Just fake it. :-)
  72. This is also for English, which doesn't use translations */
  73. $gettext_php_loaded = true;
  74. $gettext_php_loaded_language = $squirrelmail_language;
  75. /* Avoid fuzzy matching when we didn't load strings */
  76. $gettext_php_short_circuit = true;
  77. return;
  78. }
  79. $key = '';
  80. $SkipRead = false;
  81. while (! feof($file)) {
  82. if (! $SkipRead) {
  83. $line = trim(fgets($file, 4096));
  84. } else {
  85. $SkipRead = false;
  86. }
  87. if (preg_match('/^msgid "(.*)"$/', $line, $match)) {
  88. if ($match[1] == '') {
  89. /*
  90. * Potential multi-line
  91. * msgid ""
  92. * "string string "
  93. * "string string"
  94. */
  95. $key = '';
  96. $line = trim(fgets($file, 4096));
  97. while (preg_match('/^[ ]*"(.*)"[ ]*$/', $line, $match)) {
  98. $key .= $match[1];
  99. $line = trim(fgets($file, 4096));
  100. }
  101. $SkipRead = true;
  102. } else {
  103. /* msgid "string string" */
  104. $key = $match[1];
  105. }
  106. } elseif (preg_match('/^msgstr "(.*)"$/', $line, $match)) {
  107. if ($match[1] == '') {
  108. /*
  109. * Potential multi-line
  110. * msgstr ""
  111. * "string string "
  112. * "string string"
  113. */
  114. $gettext_php_translateStrings[$key] = '';
  115. $line = trim(fgets($file, 4096));
  116. while (preg_match('/^[ ]*"(.*)"[ ]*$/', $line, $match)) {
  117. $gettext_php_translateStrings[$key] .= $match[1];
  118. $line = trim(fgets($file, 4096));
  119. }
  120. $SkipRead = true;
  121. } else {
  122. /* msgstr "string string" */
  123. $gettext_php_translateStrings[$key] = $match[1];
  124. }
  125. $gettext_php_translateStrings[$key] =
  126. stripslashes($gettext_php_translateStrings[$key]);
  127. /* If there is no translation, just use the untranslated string */
  128. if ($gettext_php_translateStrings[$key] == '') {
  129. $gettext_php_translateStrings[$key] = $key;
  130. }
  131. $key = '';
  132. }
  133. }
  134. fclose($file);
  135. $gettext_php_loaded = true;
  136. $gettext_php_loaded_language = $squirrelmail_language;
  137. }
  138. /**
  139. * Alternative php gettext function (short form)
  140. *
  141. * @link http://www.php.net/function.gettext
  142. *
  143. * @param string $str English string
  144. * @return string translated string
  145. */
  146. function _($str) {
  147. global $gettext_php_loaded, $gettext_php_translateStrings,
  148. $squirrelmail_language, $gettext_php_loaded_language,
  149. $gettext_php_short_circuit;
  150. if (! $gettext_php_loaded ||
  151. $gettext_php_loaded_language != $squirrelmail_language) {
  152. gettext_php_load_strings();
  153. }
  154. /* Try finding the exact string */
  155. if (isset($gettext_php_translateStrings[$str])) {
  156. return $gettext_php_translateStrings[$str];
  157. }
  158. /* See if we should short-circuit */
  159. if ($gettext_php_short_circuit) {
  160. $gettext_php_translateStrings[$str] = $str;
  161. return $str;
  162. }
  163. /* don't do fuzzy matching for strings with sprintf() formating */
  164. if (! preg_match('/\%[\%bcdeufFosxX]/',$str)) {
  165. /* Look for a string that is very close to the one we want
  166. * Very computationally expensive */
  167. $oldPercent = 0;
  168. $oldStr = '';
  169. $newPercent = 0;
  170. foreach ($gettext_php_translateStrings as $k => $v) {
  171. similar_text($str, $k, $newPercent);
  172. if ($newPercent > $oldPercent) {
  173. $oldStr = $v;
  174. $oldPercent = $newPercent;
  175. }
  176. }
  177. /* Require 80% match or better
  178. * Adjust to suit your needs */
  179. if ($oldPercent > 80) {
  180. /* Remember this so we don't need to search again */
  181. $gettext_php_translateStrings[$str] = $oldStr;
  182. return $oldStr;
  183. }
  184. }
  185. /* Remember this so we don't need to search again */
  186. $gettext_php_translateStrings[$str] = $str;
  187. return $str;
  188. }
  189. /**
  190. * Alternative php bindtextdomain function
  191. *
  192. * Sets path to directory containing domain translations
  193. *
  194. * @link http://www.php.net/function.bindtextdomain
  195. * @param string $name gettext domain name
  196. * @param string $dir directory that contains all translations
  197. * @return string path to translation directory
  198. */
  199. function bindtextdomain($name, $dir) {
  200. global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded;
  201. if ($gettext_php_domain != $name) {
  202. $gettext_php_domain = $name;
  203. $gettext_php_loaded = false;
  204. }
  205. if ($gettext_php_dir != $dir) {
  206. $gettext_php_dir = $dir;
  207. $gettext_php_loaded = false;
  208. }
  209. return $dir;
  210. }
  211. /**
  212. * Alternative php textdomain function
  213. *
  214. * Sets default domain name
  215. *
  216. * @link http://www.php.net/function.textdomain
  217. * @param string $name gettext domain name
  218. * @return string gettext domain name
  219. */
  220. function textdomain($name = false) {
  221. global $gettext_php_domain, $gettext_php_loaded;
  222. if ($name != false && $gettext_php_domain != $name) {
  223. $gettext_php_domain = $name;
  224. $gettext_php_loaded = false;
  225. }
  226. return $gettext_php_domain;
  227. }