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