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