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