6 * Copyright (c) 1999-2003 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
16 * @package squirrelmail
19 /** Almost everything requires global.php... */
20 require_once(SM_PATH
. 'functions/global.php');
22 global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
23 $gettext_php_translateStrings, $gettext_php_loaded_language,
24 $gettext_php_short_circuit;
26 if (! isset($gettext_php_loaded)) {
27 $gettext_php_loaded = false;
28 sqsession_register($gettext_php_loaded, 'gettext_php_loaded');
30 if (! isset($gettext_php_domain)) {
31 $gettext_php_domain = '';
32 sqsession_register($gettext_php_domain, 'gettext_php_domain');
34 if (! isset($gettext_php_dir)) {
35 $gettext_php_dir = '';
36 sqsession_register($gettext_php_dir, 'gettext_php_dir');
38 if (! isset($gettext_php_translateStrings)) {
39 $gettext_php_translateStrings = array();
40 sqsession_register($gettext_php_translateStrings, 'gettext_php_translateStrings');
42 if (! isset($gettext_php_loaded_language)) {
43 $gettext_php_loaded_language = '';
44 sqsession_register($gettext_php_loaded_language, 'gettext_php_loaded_language');
46 if (! isset($gettext_php_short_circuit)) {
47 $gettext_php_short_circuit = false;
48 sqsession_register($gettext_php_short_circuit, 'gettext_php_short_circuit');
51 function gettext_php_load_strings() {
52 global $squirrelmail_language, $gettext_php_translateStrings,
53 $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
54 $gettext_php_loaded_language, $gettext_php_short_circuit;
57 * $squirrelmail_language gives 'en' for English, 'de' for German,
58 * etc. I didn't wanna use getenv or similar, but you easily could
59 * change my code to do that.
62 $gettext_php_translateStrings = array();
64 $gettext_php_short_circuit = false; /* initialization */
66 $filename = $gettext_php_dir;
67 if (substr($filename, -1) != '/')
69 $filename .= $squirrelmail_language . '/LC_MESSAGES/' .
70 $gettext_php_domain . '.po';
72 $file = @fopen
($filename, 'r');
74 /* Uh-ho -- we can't load the file. Just fake it. :-)
75 This is also for English, which doesn't use translations */
76 $gettext_php_loaded = true;
77 $gettext_php_loaded_language = $squirrelmail_language;
78 /* Avoid fuzzy matching when we didn't load strings */
79 $gettext_php_short_circuit = true;
85 while (! feof($file)) {
87 $line = trim(fgets($file, 4096));
92 if (ereg('^msgid "(.*)"$', $line, $match)) {
93 if ($match[1] == '') {
95 * Potential multi-line
101 $line = trim(fgets($file, 4096));
102 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
104 $line = trim(fgets($file, 4096));
108 /* msgid "string string" */
111 } elseif (ereg('^msgstr "(.*)"$', $line, $match)) {
112 if ($match[1] == '') {
114 * Potential multi-line
119 $gettext_php_translateStrings[$key] = '';
120 $line = trim(fgets($file, 4096));
121 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
122 $gettext_php_translateStrings[$key] .= $match[1];
123 $line = trim(fgets($file, 4096));
127 /* msgstr "string string" */
128 $gettext_php_translateStrings[$key] = $match[1];
130 $gettext_php_translateStrings[$key] =
131 stripslashes($gettext_php_translateStrings[$key]);
132 /* If there is no translation, just use the untranslated string */
133 if ($gettext_php_translateStrings[$key] == '') {
134 $gettext_php_translateStrings[$key] = $key;
141 $gettext_php_loaded = true;
142 $gettext_php_loaded_language = $squirrelmail_language;
146 global $gettext_php_loaded, $gettext_php_translateStrings,
147 $squirrelmail_language, $gettext_php_loaded_language,
148 $gettext_php_short_circuit;
150 if (! $gettext_php_loaded ||
151 $gettext_php_loaded_language != $squirrelmail_language) {
152 gettext_php_load_strings();
155 /* Try finding the exact string */
156 if (isset($gettext_php_translateStrings[$str])) {
157 return $gettext_php_translateStrings[$str];
160 /* See if we should short-circuit */
161 if ($gettext_php_short_circuit) {
162 $gettext_php_translateStrings[$str] = $str;
166 /* Look for a string that is very close to the one we want
167 Very computationally expensive */
171 foreach ($gettext_php_translateStrings as $k => $v) {
172 similar_text($str, $k, $newPercent);
173 if ($newPercent > $oldPercent) {
175 $oldPercent = $newPercent;
178 /* Require 80% match or better
179 Adjust to suit your needs */
180 if ($oldPercent > 80) {
181 /* Remember this so we don't need to search again */
182 $gettext_php_translateStrings[$str] = $oldStr;
186 /* Remember this so we don't need to search again */
187 $gettext_php_translateStrings[$str] = $str;
191 function bindtextdomain($name, $dir) {
192 global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded;
194 if ($gettext_php_domain != $name) {
195 $gettext_php_domain = $name;
196 $gettext_php_loaded = false;
198 if ($gettext_php_dir != $dir) {
199 $gettext_php_dir = $dir;
200 $gettext_php_loaded = false;
206 function textdomain($name = false) {
207 global $gettext_php_domain, $gettext_php_loaded;
209 if ($name != false && $gettext_php_domain != $name) {
210 $gettext_php_domain = $name;
211 $gettext_php_loaded = false;
213 return $gettext_php_domain;