Preparation to begin using phpdocumentor.
[squirrelmail.git] / functions / gettext.php
CommitLineData
99f538bf 1<?php
bb48d62e 2
35586184 3/**
4 * gettext.php
5 *
76911253 6 * Copyright (c) 1999-2003 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$
d6c32258 16 * @package squirrelmail
35586184 17 */
2ba13803 18
d6c32258 19/** Almost everything requires global.php... */
4f664925 20require_once(SM_PATH . 'functions/global.php');
9eb0fbd4 21
35586184 22global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
99f538bf 23 $gettext_php_translateStrings, $gettext_php_loaded_language,
24 $gettext_php_short_circuit;
829a33b6 25
99f538bf 26if (! isset($gettext_php_loaded)) {
27 $gettext_php_loaded = false;
9eb0fbd4 28 sqsession_register($gettext_php_loaded, 'gettext_php_loaded');
99f538bf 29}
30if (! isset($gettext_php_domain)) {
31 $gettext_php_domain = '';
9eb0fbd4 32 sqsession_register($gettext_php_domain, 'gettext_php_domain');
99f538bf 33}
34if (! isset($gettext_php_dir)) {
35 $gettext_php_dir = '';
9eb0fbd4 36 sqsession_register($gettext_php_dir, 'gettext_php_dir');
99f538bf 37}
38if (! isset($gettext_php_translateStrings)) {
39 $gettext_php_translateStrings = array();
9eb0fbd4 40 sqsession_register($gettext_php_translateStrings, 'gettext_php_translateStrings');
99f538bf 41}
42if (! isset($gettext_php_loaded_language)) {
43 $gettext_php_loaded_language = '';
9eb0fbd4 44 sqsession_register($gettext_php_loaded_language, 'gettext_php_loaded_language');
99f538bf 45}
46if (! isset($gettext_php_short_circuit)) {
47 $gettext_php_short_circuit = false;
9eb0fbd4 48 sqsession_register($gettext_php_short_circuit, 'gettext_php_short_circuit');
99f538bf 49}
55c10992 50
99f538bf 51function 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;
55
56 /*
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.
60 */
61
62 $gettext_php_translateStrings = array();
63
64 $gettext_php_short_circuit = false; /* initialization */
65
66 $filename = $gettext_php_dir;
67 if (substr($filename, -1) != '/')
68 $filename .= '/';
69 $filename .= $squirrelmail_language . '/LC_MESSAGES/' .
70 $gettext_php_domain . '.po';
71
72 $file = @fopen($filename, 'r');
73 if ($file == false) {
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;
80 return;
81 }
82
83 $key = '';
84 $SkipRead = false;
85 while (! feof($file)) {
a81b6556 86 if (! $SkipRead) {
bb48d62e 87 $line = trim(fgets($file, 4096));
a81b6556 88 } else {
99f538bf 89 $SkipRead = false;
a81b6556 90 }
99f538bf 91
92 if (ereg('^msgid "(.*)"$', $line, $match)) {
93 if ($match[1] == '') {
94 /*
95 * Potential multi-line
96 * msgid ""
97 * "string string "
98 * "string string"
99 */
100 $key = '';
101 $line = trim(fgets($file, 4096));
102 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
103 $key .= $match[1];
104 $line = trim(fgets($file, 4096));
105 }
106 $SkipRead = true;
107 } else {
108 /* msgid "string string" */
109 $key = $match[1];
110 }
111 } elseif (ereg('^msgstr "(.*)"$', $line, $match)) {
112 if ($match[1] == '') {
113 /*
114 * Potential multi-line
115 * msgstr ""
116 * "string string "
117 * "string string"
118 */
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));
124 }
125 $SkipRead = true;
126 } else {
127 /* msgstr "string string" */
128 $gettext_php_translateStrings[$key] = $match[1];
129 }
55c10992 130 $gettext_php_translateStrings[$key] =
99f538bf 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;
135 }
136 $key = '';
137 }
138 }
139 fclose($file);
140
141 $gettext_php_loaded = true;
142 $gettext_php_loaded_language = $squirrelmail_language;
143}
55c10992 144
99f538bf 145function _($str) {
146 global $gettext_php_loaded, $gettext_php_translateStrings,
147 $squirrelmail_language, $gettext_php_loaded_language,
148 $gettext_php_short_circuit;
149
150 if (! $gettext_php_loaded ||
151 $gettext_php_loaded_language != $squirrelmail_language) {
152 gettext_php_load_strings();
153 }
154
155 /* Try finding the exact string */
156 if (isset($gettext_php_translateStrings[$str])) {
157 return $gettext_php_translateStrings[$str];
158 }
159
160 /* See if we should short-circuit */
161 if ($gettext_php_short_circuit) {
162 $gettext_php_translateStrings[$str] = $str;
163 return $str;
164 }
165
166 /* Look for a string that is very close to the one we want
167 Very computationally expensive */
168 $oldPercent = 0;
169 $oldStr = '';
170 $newPercent = 0;
171 foreach ($gettext_php_translateStrings as $k => $v) {
172 similar_text($str, $k, $newPercent);
173 if ($newPercent > $oldPercent) {
55c10992 174 $oldStr = $v;
175 $oldPercent = $newPercent;
99f538bf 176 }
177 }
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;
183 return $oldStr;
184 }
185
186 /* Remember this so we don't need to search again */
187 $gettext_php_translateStrings[$str] = $str;
188 return $str;
189}
190
191function bindtextdomain($name, $dir) {
192 global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded;
193
194 if ($gettext_php_domain != $name) {
195 $gettext_php_domain = $name;
196 $gettext_php_loaded = false;
197 }
198 if ($gettext_php_dir != $dir) {
199 $gettext_php_dir = $dir;
200 $gettext_php_loaded = false;
201 }
202
203 return $dir;
204}
55c10992 205
99f538bf 206function textdomain($name = false) {
207 global $gettext_php_domain, $gettext_php_loaded;
208
209 if ($name != false && $gettext_php_domain != $name) {
210 $gettext_php_domain = $name;
211 $gettext_php_loaded = false;
212 }
213 return $gettext_php_domain;
214}
55c10992 215
99f538bf 216?>