File Formatting
[squirrelmail.git] / functions / gettext.php
1 <?PHP
2
3 /**
4 * gettext.php
5 *
6 * Copyright (c) 1999-2001 The SquirrelMail Development 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 /*****************************************************************/
19 /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
20 /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
21 /*** + Base level indent should begin at left margin, as ***/
22 /*** the global definition below. ***/
23 /*** + All identation should consist of four space blocks ***/
24 /*** + Tab characters are evil. ***/
25 /*** + all comments should use "slash-star ... star-slash" ***/
26 /*** style -- no pound characters, no slash-slash style ***/
27 /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
28 /*** ALWAYS USE { AND } CHARACTERS!!! ***/
29 /*** + Please use ' instead of ", when possible. Note " ***/
30 /*** should always be used in _( ) function calls. ***/
31 /*** Thank you for your help making the SM code more readable. ***/
32 /*****************************************************************/
33
34 global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
35 $gettext_php_translateStrings, $gettext_php_loaded_language,
36 $gettext_php_short_circuit;
37
38 if (! isset($gettext_php_loaded)) {
39 $gettext_php_loaded = false;
40 session_register('gettext_php_loaded');
41 }
42 if (! isset($gettext_php_domain)) {
43 $gettext_php_domain = '';
44 session_register('gettext_php_domain');
45 }
46 if (! isset($gettext_php_dir)) {
47 $gettext_php_dir = '';
48 session_register('gettext_php_dir');
49 }
50 if (! isset($gettext_php_translateStrings)) {
51 $gettext_php_translateStrings = array();
52 session_register('gettext_php_translateStrings');
53 }
54 if (! isset($gettext_php_loaded_language)) {
55 $gettext_php_loaded_language = '';
56 session_register('gettext_php_loaded_language');
57 }
58 if (! isset($gettext_php_short_circuit)) {
59 $gettext_php_short_circuit = false;
60 session_register('gettext_php_short_circuit');
61 }
62
63 function gettext_php_load_strings() {
64 global $squirrelmail_language, $gettext_php_translateStrings,
65 $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
66 $gettext_php_loaded_language, $gettext_php_short_circuit;
67
68 // $squirrelmail_language gives 'en' for English, 'de' for German,
69 // etc. I didn't wanna use getenv or similar, but you easily could
70 // change my code to do that.
71
72 $gettext_php_translateStrings = array();
73
74 $gettext_php_short_circuit = false; // initialization
75
76 $filename = $gettext_php_dir;
77 if (substr($filename, -1) != '/')
78 $filename .= '/';
79 $filename .= $squirrelmail_language . '/LC_MESSAGES/' .
80 $gettext_php_domain . '.po';
81
82 $file = @fopen($filename, 'r');
83 if ($file == false) {
84 // Uh-ho -- we can't load the file. Just fake it. :-)
85 // This is also for English, which doesn't use translations
86 $gettext_php_loaded = true;
87 $gettext_php_loaded_language = $squirrelmail_language;
88 $gettext_php_short_circuit = true; // Avoid fuzzy matching when we
89 // didn't load strings
90 return;
91 }
92
93 $key = '';
94 $SkipRead = false;
95 while (! feof($file)) {
96 if (! $SkipRead)
97 $line = trim(fgets($file, 4096));
98 else
99 $SkipRead = false;
100
101 if (ereg('^msgid "(.*)"$', $line, $match)) {
102 if ($match[1] == '') {
103 // Potential multi-line
104 // msgid ""
105 // "string string "
106 // "string string"
107 $key = '';
108 $line = trim(fgets($file, 4096));
109 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
110 $key .= $match[1];
111 $line = trim(fgets($file, 4096));
112 }
113 $SkipRead = true;
114 } else {
115 // msgid "string string"
116 $key = $match[1];
117 }
118 } elseif (ereg('^msgstr "(.*)"$', $line, $match)) {
119 if ($match[1] == '') {
120 // Potential multi-line
121 // msgstr ""
122 // "string string "
123 // "string string"
124 $gettext_php_translateStrings[$key] = '';
125 $line = trim(fgets($file, 4096));
126 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
127 $gettext_php_translateStrings[$key] .= $match[1];
128 $line = trim(fgets($file, 4096));
129 }
130 $SkipRead = true;
131 } else {
132 // msgstr "string string"
133 $gettext_php_translateStrings[$key] = $match[1];
134 }
135 $gettext_php_translateStrings[$key] =
136 stripslashes($gettext_php_translateStrings[$key]);
137 $key = '';
138 }
139 }
140 fclose($file);
141
142 $gettext_php_loaded = true;
143 $gettext_php_loaded_language = $squirrelmail_language;
144 }
145
146 function _($str) {
147 global $gettext_php_loaded, $gettext_php_translateStrings,
148 $squirrelmail_language, $gettext_php_loaded_language,
149 $gettext_php_short_circuit;
150
151 if (! $gettext_php_loaded ||
152 $gettext_php_loaded_language != $squirrelmail_language)
153 gettext_php_load_strings();
154
155 // Try finding the exact string
156 if (isset($gettext_php_translateStrings[$str]))
157 return $gettext_php_translateStrings[$str];
158
159 // See if we should short-circuit
160 if ($gettext_php_short_circuit) {
161 $gettext_php_translateStrings[$str] = $str;
162 return $str;
163 }
164
165 // Look for a string that is very close to the one we want
166 // Very computationally expensive
167 $oldPercent = 0;
168 $oldStr = '';
169 $newPercent = 0;
170 foreach ($gettext_php_translateStrings as $k => $v) {
171 similar_text($str, $k, $newPercent);
172 if ($newPercent > $oldPercent) {
173 $oldStr = $v;
174 $oldPercent = $newPercent;
175 }
176 }
177 // Require 80% match or better
178 // Adjust to suit your needs
179 if ($oldPercent > 80) {
180 // Remember this so we don't need to search again
181 $gettext_php_translateStrings[$str] = $oldStr;
182 return $oldStr;
183 }
184
185 // Remember this so we don't need to search again
186 $gettext_php_translateStrings[$str] = $str;
187 return $str;
188 }
189
190 function bindtextdomain($name, $dir) {
191 global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded;
192
193 if ($gettext_php_domain != $name) {
194 $gettext_php_domain = $name;
195 $gettext_php_loaded = false;
196 }
197 if ($gettext_php_dir != $dir) {
198 $gettext_php_dir = $dir;
199 $gettext_php_loaded = false;
200 }
201
202 return $dir;
203 }
204
205 function textdomain($name = false) {
206 global $gettext_php_domain, $gettext_php_loaded;
207
208 if ($name != false && $gettext_php_domain != $name) {
209 $gettext_php_domain = $name;
210 $gettext_php_loaded = false;
211 }
212 return $gettext_php_domain;
213 }
214