adding two hooks that allow integrating third party address book backends
[squirrelmail.git] / functions / gettext.php
1 <?php
2
3 /**
4 * gettext.php
5 *
6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
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
14 *
15 * @link http://www.php.net/gettext Original php gettext manual
16 *
17 * $Id$
18 * @package squirrelmail
19 */
20
21 /** Almost everything requires global.php... */
22 require_once(SM_PATH . 'functions/global.php');
23
24 global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
25 $gettext_php_translateStrings, $gettext_php_loaded_language,
26 $gettext_php_short_circuit;
27
28 if (! isset($gettext_php_loaded)) {
29 $gettext_php_loaded = false;
30 sqsession_register($gettext_php_loaded, 'gettext_php_loaded');
31 }
32 if (! isset($gettext_php_domain)) {
33 $gettext_php_domain = '';
34 sqsession_register($gettext_php_domain, 'gettext_php_domain');
35 }
36 if (! isset($gettext_php_dir)) {
37 $gettext_php_dir = '';
38 sqsession_register($gettext_php_dir, 'gettext_php_dir');
39 }
40 if (! isset($gettext_php_translateStrings)) {
41 $gettext_php_translateStrings = array();
42 sqsession_register($gettext_php_translateStrings, 'gettext_php_translateStrings');
43 }
44 if (! isset($gettext_php_loaded_language)) {
45 $gettext_php_loaded_language = '';
46 sqsession_register($gettext_php_loaded_language, 'gettext_php_loaded_language');
47 }
48 if (! isset($gettext_php_short_circuit)) {
49 $gettext_php_short_circuit = false;
50 sqsession_register($gettext_php_short_circuit, 'gettext_php_short_circuit');
51 }
52
53 /**
54 * Converts .po file into array and stores it in session.
55 *
56 * Used internally by _($str) function
57 *
58 * @internal function is used internally by functions/gettext.php code
59 */
60 function gettext_php_load_strings() {
61 global $squirrelmail_language, $gettext_php_translateStrings,
62 $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
63 $gettext_php_loaded_language, $gettext_php_short_circuit;
64
65 /*
66 * $squirrelmail_language gives 'en' for English, 'de' for German,
67 * etc. I didn't wanna use getenv or similar, but you easily could
68 * change my code to do that.
69 */
70
71 $gettext_php_translateStrings = array();
72
73 $gettext_php_short_circuit = false; /* initialization */
74
75 $filename = $gettext_php_dir;
76 if (substr($filename, -1) != '/')
77 $filename .= '/';
78 $filename .= $squirrelmail_language . '/LC_MESSAGES/' .
79 $gettext_php_domain . '.po';
80
81 $file = @fopen($filename, 'r');
82 if ($file == false) {
83 /* Uh-ho -- we can't load the file. Just fake it. :-)
84 This is also for English, which doesn't use translations */
85 $gettext_php_loaded = true;
86 $gettext_php_loaded_language = $squirrelmail_language;
87 /* Avoid fuzzy matching when we didn't load strings */
88 $gettext_php_short_circuit = true;
89 return;
90 }
91
92 $key = '';
93 $SkipRead = false;
94 while (! feof($file)) {
95 if (! $SkipRead) {
96 $line = trim(fgets($file, 4096));
97 } else {
98 $SkipRead = false;
99 }
100
101 if (ereg('^msgid "(.*)"$', $line, $match)) {
102 if ($match[1] == '') {
103 /*
104 * Potential multi-line
105 * msgid ""
106 * "string string "
107 * "string string"
108 */
109 $key = '';
110 $line = trim(fgets($file, 4096));
111 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
112 $key .= $match[1];
113 $line = trim(fgets($file, 4096));
114 }
115 $SkipRead = true;
116 } else {
117 /* msgid "string string" */
118 $key = $match[1];
119 }
120 } elseif (ereg('^msgstr "(.*)"$', $line, $match)) {
121 if ($match[1] == '') {
122 /*
123 * Potential multi-line
124 * msgstr ""
125 * "string string "
126 * "string string"
127 */
128 $gettext_php_translateStrings[$key] = '';
129 $line = trim(fgets($file, 4096));
130 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
131 $gettext_php_translateStrings[$key] .= $match[1];
132 $line = trim(fgets($file, 4096));
133 }
134 $SkipRead = true;
135 } else {
136 /* msgstr "string string" */
137 $gettext_php_translateStrings[$key] = $match[1];
138 }
139 $gettext_php_translateStrings[$key] =
140 stripslashes($gettext_php_translateStrings[$key]);
141 /* If there is no translation, just use the untranslated string */
142 if ($gettext_php_translateStrings[$key] == '') {
143 $gettext_php_translateStrings[$key] = $key;
144 }
145 $key = '';
146 }
147 }
148 fclose($file);
149
150 $gettext_php_loaded = true;
151 $gettext_php_loaded_language = $squirrelmail_language;
152 }
153
154 /**
155 * Alternative php gettext function (short form)
156 *
157 * @link http://www.php.net/function.gettext
158 *
159 * @param string $str English string
160 * @return string translated string
161 */
162 function _($str) {
163 global $gettext_php_loaded, $gettext_php_translateStrings,
164 $squirrelmail_language, $gettext_php_loaded_language,
165 $gettext_php_short_circuit;
166
167 if (! $gettext_php_loaded ||
168 $gettext_php_loaded_language != $squirrelmail_language) {
169 gettext_php_load_strings();
170 }
171
172 /* Try finding the exact string */
173 if (isset($gettext_php_translateStrings[$str])) {
174 return $gettext_php_translateStrings[$str];
175 }
176
177 /* See if we should short-circuit */
178 if ($gettext_php_short_circuit) {
179 $gettext_php_translateStrings[$str] = $str;
180 return $str;
181 }
182
183 /* Look for a string that is very close to the one we want
184 Very computationally expensive */
185 $oldPercent = 0;
186 $oldStr = '';
187 $newPercent = 0;
188 foreach ($gettext_php_translateStrings as $k => $v) {
189 similar_text($str, $k, $newPercent);
190 if ($newPercent > $oldPercent) {
191 $oldStr = $v;
192 $oldPercent = $newPercent;
193 }
194 }
195 /* Require 80% match or better
196 Adjust to suit your needs */
197 if ($oldPercent > 80) {
198 /* Remember this so we don't need to search again */
199 $gettext_php_translateStrings[$str] = $oldStr;
200 return $oldStr;
201 }
202
203 /* Remember this so we don't need to search again */
204 $gettext_php_translateStrings[$str] = $str;
205 return $str;
206 }
207
208 /**
209 * Alternative php bindtextdomain function
210 *
211 * Sets path to directory containing domain translations
212 *
213 * @link http://www.php.net/function.bindtextdomain
214 * @param string $name gettext domain name
215 * @param string $dir directory that contains all translations
216 * @return string path to translation directory
217 */
218 function bindtextdomain($name, $dir) {
219 global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded;
220
221 if ($gettext_php_domain != $name) {
222 $gettext_php_domain = $name;
223 $gettext_php_loaded = false;
224 }
225 if ($gettext_php_dir != $dir) {
226 $gettext_php_dir = $dir;
227 $gettext_php_loaded = false;
228 }
229
230 return $dir;
231 }
232
233 /**
234 * Alternative php textdomain function
235 *
236 * Sets default domain name
237 *
238 * @link http://www.php.net/function.textdomain
239 * @param string $name gettext domain name
240 * @return string gettext domain name
241 */
242 function textdomain($name = false) {
243 global $gettext_php_domain, $gettext_php_loaded;
244
245 if ($name != false && $gettext_php_domain != $name) {
246 $gettext_php_domain = $name;
247 $gettext_php_loaded = false;
248 }
249
250 return $gettext_php_domain;
251 }
252 ?>