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;
20 if (! isset($gettext_php_loaded)) {
21 $gettext_php_loaded = false;
22 session_register('gettext_php_loaded');
24 if (! isset($gettext_php_domain)) {
25 $gettext_php_domain = '';
26 session_register('gettext_php_domain');
28 if (! isset($gettext_php_dir)) {
29 $gettext_php_dir = '';
30 session_register('gettext_php_dir');
32 if (! isset($gettext_php_translateStrings)) {
33 $gettext_php_translateStrings = array();
34 session_register('gettext_php_translateStrings');
36 if (! isset($gettext_php_loaded_language)) {
37 $gettext_php_loaded_language = '';
38 session_register('gettext_php_loaded_language');
41 function gettext_php_load_strings() {
42 global $squirrelmail_language, $gettext_php_translateStrings,
43 $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
44 $gettext_php_loaded_language;
46 // $squirrelmail_language gives 'en' for English, 'de' for German,
47 // etc. I didn't wanna use getenv or similar, but you easily could
48 // change my code to do that.
50 $gettext_php_translateStrings = array();
52 $filename = $gettext_php_dir;
53 if (substr($filename, -1) != '/')
55 $filename .= $squirrelmail_language . '/LC_MESSAGES/' .
56 $gettext_php_domain . '.po';
58 $file = @fopen
($filename, 'r');
59 if ($file === false) {
60 // Uh-ho -- we can't load the file. Just fake it. :-)
61 // This is also for English, which doesn't use translations
62 $gettext_php_loaded = true;
63 $gettext_php_loaded_language = $squirrelmail_language;
69 while (! feof($file)) {
71 $line = trim(fgets($file, 4096));
75 if (ereg('^msgid "(.*)"$', $line, $match)) {
76 if ($match[1] == '') {
77 // Potential multi-line
82 $line = trim(fgets($file, 4096));
83 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
85 $line = trim(fgets($file, 4096));
89 // msgid "string string"
92 } elseif (ereg('^msgstr "(.*)"$', $line, $match)) {
93 if ($match[1] == '') {
94 // Potential multi-line
98 $gettext_php_translateStrings[$key] = '';
99 $line = trim(fgets($file, 4096));
100 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
101 $gettext_php_translateStrings[$key] .= $match[1];
102 $line = trim(fgets($file, 4096));
106 // msgstr "string string"
107 $gettext_php_translateStrings[$key] = $match[1];
109 $gettext_php_translateStrings[$key] =
110 stripslashes($gettext_php_translateStrings[$key]);
116 $gettext_php_loaded = true;
117 $gettext_php_loaded_language = $squirrelmail_language;
121 global $gettext_php_loaded, $gettext_php_translateStrings,
122 $squirrelmail_language, $gettext_php_loaded_language;
124 if (! $gettext_php_loaded ||
125 $gettext_php_loaded_language != $squirrelmail_language)
126 gettext_php_load_strings();
128 // Try finding the exact string
129 if (isset($gettext_php_translateStrings[$str]))
130 return $gettext_php_translateStrings[$str];
132 // Look for a string that is very close to the one we want
133 // Very computationally expensive
137 foreach ($gettext_php_translateStrings as $k => $v) {
138 similar_text($str, $k, $newPercent);
139 if ($newPercent > $oldPercent) {
141 $oldPercent = $newPercent;
144 // Require 80% match or better
145 // Adjust to suit your needs
146 if ($oldPercent > 80) {
147 // Remember this so we don't need to search again
148 $gettext_php_translateStrings[$str] = $oldStr;
152 // Remember this so we don't need to search again
153 $gettext_php_translateStrings[$str] = $str;
157 function bindtextdomain($name, $dir) {
158 global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded;
160 if ($gettext_php_domain != $name) {
161 $gettext_php_domain = $name;
162 $gettext_php_loaded = false;
164 if ($gettext_php_dir != $dir) {
165 $gettext_php_dir = $dir;
166 $gettext_php_loaded = false;
172 function textdomain($name = false) {
173 global $gettext_php_domain, $gettext_php_loaded;
175 if ($name != false && $gettext_php_domain != $name) {
176 $gettext_php_domain = $name;
177 $gettext_php_loaded = false;
179 return $gettext_php_domain;