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
9 * Very special thanks to Konstantin Riabitsev for letting me use a
10 * server that didn't already have gettext on it!
13 if (defined('gettext_php'))
15 define('gettext_php', true);
17 global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
18 $gettext_php_translateStrings, $gettext_php_loaded_language,
19 $gettext_php_short_circuit;
21 if (! isset($gettext_php_loaded)) {
22 $gettext_php_loaded = false;
23 session_register('gettext_php_loaded');
25 if (! isset($gettext_php_domain)) {
26 $gettext_php_domain = '';
27 session_register('gettext_php_domain');
29 if (! isset($gettext_php_dir)) {
30 $gettext_php_dir = '';
31 session_register('gettext_php_dir');
33 if (! isset($gettext_php_translateStrings)) {
34 $gettext_php_translateStrings = array();
35 session_register('gettext_php_translateStrings');
37 if (! isset($gettext_php_loaded_language)) {
38 $gettext_php_loaded_language = '';
39 session_register('gettext_php_loaded_language');
41 if (! isset($gettext_php_short_circuit)) {
42 $gettext_php_short_circuit = false;
43 session_register('gettext_php_short_circuit');
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;
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.
55 $gettext_php_translateStrings = array();
57 $gettext_php_short_circuit = false; // initialization
59 $filename = $gettext_php_dir;
60 if (substr($filename, -1) != '/')
62 $filename .= $squirrelmail_language . '/LC_MESSAGES/' .
63 $gettext_php_domain . '.po';
65 $file = @fopen
($filename, 'r');
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
78 while (! feof($file)) {
80 $line = trim(fgets($file, 4096));
84 if (ereg('^msgid "(.*)"$', $line, $match)) {
85 if ($match[1] == '') {
86 // Potential multi-line
91 $line = trim(fgets($file, 4096));
92 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
94 $line = trim(fgets($file, 4096));
98 // msgid "string string"
101 } elseif (ereg('^msgstr "(.*)"$', $line, $match)) {
102 if ($match[1] == '') {
103 // Potential multi-line
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));
115 // msgstr "string string"
116 $gettext_php_translateStrings[$key] = $match[1];
118 $gettext_php_translateStrings[$key] =
119 stripslashes($gettext_php_translateStrings[$key]);
125 $gettext_php_loaded = true;
126 $gettext_php_loaded_language = $squirrelmail_language;
130 global $gettext_php_loaded, $gettext_php_translateStrings,
131 $squirrelmail_language, $gettext_php_loaded_language,
132 $gettext_php_short_circuit;
134 if (! $gettext_php_loaded ||
135 $gettext_php_loaded_language != $squirrelmail_language)
136 gettext_php_load_strings();
138 // Try finding the exact string
139 if (isset($gettext_php_translateStrings[$str]))
140 return $gettext_php_translateStrings[$str];
142 // See if we should short-circuit
143 if ($gettext_php_short_circuit) {
144 $gettext_php_translateStrings[$str] = $str;
148 // Look for a string that is very close to the one we want
149 // Very computationally expensive
153 foreach ($gettext_php_translateStrings as $k => $v) {
154 similar_text($str, $k, $newPercent);
155 if ($newPercent > $oldPercent) {
157 $oldPercent = $newPercent;
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;
168 // Remember this so we don't need to search again
169 $gettext_php_translateStrings[$str] = $str;
173 function bindtextdomain($name, $dir) {
174 global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded;
176 if ($gettext_php_domain != $name) {
177 $gettext_php_domain = $name;
178 $gettext_php_loaded = false;
180 if ($gettext_php_dir != $dir) {
181 $gettext_php_dir = $dir;
182 $gettext_php_loaded = false;
188 function textdomain($name = false) {
189 global $gettext_php_domain, $gettext_php_loaded;
191 if ($name != false && $gettext_php_domain != $name) {
192 $gettext_php_domain = $name;
193 $gettext_php_loaded = false;
195 return $gettext_php_domain;