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