Some fixup:
[squirrelmail.git] / functions / gettext.php
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)
6 * Possible use in other PHP scripts? The only SM-specific thing is
7 * $sm_language, I think
8 *
9 * Very special thanks to Konstantin Riabitsev for letting me use a
10 * server that didn't already have gettext on it!
11 */
12
13 global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
14 $gettext_php_translateStrings, $gettext_php_loaded_language,
15 $gettext_php_short_circuit;
16
17 if (! isset($gettext_php_loaded)) {
18 $gettext_php_loaded = false;
19 session_register('gettext_php_loaded');
20 }
21 if (! isset($gettext_php_domain)) {
22 $gettext_php_domain = '';
23 session_register('gettext_php_domain');
24 }
25 if (! isset($gettext_php_dir)) {
26 $gettext_php_dir = '';
27 session_register('gettext_php_dir');
28 }
29 if (! isset($gettext_php_translateStrings)) {
30 $gettext_php_translateStrings = array();
31 session_register('gettext_php_translateStrings');
32 }
33 if (! isset($gettext_php_loaded_language)) {
34 $gettext_php_loaded_language = '';
35 session_register('gettext_php_loaded_language');
36 }
37 if (! isset($gettext_php_short_circuit)) {
38 $gettext_php_short_circuit = false;
39 session_register('gettext_php_short_circuit');
40 }
41
42 function gettext_php_load_strings() {
43 global $squirrelmail_language, $gettext_php_translateStrings,
44 $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
45 $gettext_php_loaded_language, $gettext_php_short_circuit;
46
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.
50
51 $gettext_php_translateStrings = array();
52
53 $gettext_php_short_circuit = false; // initialization
54
55 $filename = $gettext_php_dir;
56 if (substr($filename, -1) != '/')
57 $filename .= '/';
58 $filename .= $squirrelmail_language . '/LC_MESSAGES/' .
59 $gettext_php_domain . '.po';
60
61 $file = @fopen($filename, 'r');
62 if ($file == false) {
63 // Uh-ho -- we can't load the file. Just fake it. :-)
64 // This is also for English, which doesn't use translations
65 $gettext_php_loaded = true;
66 $gettext_php_loaded_language = $squirrelmail_language;
67 $gettext_php_short_circuit = true; // Avoid fuzzy matching when we
68 // didn't load strings
69 return;
70 }
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
83 // msgid ""
84 // "string string "
85 // "string string"
86 $key = '';
87 $line = trim(fgets($file, 4096));
88 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
89 $key .= $match[1];
90 $line = trim(fgets($file, 4096));
91 }
92 $SkipRead = true;
93 } else {
94 // msgid "string string"
95 $key = $match[1];
96 }
97 } elseif (ereg('^msgstr "(.*)"$', $line, $match)) {
98 if ($match[1] == '') {
99 // Potential multi-line
100 // msgstr ""
101 // "string string "
102 // "string string"
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 }
109 $SkipRead = true;
110 } else {
111 // msgstr "string string"
112 $gettext_php_translateStrings[$key] = $match[1];
113 }
114 $gettext_php_translateStrings[$key] =
115 stripslashes($gettext_php_translateStrings[$key]);
116 $key = '';
117 }
118 }
119 fclose($file);
120
121 $gettext_php_loaded = true;
122 $gettext_php_loaded_language = $squirrelmail_language;
123 }
124
125 function _($str) {
126 global $gettext_php_loaded, $gettext_php_translateStrings,
127 $squirrelmail_language, $gettext_php_loaded_language,
128 $gettext_php_short_circuit;
129
130 if (! $gettext_php_loaded ||
131 $gettext_php_loaded_language != $squirrelmail_language)
132 gettext_php_load_strings();
133
134 // Try finding the exact string
135 if (isset($gettext_php_translateStrings[$str]))
136 return $gettext_php_translateStrings[$str];
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
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) {
150 similar_text($str, $k, $newPercent);
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 }
163
164 // Remember this so we don't need to search again
165 $gettext_php_translateStrings[$str] = $str;
166 return $str;
167 }
168
169 function bindtextdomain($name, $dir) {
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 }
180
181 return $dir;
182 }
183
184 function textdomain($name = false) {
185 global $gettext_php_domain, $gettext_php_loaded;
186
187 if ($name != false && $gettext_php_domain != $name) {
188 $gettext_php_domain = $name;
189 $gettext_php_loaded = false;
190 }
191 return $gettext_php_domain;
192 }
193