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');
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;
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.
51 $gettext_php_translateStrings = array();
53 $gettext_php_short_circuit = false; // initialization
55 $filename = $gettext_php_dir;
56 if (substr($filename, -1) != '/')
58 $filename .= $squirrelmail_language . '/LC_MESSAGES/' .
59 $gettext_php_domain . '.po';
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
74 while (! feof($file)) {
76 $line = trim(fgets($file, 4096));
80 if (ereg('^msgid "(.*)"$', $line, $match)) {
81 if ($match[1] == '') {
82 // Potential multi-line
87 $line = trim(fgets($file, 4096));
88 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
90 $line = trim(fgets($file, 4096));
94 // msgid "string string"
97 } elseif (ereg('^msgstr "(.*)"$', $line, $match)) {
98 if ($match[1] == '') {
99 // Potential multi-line
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));
111 // msgstr "string string"
112 $gettext_php_translateStrings[$key] = $match[1];
114 $gettext_php_translateStrings[$key] =
115 stripslashes($gettext_php_translateStrings[$key]);
121 $gettext_php_loaded = true;
122 $gettext_php_loaded_language = $squirrelmail_language;
126 global $gettext_php_loaded, $gettext_php_translateStrings,
127 $squirrelmail_language, $gettext_php_loaded_language,
128 $gettext_php_short_circuit;
130 if (! $gettext_php_loaded ||
131 $gettext_php_loaded_language != $squirrelmail_language)
132 gettext_php_load_strings();
134 // Try finding the exact string
135 if (isset($gettext_php_translateStrings[$str]))
136 return $gettext_php_translateStrings[$str];
138 // See if we should short-circuit
139 if ($gettext_php_short_circuit) {
140 $gettext_php_translateStrings[$str] = $str;
144 // Look for a string that is very close to the one we want
145 // Very computationally expensive
149 foreach ($gettext_php_translateStrings as $k => $v) {
150 similar_text($str, $k, $newPercent);
151 if ($newPercent > $oldPercent) {
153 $oldPercent = $newPercent;
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;
164 // Remember this so we don't need to search again
165 $gettext_php_translateStrings[$str] = $str;
169 function bindtextdomain($name, $dir) {
170 global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded;
172 if ($gettext_php_domain != $name) {
173 $gettext_php_domain = $name;
174 $gettext_php_loaded = false;
176 if ($gettext_php_dir != $dir) {
177 $gettext_php_dir = $dir;
178 $gettext_php_loaded = false;
184 function textdomain($name = false) {
185 global $gettext_php_domain, $gettext_php_loaded;
187 if ($name != false && $gettext_php_domain != $name) {
188 $gettext_php_domain = $name;
189 $gettext_php_loaded = false;
191 return $gettext_php_domain;