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