6 ** Copyright (c) 1999-2001 The Squirrelmail Development Team
7 ** Licensed under the GNU GPL. For full terms see the file COPYING.
9 ** Alternate to the system's built-in gettext.
10 ** relies on .po files (can't read .mo easily).
11 ** Uses the session for caching (speed increase)
12 ** Possible use in other PHP scripts? The only SM-specific thing is
13 ** $sm_language, I think
18 global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
19 $gettext_php_translateStrings, $gettext_php_loaded_language,
20 $gettext_php_short_circuit;
22 if (! isset($gettext_php_loaded)) {
23 $gettext_php_loaded = false;
24 session_register('gettext_php_loaded');
26 if (! isset($gettext_php_domain)) {
27 $gettext_php_domain = '';
28 session_register('gettext_php_domain');
30 if (! isset($gettext_php_dir)) {
31 $gettext_php_dir = '';
32 session_register('gettext_php_dir');
34 if (! isset($gettext_php_translateStrings)) {
35 $gettext_php_translateStrings = array();
36 session_register('gettext_php_translateStrings');
38 if (! isset($gettext_php_loaded_language)) {
39 $gettext_php_loaded_language = '';
40 session_register('gettext_php_loaded_language');
42 if (! isset($gettext_php_short_circuit)) {
43 $gettext_php_short_circuit = false;
44 session_register('gettext_php_short_circuit');
47 function gettext_php_load_strings() {
48 global $squirrelmail_language, $gettext_php_translateStrings,
49 $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
50 $gettext_php_loaded_language, $gettext_php_short_circuit;
52 // $squirrelmail_language gives 'en' for English, 'de' for German,
53 // etc. I didn't wanna use getenv or similar, but you easily could
54 // change my code to do that.
56 $gettext_php_translateStrings = array();
58 $gettext_php_short_circuit = false; // initialization
60 $filename = $gettext_php_dir;
61 if (substr($filename, -1) != '/')
63 $filename .= $squirrelmail_language . '/LC_MESSAGES/' .
64 $gettext_php_domain . '.po';
66 $file = @fopen
($filename, 'r');
68 // Uh-ho -- we can't load the file. Just fake it. :-)
69 // This is also for English, which doesn't use translations
70 $gettext_php_loaded = true;
71 $gettext_php_loaded_language = $squirrelmail_language;
72 $gettext_php_short_circuit = true; // Avoid fuzzy matching when we
73 // didn't load strings
79 while (! feof($file)) {
81 $line = trim(fgets($file, 4096));
85 if (ereg('^msgid "(.*)"$', $line, $match)) {
86 if ($match[1] == '') {
87 // Potential multi-line
92 $line = trim(fgets($file, 4096));
93 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
95 $line = trim(fgets($file, 4096));
99 // msgid "string string"
102 } elseif (ereg('^msgstr "(.*)"$', $line, $match)) {
103 if ($match[1] == '') {
104 // Potential multi-line
108 $gettext_php_translateStrings[$key] = '';
109 $line = trim(fgets($file, 4096));
110 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
111 $gettext_php_translateStrings[$key] .= $match[1];
112 $line = trim(fgets($file, 4096));
116 // msgstr "string string"
117 $gettext_php_translateStrings[$key] = $match[1];
119 $gettext_php_translateStrings[$key] =
120 stripslashes($gettext_php_translateStrings[$key]);
126 $gettext_php_loaded = true;
127 $gettext_php_loaded_language = $squirrelmail_language;
131 global $gettext_php_loaded, $gettext_php_translateStrings,
132 $squirrelmail_language, $gettext_php_loaded_language,
133 $gettext_php_short_circuit;
135 if (! $gettext_php_loaded ||
136 $gettext_php_loaded_language != $squirrelmail_language)
137 gettext_php_load_strings();
139 // Try finding the exact string
140 if (isset($gettext_php_translateStrings[$str]))
141 return $gettext_php_translateStrings[$str];
143 // See if we should short-circuit
144 if ($gettext_php_short_circuit) {
145 $gettext_php_translateStrings[$str] = $str;
149 // Look for a string that is very close to the one we want
150 // Very computationally expensive
154 foreach ($gettext_php_translateStrings as $k => $v) {
155 similar_text($str, $k, $newPercent);
156 if ($newPercent > $oldPercent) {
158 $oldPercent = $newPercent;
161 // Require 80% match or better
162 // Adjust to suit your needs
163 if ($oldPercent > 80) {
164 // Remember this so we don't need to search again
165 $gettext_php_translateStrings[$str] = $oldStr;
169 // Remember this so we don't need to search again
170 $gettext_php_translateStrings[$str] = $str;
174 function bindtextdomain($name, $dir) {
175 global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded;
177 if ($gettext_php_domain != $name) {
178 $gettext_php_domain = $name;
179 $gettext_php_loaded = false;
181 if ($gettext_php_dir != $dir) {
182 $gettext_php_dir = $dir;
183 $gettext_php_loaded = false;
189 function textdomain($name = false) {
190 global $gettext_php_domain, $gettext_php_loaded;
192 if ($name != false && $gettext_php_domain != $name) {
193 $gettext_php_domain = $name;
194 $gettext_php_loaded = false;
196 return $gettext_php_domain;