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