75edf2d0ddc53838189b1405e02849e1c26b6593
[squirrelmail.git] / functions / gettext.php
1 <?php
2
3 /**
4 * gettext.php
5 *
6 * Copyright (c) 1999-2002 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 * $Id$
16 */
17
18 global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
19 $gettext_php_translateStrings, $gettext_php_loaded_language,
20 $gettext_php_short_circuit;
21
22 if (! isset($gettext_php_loaded)) {
23 $gettext_php_loaded = false;
24 session_register('gettext_php_loaded');
25 }
26 if (! isset($gettext_php_domain)) {
27 $gettext_php_domain = '';
28 session_register('gettext_php_domain');
29 }
30 if (! isset($gettext_php_dir)) {
31 $gettext_php_dir = '';
32 session_register('gettext_php_dir');
33 }
34 if (! isset($gettext_php_translateStrings)) {
35 $gettext_php_translateStrings = array();
36 session_register('gettext_php_translateStrings');
37 }
38 if (! isset($gettext_php_loaded_language)) {
39 $gettext_php_loaded_language = '';
40 session_register('gettext_php_loaded_language');
41 }
42 if (! isset($gettext_php_short_circuit)) {
43 $gettext_php_short_circuit = false;
44 session_register('gettext_php_short_circuit');
45 }
46
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;
51
52 /*
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.
56 */
57
58 $gettext_php_translateStrings = array();
59
60 $gettext_php_short_circuit = false; /* initialization */
61
62 $filename = $gettext_php_dir;
63 if (substr($filename, -1) != '/')
64 $filename .= '/';
65 $filename .= $squirrelmail_language . '/LC_MESSAGES/' .
66 $gettext_php_domain . '.po';
67
68 $file = @fopen($filename, 'r');
69 if ($file == false) {
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;
76 return;
77 }
78
79 $key = '';
80 $SkipRead = false;
81 while (! feof($file)) {
82 if (! $SkipRead)
83 $line = trim(fgets($file, 4096));
84 else
85 $SkipRead = false;
86
87 if (ereg('^msgid "(.*)"$', $line, $match)) {
88 if ($match[1] == '') {
89 /*
90 * Potential multi-line
91 * msgid ""
92 * "string string "
93 * "string string"
94 */
95 $key = '';
96 $line = trim(fgets($file, 4096));
97 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
98 $key .= $match[1];
99 $line = trim(fgets($file, 4096));
100 }
101 $SkipRead = true;
102 } else {
103 /* msgid "string string" */
104 $key = $match[1];
105 }
106 } elseif (ereg('^msgstr "(.*)"$', $line, $match)) {
107 if ($match[1] == '') {
108 /*
109 * Potential multi-line
110 * msgstr ""
111 * "string string "
112 * "string string"
113 */
114 $gettext_php_translateStrings[$key] = '';
115 $line = trim(fgets($file, 4096));
116 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
117 $gettext_php_translateStrings[$key] .= $match[1];
118 $line = trim(fgets($file, 4096));
119 }
120 $SkipRead = true;
121 } else {
122 /* msgstr "string string" */
123 $gettext_php_translateStrings[$key] = $match[1];
124 }
125 $gettext_php_translateStrings[$key] =
126 stripslashes($gettext_php_translateStrings[$key]);
127 /* If there is no translation, just use the untranslated string */
128 if ($gettext_php_translateStrings[$key] == '') {
129 $gettext_php_translateStrings[$key] = $key;
130 }
131 $key = '';
132 }
133 }
134 fclose($file);
135
136 $gettext_php_loaded = true;
137 $gettext_php_loaded_language = $squirrelmail_language;
138 }
139
140 function _($str) {
141 global $gettext_php_loaded, $gettext_php_translateStrings,
142 $squirrelmail_language, $gettext_php_loaded_language,
143 $gettext_php_short_circuit;
144
145 if (! $gettext_php_loaded ||
146 $gettext_php_loaded_language != $squirrelmail_language) {
147 gettext_php_load_strings();
148 }
149
150 /* Try finding the exact string */
151 if (isset($gettext_php_translateStrings[$str])) {
152 return $gettext_php_translateStrings[$str];
153 }
154
155 /* See if we should short-circuit */
156 if ($gettext_php_short_circuit) {
157 $gettext_php_translateStrings[$str] = $str;
158 return $str;
159 }
160
161 /* Look for a string that is very close to the one we want
162 Very computationally expensive */
163 $oldPercent = 0;
164 $oldStr = '';
165 $newPercent = 0;
166 foreach ($gettext_php_translateStrings as $k => $v) {
167 similar_text($str, $k, $newPercent);
168 if ($newPercent > $oldPercent) {
169 $oldStr = $v;
170 $oldPercent = $newPercent;
171 }
172 }
173 /* Require 80% match or better
174 Adjust to suit your needs */
175 if ($oldPercent > 80) {
176 /* Remember this so we don't need to search again */
177 $gettext_php_translateStrings[$str] = $oldStr;
178 return $oldStr;
179 }
180
181 /* Remember this so we don't need to search again */
182 $gettext_php_translateStrings[$str] = $str;
183 return $str;
184 }
185
186 function bindtextdomain($name, $dir) {
187 global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded;
188
189 if ($gettext_php_domain != $name) {
190 $gettext_php_domain = $name;
191 $gettext_php_loaded = false;
192 }
193 if ($gettext_php_dir != $dir) {
194 $gettext_php_dir = $dir;
195 $gettext_php_loaded = false;
196 }
197
198 return $dir;
199 }
200
201 function textdomain($name = false) {
202 global $gettext_php_domain, $gettext_php_loaded;
203
204 if ($name != false && $gettext_php_domain != $name) {
205 $gettext_php_domain = $name;
206 $gettext_php_loaded = false;
207 }
208 return $gettext_php_domain;
209 }
210
211 ?>