3 * SquirrelMail internal gettext functions
5 * Copyright (c) 1999-2004 The SquirrelMail Project Team
6 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 * Alternate to the system's built-in gettext.
9 * relies on .po files (can't read .mo easily).
10 * Uses the session for caching (speed increase)
11 * Possible use in other PHP scripts? The only SM-specific thing is
12 * $sm_language, I think
14 * @link http://www.php.net/gettext Original php gettext manual
16 * @package squirrelmail
20 /** Almost everything requires global.php... */
21 require_once(SM_PATH
. 'functions/global.php');
23 global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
24 $gettext_php_translateStrings, $gettext_php_loaded_language,
25 $gettext_php_short_circuit;
27 if (! isset($gettext_php_loaded)) {
28 $gettext_php_loaded = false;
29 sqsession_register($gettext_php_loaded, 'gettext_php_loaded');
31 if (! isset($gettext_php_domain)) {
32 $gettext_php_domain = '';
33 sqsession_register($gettext_php_domain, 'gettext_php_domain');
35 if (! isset($gettext_php_dir)) {
36 $gettext_php_dir = '';
37 sqsession_register($gettext_php_dir, 'gettext_php_dir');
39 if (! isset($gettext_php_translateStrings)) {
40 $gettext_php_translateStrings = array();
41 sqsession_register($gettext_php_translateStrings, 'gettext_php_translateStrings');
43 if (! isset($gettext_php_loaded_language)) {
44 $gettext_php_loaded_language = '';
45 sqsession_register($gettext_php_loaded_language, 'gettext_php_loaded_language');
47 if (! isset($gettext_php_short_circuit)) {
48 $gettext_php_short_circuit = false;
49 sqsession_register($gettext_php_short_circuit, 'gettext_php_short_circuit');
53 * Converts .po file into array and stores it in session.
55 * Used internally by _($str) function
57 * @internal function is used internally by functions/gettext.php code
59 function gettext_php_load_strings() {
60 global $squirrelmail_language, $gettext_php_translateStrings,
61 $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
62 $gettext_php_loaded_language, $gettext_php_short_circuit;
65 * $squirrelmail_language gives 'en' for English, 'de' for German,
66 * etc. I didn't wanna use getenv or similar, but you easily could
67 * change my code to do that.
70 $gettext_php_translateStrings = array();
72 $gettext_php_short_circuit = false; /* initialization */
74 $filename = $gettext_php_dir;
75 if (substr($filename, -1) != '/')
77 $filename .= $squirrelmail_language . '/LC_MESSAGES/' .
78 $gettext_php_domain . '.po';
80 $file = @fopen
($filename, 'r');
82 /* Uh-ho -- we can't load the file. Just fake it. :-)
83 This is also for English, which doesn't use translations */
84 $gettext_php_loaded = true;
85 $gettext_php_loaded_language = $squirrelmail_language;
86 /* Avoid fuzzy matching when we didn't load strings */
87 $gettext_php_short_circuit = true;
93 while (! feof($file)) {
95 $line = trim(fgets($file, 4096));
100 if (ereg('^msgid "(.*)"$', $line, $match)) {
101 if ($match[1] == '') {
103 * Potential multi-line
109 $line = trim(fgets($file, 4096));
110 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
112 $line = trim(fgets($file, 4096));
116 /* msgid "string string" */
119 } elseif (ereg('^msgstr "(.*)"$', $line, $match)) {
120 if ($match[1] == '') {
122 * Potential multi-line
127 $gettext_php_translateStrings[$key] = '';
128 $line = trim(fgets($file, 4096));
129 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
130 $gettext_php_translateStrings[$key] .= $match[1];
131 $line = trim(fgets($file, 4096));
135 /* msgstr "string string" */
136 $gettext_php_translateStrings[$key] = $match[1];
138 $gettext_php_translateStrings[$key] =
139 stripslashes($gettext_php_translateStrings[$key]);
140 /* If there is no translation, just use the untranslated string */
141 if ($gettext_php_translateStrings[$key] == '') {
142 $gettext_php_translateStrings[$key] = $key;
149 $gettext_php_loaded = true;
150 $gettext_php_loaded_language = $squirrelmail_language;
154 * Alternative php gettext function (short form)
156 * @link http://www.php.net/function.gettext
158 * @param string $str English string
159 * @return string translated string
162 global $gettext_php_loaded, $gettext_php_translateStrings,
163 $squirrelmail_language, $gettext_php_loaded_language,
164 $gettext_php_short_circuit;
166 if (! $gettext_php_loaded ||
167 $gettext_php_loaded_language != $squirrelmail_language) {
168 gettext_php_load_strings();
171 /* Try finding the exact string */
172 if (isset($gettext_php_translateStrings[$str])) {
173 return $gettext_php_translateStrings[$str];
176 /* See if we should short-circuit */
177 if ($gettext_php_short_circuit) {
178 $gettext_php_translateStrings[$str] = $str;
182 /* Look for a string that is very close to the one we want
183 Very computationally expensive */
187 foreach ($gettext_php_translateStrings as $k => $v) {
188 similar_text($str, $k, $newPercent);
189 if ($newPercent > $oldPercent) {
191 $oldPercent = $newPercent;
194 /* Require 80% match or better
195 Adjust to suit your needs */
196 if ($oldPercent > 80) {
197 /* Remember this so we don't need to search again */
198 $gettext_php_translateStrings[$str] = $oldStr;
202 /* Remember this so we don't need to search again */
203 $gettext_php_translateStrings[$str] = $str;
208 * Alternative php bindtextdomain function
210 * Sets path to directory containing domain translations
212 * @link http://www.php.net/function.bindtextdomain
213 * @param string $name gettext domain name
214 * @param string $dir directory that contains all translations
215 * @return string path to translation directory
217 function bindtextdomain($name, $dir) {
218 global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded;
220 if ($gettext_php_domain != $name) {
221 $gettext_php_domain = $name;
222 $gettext_php_loaded = false;
224 if ($gettext_php_dir != $dir) {
225 $gettext_php_dir = $dir;
226 $gettext_php_loaded = false;
233 * Alternative php textdomain function
235 * Sets default domain name
237 * @link http://www.php.net/function.textdomain
238 * @param string $name gettext domain name
239 * @return string gettext domain name
241 function textdomain($name = false) {
242 global $gettext_php_domain, $gettext_php_loaded;
244 if ($name != false && $gettext_php_domain != $name) {
245 $gettext_php_domain = $name;
246 $gettext_php_loaded = false;
249 return $gettext_php_domain;