Potential gettext emulation speedup for languages without .po files (like
[squirrelmail.git] / functions / gettext.php
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)
6 * Possible use in other PHP scripts? The only SM-specific thing is
7 * $sm_language, I think
8 *
9 * Very special thanks to Konstantin Riabitsev for letting me use a
10 * server that didn't already have gettext on it!
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,
18 $gettext_php_translateStrings, $gettext_php_loaded_language;
19
20 if (! isset($gettext_php_loaded)) {
21 $gettext_php_loaded = false;
22 session_register('gettext_php_loaded');
23 }
24 if (! isset($gettext_php_domain)) {
25 $gettext_php_domain = '';
26 session_register('gettext_php_domain');
27 }
28 if (! isset($gettext_php_dir)) {
29 $gettext_php_dir = '';
30 session_register('gettext_php_dir');
31 }
32 if (! isset($gettext_php_translateStrings)) {
33 $gettext_php_translateStrings = array();
34 session_register('gettext_php_translateStrings');
35 }
36 if (! isset($gettext_php_loaded_language)) {
37 $gettext_php_loaded_language = '';
38 session_register('gettext_php_loaded_language');
39 }
40
41 function gettext_php_load_strings() {
42 global $squirrelmail_language, $gettext_php_translateStrings,
43 $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
44 $gettext_php_loaded_language;
45
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.
49
50 $gettext_php_translateStrings = array();
51
52 $filename = $gettext_php_dir;
53 if (substr($filename, -1) != '/')
54 $filename .= '/';
55 $filename .= $squirrelmail_language . '/LC_MESSAGES/' .
56 $gettext_php_domain . '.po';
57
58 $file = @fopen($filename, 'r');
59 if ($file === false) {
60 // Uh-ho -- we can't load the file. Just fake it. :-)
61 // This is also for English, which doesn't use translations
62 $gettext_php_loaded = true;
63 $gettext_php_loaded_language = $squirrelmail_language;
64 return;
65 }
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
78 // msgid ""
79 // "string string "
80 // "string string"
81 $key = '';
82 $line = trim(fgets($file, 4096));
83 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
84 $key .= $match[1];
85 $line = trim(fgets($file, 4096));
86 }
87 $SkipRead = true;
88 } else {
89 // msgid "string string"
90 $key = $match[1];
91 }
92 } elseif (ereg('^msgstr "(.*)"$', $line, $match)) {
93 if ($match[1] == '') {
94 // Potential multi-line
95 // msgstr ""
96 // "string string "
97 // "string string"
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 }
104 $SkipRead = true;
105 } else {
106 // msgstr "string string"
107 $gettext_php_translateStrings[$key] = $match[1];
108 }
109 $gettext_php_translateStrings[$key] =
110 stripslashes($gettext_php_translateStrings[$key]);
111 $key = '';
112 }
113 }
114 fclose($file);
115
116 $gettext_php_loaded = true;
117 $gettext_php_loaded_language = $squirrelmail_language;
118 }
119
120 function _($str) {
121 global $gettext_php_loaded, $gettext_php_translateStrings,
122 $squirrelmail_language, $gettext_php_loaded_language;
123
124 if (! $gettext_php_loaded ||
125 $gettext_php_loaded_language != $squirrelmail_language)
126 gettext_php_load_strings();
127
128 // Try finding the exact string
129 if (isset($gettext_php_translateStrings[$str]))
130 return $gettext_php_translateStrings[$str];
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) {
138 similar_text($str, $k, $newPercent);
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 }
151
152 // Remember this so we don't need to search again
153 $gettext_php_translateStrings[$str] = $str;
154 return $str;
155 }
156
157 function bindtextdomain($name, $dir) {
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 }
168
169 return $dir;
170 }
171
172 function textdomain($name = false) {
173 global $gettext_php_domain, $gettext_php_loaded;
174
175 if ($name != false && $gettext_php_domain != $name) {
176 $gettext_php_domain = $name;
177 $gettext_php_loaded = false;
178 }
179 return $gettext_php_domain;
180 }
181