String bug fix
[squirrelmail.git] / functions / gettext.php
CommitLineData
bb48d62e 1<?PHP
2
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)
829a33b6 6 * Possible use in other PHP scripts? The only SM-specific thing is
7 * $sm_language, I think
55c10992 8 *
9 * Very special thanks to Konstantin Riabitsev for letting me use a
10 * server that didn't already have gettext on it!
bb48d62e 11 */
12
13 if (defined('gettext_php'))
14 return;
15 define('gettext_php', true);
16
17 global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
55c10992 18 $gettext_php_translateStrings, $gettext_php_loaded_language;
bb48d62e 19
20 if (! isset($gettext_php_loaded)) {
21 $gettext_php_loaded = false;
22 session_register('gettext_php_loaded');
23 }
829a33b6 24 if (! isset($gettext_php_domain)) {
25 $gettext_php_domain = '';
55c10992 26 session_register('gettext_php_domain');
829a33b6 27 }
28 if (! isset($gettext_php_dir)) {
29 $gettext_php_dir = '';
55c10992 30 session_register('gettext_php_dir');
829a33b6 31 }
32 if (! isset($gettext_php_translateStrings)) {
33 $gettext_php_translateStrings = array();
34 session_register('gettext_php_translateStrings');
35 }
55c10992 36 if (! isset($gettext_php_loaded_language)) {
37 $gettext_php_loaded_language = '';
38 session_register('gettext_php_loaded_language');
39 }
829a33b6 40
bb48d62e 41 function gettext_php_load_strings() {
55c10992 42 global $squirrelmail_language, $gettext_php_translateStrings,
43 $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
44 $gettext_php_loaded_language;
bb48d62e 45
55c10992 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.
bb48d62e 49
50 $gettext_php_translateStrings = array();
55c10992 51
bb48d62e 52 $filename = $gettext_php_dir;
53 if (substr($filename, -1) != '/')
54 $filename .= '/';
55c10992 55 $filename .= $squirrelmail_language . '/LC_MESSAGES/' .
56 $gettext_php_domain . '.po';
bb48d62e 57
55c10992 58 $file = @fopen($filename, 'r');
59 if ($file === false) {
a68fb657 60 // Uh-ho -- we can't load the file. Just fake it. :-)
61 // This is also for English, which doesn't use translations
55c10992 62 $gettext_php_loaded = true;
a68fb657 63 $gettext_php_loaded_language = $squirrelmail_language;
bb48d62e 64 return;
55c10992 65 }
bb48d62e 66
67 $key = '';
68 $SkipRead = false;
69 while (! feof($file)) {
70 if (! $SkipRead)
71 $line = trim(fgets($file, 4096));
72 else
73 $SkipRead = false;
74
75 if (ereg('^msgid "(.*)"$', $line, $match)) {
76 if ($match[1] == '') {
77 // Potential multi-line
829a33b6 78 // msgid ""
79 // "string string "
80 // "string string"
bb48d62e 81 $key = '';
82 $line = trim(fgets($file, 4096));
83 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
84 $key .= $match[1];
85 $line = trim(fgets($file, 4096));
86 }
55c10992 87 $SkipRead = true;
bb48d62e 88 } else {
829a33b6 89 // msgid "string string"
bb48d62e 90 $key = $match[1];
91 }
92 } elseif (ereg('^msgstr "(.*)"$', $line, $match)) {
93 if ($match[1] == '') {
94 // Potential multi-line
829a33b6 95 // msgstr ""
96 // "string string "
97 // "string string"
bb48d62e 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));
103 }
55c10992 104 $SkipRead = true;
bb48d62e 105 } else {
829a33b6 106 // msgstr "string string"
bb48d62e 107 $gettext_php_translateStrings[$key] = $match[1];
108 }
55c10992 109 $gettext_php_translateStrings[$key] =
110 stripslashes($gettext_php_translateStrings[$key]);
bb48d62e 111 $key = '';
112 }
113 }
114 fclose($file);
55c10992 115
bb48d62e 116 $gettext_php_loaded = true;
55c10992 117 $gettext_php_loaded_language = $squirrelmail_language;
bb48d62e 118 }
55c10992 119
bb48d62e 120 function _($str) {
55c10992 121 global $gettext_php_loaded, $gettext_php_translateStrings,
122 $squirrelmail_language, $gettext_php_loaded_language;
bb48d62e 123
55c10992 124 if (! $gettext_php_loaded ||
125 $gettext_php_loaded_language != $squirrelmail_language)
bb48d62e 126 gettext_php_load_strings();
55c10992 127
128 // Try finding the exact string
bb48d62e 129 if (isset($gettext_php_translateStrings[$str]))
130 return $gettext_php_translateStrings[$str];
55c10992 131
132 // Look for a string that is very close to the one we want
133 // Very computationally expensive
134 $oldPercent = 0;
135 $oldStr = '';
136 $newPercent = 0;
137 foreach ($gettext_php_translateStrings as $k => $v) {
0fb42fee 138 similar_text($str, $k, $newPercent);
55c10992 139 if ($newPercent > $oldPercent) {
140 $oldStr = $v;
141 $oldPercent = $newPercent;
142 }
143 }
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;
149 return $oldStr;
150 }
bb48d62e 151
55c10992 152 // Remember this so we don't need to search again
153 $gettext_php_translateStrings[$str] = $str;
bb48d62e 154 return $str;
155 }
156
157 function bindtextdomain($name, $dir) {
55c10992 158 global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded;
159
160 if ($gettext_php_domain != $name) {
161 $gettext_php_domain = $name;
162 $gettext_php_loaded = false;
163 }
164 if ($gettext_php_dir != $dir) {
165 $gettext_php_dir = $dir;
166 $gettext_php_loaded = false;
167 }
bb48d62e 168
169 return $dir;
170 }
171
172 function textdomain($name = false) {
55c10992 173 global $gettext_php_domain, $gettext_php_loaded;
174
175 if ($name != false && $gettext_php_domain != $name) {
bb48d62e 176 $gettext_php_domain = $name;
177 $gettext_php_loaded = false;
178 }
179 return $gettext_php_domain;
180 }
181