6 * Copyright (c) 1999-2002 The SquirrelMail Project 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;
53 * $squirrelmail_language gives 'en' for English, 'de' for German,
54 * etc. I didn't wanna use getenv or similar, but you easily could
55 * change my code to do that.
58 $gettext_php_translateStrings = array();
60 $gettext_php_short_circuit = false; /* initialization */
62 $filename = $gettext_php_dir;
63 if (substr($filename, -1) != '/')
65 $filename .= $squirrelmail_language . '/LC_MESSAGES/' .
66 $gettext_php_domain . '.po';
68 $file = @fopen
($filename, 'r');
70 /* Uh-ho -- we can't load the file. Just fake it. :-)
71 This is also for English, which doesn't use translations */
72 $gettext_php_loaded = true;
73 $gettext_php_loaded_language = $squirrelmail_language;
74 /* Avoid fuzzy matching when we didn't load strings */
75 $gettext_php_short_circuit = true;
81 while (! feof($file)) {
83 $line = trim(fgets($file, 4096));
88 if (ereg('^msgid "(.*)"$', $line, $match)) {
89 if ($match[1] == '') {
91 * Potential multi-line
97 $line = trim(fgets($file, 4096));
98 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
100 $line = trim(fgets($file, 4096));
104 /* msgid "string string" */
107 } elseif (ereg('^msgstr "(.*)"$', $line, $match)) {
108 if ($match[1] == '') {
110 * Potential multi-line
115 $gettext_php_translateStrings[$key] = '';
116 $line = trim(fgets($file, 4096));
117 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
118 $gettext_php_translateStrings[$key] .= $match[1];
119 $line = trim(fgets($file, 4096));
123 /* msgstr "string string" */
124 $gettext_php_translateStrings[$key] = $match[1];
126 $gettext_php_translateStrings[$key] =
127 stripslashes($gettext_php_translateStrings[$key]);
128 /* If there is no translation, just use the untranslated string */
129 if ($gettext_php_translateStrings[$key] == '') {
130 $gettext_php_translateStrings[$key] = $key;
137 $gettext_php_loaded = true;
138 $gettext_php_loaded_language = $squirrelmail_language;
142 global $gettext_php_loaded, $gettext_php_translateStrings,
143 $squirrelmail_language, $gettext_php_loaded_language,
144 $gettext_php_short_circuit;
146 if (! $gettext_php_loaded ||
147 $gettext_php_loaded_language != $squirrelmail_language) {
148 gettext_php_load_strings();
151 /* Try finding the exact string */
152 if (isset($gettext_php_translateStrings[$str])) {
153 return $gettext_php_translateStrings[$str];
156 /* See if we should short-circuit */
157 if ($gettext_php_short_circuit) {
158 $gettext_php_translateStrings[$str] = $str;
162 /* Look for a string that is very close to the one we want
163 Very computationally expensive */
167 foreach ($gettext_php_translateStrings as $k => $v) {
168 similar_text($str, $k, $newPercent);
169 if ($newPercent > $oldPercent) {
171 $oldPercent = $newPercent;
174 /* Require 80% match or better
175 Adjust to suit your needs */
176 if ($oldPercent > 80) {
177 /* Remember this so we don't need to search again */
178 $gettext_php_translateStrings[$str] = $oldStr;
182 /* Remember this so we don't need to search again */
183 $gettext_php_translateStrings[$str] = $str;
187 function bindtextdomain($name, $dir) {
188 global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded;
190 if ($gettext_php_domain != $name) {
191 $gettext_php_domain = $name;
192 $gettext_php_loaded = false;
194 if ($gettext_php_dir != $dir) {
195 $gettext_php_dir = $dir;
196 $gettext_php_loaded = false;
202 function textdomain($name = false) {
203 global $gettext_php_domain, $gettext_php_loaded;
205 if ($name != false && $gettext_php_domain != $name) {
206 $gettext_php_domain = $name;
207 $gettext_php_loaded = false;
209 return $gettext_php_domain;