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