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