** -> *
[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 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 // $squirrelmail_language gives 'en' for English, 'de' for German,
53 // etc. I didn't wanna use getenv or similar, but you easily could
54 // change my code to do that.
55
56 $gettext_php_translateStrings = array();
57
58 $gettext_php_short_circuit = false; // initialization
59
60 $filename = $gettext_php_dir;
61 if (substr($filename, -1) != '/')
62 $filename .= '/';
63 $filename .= $squirrelmail_language . '/LC_MESSAGES/' .
64 $gettext_php_domain . '.po';
65
66 $file = @fopen($filename, 'r');
67 if ($file == false) {
68 // Uh-ho -- we can't load the file. Just fake it. :-)
69 // This is also for English, which doesn't use translations
70 $gettext_php_loaded = true;
71 $gettext_php_loaded_language = $squirrelmail_language;
72 $gettext_php_short_circuit = true; // Avoid fuzzy matching when we
73 // didn't load strings
74 return;
75 }
76
77 $key = '';
78 $SkipRead = false;
79 while (! feof($file)) {
80 if (! $SkipRead)
81 $line = trim(fgets($file, 4096));
82 else
83 $SkipRead = false;
84
85 if (ereg('^msgid "(.*)"$', $line, $match)) {
86 if ($match[1] == '') {
87 // Potential multi-line
88 // msgid ""
89 // "string string "
90 // "string string"
91 $key = '';
92 $line = trim(fgets($file, 4096));
93 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
94 $key .= $match[1];
95 $line = trim(fgets($file, 4096));
96 }
97 $SkipRead = true;
98 } else {
99 // msgid "string string"
100 $key = $match[1];
101 }
102 } elseif (ereg('^msgstr "(.*)"$', $line, $match)) {
103 if ($match[1] == '') {
104 // Potential multi-line
105 // msgstr ""
106 // "string string "
107 // "string string"
108 $gettext_php_translateStrings[$key] = '';
109 $line = trim(fgets($file, 4096));
110 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
111 $gettext_php_translateStrings[$key] .= $match[1];
112 $line = trim(fgets($file, 4096));
113 }
114 $SkipRead = true;
115 } else {
116 // msgstr "string string"
117 $gettext_php_translateStrings[$key] = $match[1];
118 }
119 $gettext_php_translateStrings[$key] =
120 stripslashes($gettext_php_translateStrings[$key]);
121 $key = '';
122 }
123 }
124 fclose($file);
125
126 $gettext_php_loaded = true;
127 $gettext_php_loaded_language = $squirrelmail_language;
128 }
129
130 function _($str) {
131 global $gettext_php_loaded, $gettext_php_translateStrings,
132 $squirrelmail_language, $gettext_php_loaded_language,
133 $gettext_php_short_circuit;
134
135 if (! $gettext_php_loaded ||
136 $gettext_php_loaded_language != $squirrelmail_language)
137 gettext_php_load_strings();
138
139 // Try finding the exact string
140 if (isset($gettext_php_translateStrings[$str]))
141 return $gettext_php_translateStrings[$str];
142
143 // See if we should short-circuit
144 if ($gettext_php_short_circuit) {
145 $gettext_php_translateStrings[$str] = $str;
146 return $str;
147 }
148
149 // Look for a string that is very close to the one we want
150 // Very computationally expensive
151 $oldPercent = 0;
152 $oldStr = '';
153 $newPercent = 0;
154 foreach ($gettext_php_translateStrings as $k => $v) {
155 similar_text($str, $k, $newPercent);
156 if ($newPercent > $oldPercent) {
157 $oldStr = $v;
158 $oldPercent = $newPercent;
159 }
160 }
161 // Require 80% match or better
162 // Adjust to suit your needs
163 if ($oldPercent > 80) {
164 // Remember this so we don't need to search again
165 $gettext_php_translateStrings[$str] = $oldStr;
166 return $oldStr;
167 }
168
169 // Remember this so we don't need to search again
170 $gettext_php_translateStrings[$str] = $str;
171 return $str;
172 }
173
174 function bindtextdomain($name, $dir) {
175 global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded;
176
177 if ($gettext_php_domain != $name) {
178 $gettext_php_domain = $name;
179 $gettext_php_loaded = false;
180 }
181 if ($gettext_php_dir != $dir) {
182 $gettext_php_dir = $dir;
183 $gettext_php_loaded = false;
184 }
185
186 return $dir;
187 }
188
189 function textdomain($name = false) {
190 global $gettext_php_domain, $gettext_php_loaded;
191
192 if ($name != false && $gettext_php_domain != $name) {
193 $gettext_php_domain = $name;
194 $gettext_php_loaded = false;
195 }
196 return $gettext_php_domain;
197 }
198