Getting ready for 1.2.0 release.
[squirrelmail.git] / functions / gettext.php
CommitLineData
bb48d62e 1<?PHP
2
35586184 3/**
4 * gettext.php
5 *
15e6162e 6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
35586184 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 */
2ba13803 17
35586184 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
34global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
35 $gettext_php_translateStrings, $gettext_php_loaded_language,
36 $gettext_php_short_circuit;
bb48d62e 37
38 if (! isset($gettext_php_loaded)) {
39 $gettext_php_loaded = false;
40 session_register('gettext_php_loaded');
41 }
829a33b6 42 if (! isset($gettext_php_domain)) {
43 $gettext_php_domain = '';
55c10992 44 session_register('gettext_php_domain');
829a33b6 45 }
46 if (! isset($gettext_php_dir)) {
47 $gettext_php_dir = '';
55c10992 48 session_register('gettext_php_dir');
829a33b6 49 }
50 if (! isset($gettext_php_translateStrings)) {
51 $gettext_php_translateStrings = array();
52 session_register('gettext_php_translateStrings');
53 }
55c10992 54 if (! isset($gettext_php_loaded_language)) {
55 $gettext_php_loaded_language = '';
56 session_register('gettext_php_loaded_language');
57 }
be921d7a 58 if (! isset($gettext_php_short_circuit)) {
59 $gettext_php_short_circuit = false;
60 session_register('gettext_php_short_circuit');
61 }
829a33b6 62
bb48d62e 63 function gettext_php_load_strings() {
55c10992 64 global $squirrelmail_language, $gettext_php_translateStrings,
65 $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
48581a4d 66 $gettext_php_loaded_language, $gettext_php_short_circuit;
bb48d62e 67
55c10992 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.
bb48d62e 71
72 $gettext_php_translateStrings = array();
48581a4d 73
74 $gettext_php_short_circuit = false; // initialization
55c10992 75
bb48d62e 76 $filename = $gettext_php_dir;
77 if (substr($filename, -1) != '/')
78 $filename .= '/';
55c10992 79 $filename .= $squirrelmail_language . '/LC_MESSAGES/' .
80 $gettext_php_domain . '.po';
bb48d62e 81
55c10992 82 $file = @fopen($filename, 'r');
b771345a 83 if ($file == false) {
a68fb657 84 // Uh-ho -- we can't load the file. Just fake it. :-)
85 // This is also for English, which doesn't use translations
55c10992 86 $gettext_php_loaded = true;
a68fb657 87 $gettext_php_loaded_language = $squirrelmail_language;
48581a4d 88 $gettext_php_short_circuit = true; // Avoid fuzzy matching when we
89 // didn't load strings
bb48d62e 90 return;
55c10992 91 }
bb48d62e 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
829a33b6 104 // msgid ""
105 // "string string "
106 // "string string"
bb48d62e 107 $key = '';
108 $line = trim(fgets($file, 4096));
109 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
110 $key .= $match[1];
111 $line = trim(fgets($file, 4096));
112 }
55c10992 113 $SkipRead = true;
bb48d62e 114 } else {
829a33b6 115 // msgid "string string"
bb48d62e 116 $key = $match[1];
117 }
118 } elseif (ereg('^msgstr "(.*)"$', $line, $match)) {
119 if ($match[1] == '') {
120 // Potential multi-line
829a33b6 121 // msgstr ""
122 // "string string "
123 // "string string"
bb48d62e 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 }
55c10992 130 $SkipRead = true;
bb48d62e 131 } else {
829a33b6 132 // msgstr "string string"
bb48d62e 133 $gettext_php_translateStrings[$key] = $match[1];
134 }
55c10992 135 $gettext_php_translateStrings[$key] =
136 stripslashes($gettext_php_translateStrings[$key]);
1e45dcf6 137 // If there is no translation, just use the untranslated string
138 if ($gettext_php_translateStrings[$key] == '')
139 $gettext_php_translateStrings[$key] = $key;
bb48d62e 140 $key = '';
141 }
142 }
143 fclose($file);
55c10992 144
bb48d62e 145 $gettext_php_loaded = true;
55c10992 146 $gettext_php_loaded_language = $squirrelmail_language;
bb48d62e 147 }
55c10992 148
bb48d62e 149 function _($str) {
55c10992 150 global $gettext_php_loaded, $gettext_php_translateStrings,
48581a4d 151 $squirrelmail_language, $gettext_php_loaded_language,
152 $gettext_php_short_circuit;
bb48d62e 153
55c10992 154 if (! $gettext_php_loaded ||
155 $gettext_php_loaded_language != $squirrelmail_language)
bb48d62e 156 gettext_php_load_strings();
55c10992 157
158 // Try finding the exact string
bb48d62e 159 if (isset($gettext_php_translateStrings[$str]))
160 return $gettext_php_translateStrings[$str];
48581a4d 161
162 // See if we should short-circuit
163 if ($gettext_php_short_circuit) {
164 $gettext_php_translateStrings[$str] = $str;
165 return $str;
166 }
167
55c10992 168 // Look for a string that is very close to the one we want
169 // Very computationally expensive
170 $oldPercent = 0;
171 $oldStr = '';
172 $newPercent = 0;
173 foreach ($gettext_php_translateStrings as $k => $v) {
0fb42fee 174 similar_text($str, $k, $newPercent);
55c10992 175 if ($newPercent > $oldPercent) {
176 $oldStr = $v;
177 $oldPercent = $newPercent;
178 }
179 }
180 // Require 80% match or better
181 // Adjust to suit your needs
182 if ($oldPercent > 80) {
183 // Remember this so we don't need to search again
184 $gettext_php_translateStrings[$str] = $oldStr;
185 return $oldStr;
186 }
bb48d62e 187
55c10992 188 // Remember this so we don't need to search again
189 $gettext_php_translateStrings[$str] = $str;
bb48d62e 190 return $str;
191 }
192
193 function bindtextdomain($name, $dir) {
55c10992 194 global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded;
195
196 if ($gettext_php_domain != $name) {
197 $gettext_php_domain = $name;
198 $gettext_php_loaded = false;
199 }
200 if ($gettext_php_dir != $dir) {
201 $gettext_php_dir = $dir;
202 $gettext_php_loaded = false;
203 }
bb48d62e 204
205 return $dir;
206 }
207
208 function textdomain($name = false) {
55c10992 209 global $gettext_php_domain, $gettext_php_loaded;
210
211 if ($name != false && $gettext_php_domain != $name) {
bb48d62e 212 $gettext_php_domain = $name;
213 $gettext_php_loaded = false;
214 }
215 return $gettext_php_domain;
216 }
217