6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
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
15 * @link http://www.php.net/gettext Original php gettext manual
18 * @package squirrelmail
21 /** Almost everything requires global.php... */
22 require_once(SM_PATH
. 'functions/global.php');
24 global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
25 $gettext_php_translateStrings, $gettext_php_loaded_language,
26 $gettext_php_short_circuit;
28 if (! isset($gettext_php_loaded)) {
29 $gettext_php_loaded = false;
30 sqsession_register($gettext_php_loaded, 'gettext_php_loaded');
32 if (! isset($gettext_php_domain)) {
33 $gettext_php_domain = '';
34 sqsession_register($gettext_php_domain, 'gettext_php_domain');
36 if (! isset($gettext_php_dir)) {
37 $gettext_php_dir = '';
38 sqsession_register($gettext_php_dir, 'gettext_php_dir');
40 if (! isset($gettext_php_translateStrings)) {
41 $gettext_php_translateStrings = array();
42 sqsession_register($gettext_php_translateStrings, 'gettext_php_translateStrings');
44 if (! isset($gettext_php_loaded_language)) {
45 $gettext_php_loaded_language = '';
46 sqsession_register($gettext_php_loaded_language, 'gettext_php_loaded_language');
48 if (! isset($gettext_php_short_circuit)) {
49 $gettext_php_short_circuit = false;
50 sqsession_register($gettext_php_short_circuit, 'gettext_php_short_circuit');
54 * Converts .po file into array and stores it in session.
56 * Used internally by _($str) function
58 * @internal function is used internally by functions/gettext.php code
60 function gettext_php_load_strings() {
61 global $squirrelmail_language, $gettext_php_translateStrings,
62 $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
63 $gettext_php_loaded_language, $gettext_php_short_circuit;
66 * $squirrelmail_language gives 'en' for English, 'de' for German,
67 * etc. I didn't wanna use getenv or similar, but you easily could
68 * change my code to do that.
71 $gettext_php_translateStrings = array();
73 $gettext_php_short_circuit = false; /* initialization */
75 $filename = $gettext_php_dir;
76 if (substr($filename, -1) != '/')
78 $filename .= $squirrelmail_language . '/LC_MESSAGES/' .
79 $gettext_php_domain . '.po';
81 $file = @fopen
($filename, 'r');
83 /* Uh-ho -- we can't load the file. Just fake it. :-)
84 This is also for English, which doesn't use translations */
85 $gettext_php_loaded = true;
86 $gettext_php_loaded_language = $squirrelmail_language;
87 /* Avoid fuzzy matching when we didn't load strings */
88 $gettext_php_short_circuit = true;
94 while (! feof($file)) {
96 $line = trim(fgets($file, 4096));
101 if (ereg('^msgid "(.*)"$', $line, $match)) {
102 if ($match[1] == '') {
104 * Potential multi-line
110 $line = trim(fgets($file, 4096));
111 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
113 $line = trim(fgets($file, 4096));
117 /* msgid "string string" */
120 } elseif (ereg('^msgstr "(.*)"$', $line, $match)) {
121 if ($match[1] == '') {
123 * Potential multi-line
128 $gettext_php_translateStrings[$key] = '';
129 $line = trim(fgets($file, 4096));
130 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
131 $gettext_php_translateStrings[$key] .= $match[1];
132 $line = trim(fgets($file, 4096));
136 /* msgstr "string string" */
137 $gettext_php_translateStrings[$key] = $match[1];
139 $gettext_php_translateStrings[$key] =
140 stripslashes($gettext_php_translateStrings[$key]);
141 /* If there is no translation, just use the untranslated string */
142 if ($gettext_php_translateStrings[$key] == '') {
143 $gettext_php_translateStrings[$key] = $key;
150 $gettext_php_loaded = true;
151 $gettext_php_loaded_language = $squirrelmail_language;
155 * Alternative php gettext function (short form)
157 * @link http://www.php.net/function.gettext
159 * @param string $str English string
160 * @return string translated string
163 global $gettext_php_loaded, $gettext_php_translateStrings,
164 $squirrelmail_language, $gettext_php_loaded_language,
165 $gettext_php_short_circuit;
167 if (! $gettext_php_loaded ||
168 $gettext_php_loaded_language != $squirrelmail_language) {
169 gettext_php_load_strings();
172 /* Try finding the exact string */
173 if (isset($gettext_php_translateStrings[$str])) {
174 return $gettext_php_translateStrings[$str];
177 /* See if we should short-circuit */
178 if ($gettext_php_short_circuit) {
179 $gettext_php_translateStrings[$str] = $str;
183 /* Look for a string that is very close to the one we want
184 Very computationally expensive */
188 foreach ($gettext_php_translateStrings as $k => $v) {
189 similar_text($str, $k, $newPercent);
190 if ($newPercent > $oldPercent) {
192 $oldPercent = $newPercent;
195 /* Require 80% match or better
196 Adjust to suit your needs */
197 if ($oldPercent > 80) {
198 /* Remember this so we don't need to search again */
199 $gettext_php_translateStrings[$str] = $oldStr;
203 /* Remember this so we don't need to search again */
204 $gettext_php_translateStrings[$str] = $str;
209 * Alternative php bindtextdomain function
211 * Sets path to directory containing domain translations
213 * @link http://www.php.net/function.bindtextdomain
214 * @param string $name gettext domain name
215 * @param string $dir directory that contains all translations
216 * @return string path to translation directory
218 function bindtextdomain($name, $dir) {
219 global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded;
221 if ($gettext_php_domain != $name) {
222 $gettext_php_domain = $name;
223 $gettext_php_loaded = false;
225 if ($gettext_php_dir != $dir) {
226 $gettext_php_dir = $dir;
227 $gettext_php_loaded = false;
234 * Alternative php textdomain function
236 * Sets default domain name
238 * @link http://www.php.net/function.textdomain
239 * @param string $name gettext domain name
240 * @return string gettext domain name
242 function textdomain($name = false) {
243 global $gettext_php_domain, $gettext_php_loaded;
245 if ($name != false && $gettext_php_domain != $name) {
246 $gettext_php_domain = $name;
247 $gettext_php_loaded = false;
250 return $gettext_php_domain;