80f3c63d1318f1af97cdb2aa6c00a608db27de80
3 /* Alternate to the system's built-in gettext.
4 * relies on .po files (can't read .mo easily).
5 * Uses the session for caching (speed increase)
6 * Possible use in other PHP scripts? The only SM-specific thing is
7 * $sm_language, I think
9 * Very special thanks to Konstantin Riabitsev for letting me use a
10 * server that didn't already have gettext on it!
13 if (defined('gettext_php'))
15 define('gettext_php', true);
17 global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
18 $gettext_php_translateStrings, $gettext_php_loaded_language;
20 if (! isset($gettext_php_loaded)) {
21 $gettext_php_loaded = false;
22 session_register('gettext_php_loaded');
24 if (! isset($gettext_php_domain)) {
25 $gettext_php_domain = '';
26 session_register('gettext_php_domain');
28 if (! isset($gettext_php_dir)) {
29 $gettext_php_dir = '';
30 session_register('gettext_php_dir');
32 if (! isset($gettext_php_translateStrings)) {
33 $gettext_php_translateStrings = array();
34 session_register('gettext_php_translateStrings');
36 if (! isset($gettext_php_loaded_language)) {
37 $gettext_php_loaded_language = '';
38 session_register('gettext_php_loaded_language');
41 function gettext_php_load_strings() {
42 global $squirrelmail_language, $gettext_php_translateStrings,
43 $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
44 $gettext_php_loaded_language;
46 // $squirrelmail_language gives 'en' for English, 'de' for German,
47 // etc. I didn't wanna use getenv or similar, but you easily could
48 // change my code to do that.
50 $gettext_php_translateStrings = array();
52 $filename = $gettext_php_dir;
53 if (substr($filename, -1) != '/')
55 $filename .= $squirrelmail_language . '/LC_MESSAGES/' .
56 $gettext_php_domain . '.po';
58 $file = @fopen
($filename, 'r');
59 if ($file === false) {
60 // Uh-ho -- we can't load the file.
62 $gettext_php_loaded = true;
68 while (! feof($file)) {
70 $line = trim(fgets($file, 4096));
74 if (ereg('^msgid "(.*)"$', $line, $match)) {
75 if ($match[1] == '') {
76 // Potential multi-line
81 $line = trim(fgets($file, 4096));
82 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
84 $line = trim(fgets($file, 4096));
88 // msgid "string string"
91 } elseif (ereg('^msgstr "(.*)"$', $line, $match)) {
92 if ($match[1] == '') {
93 // Potential multi-line
97 $gettext_php_translateStrings[$key] = '';
98 $line = trim(fgets($file, 4096));
99 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
100 $gettext_php_translateStrings[$key] .= $match[1];
101 $line = trim(fgets($file, 4096));
105 // msgstr "string string"
106 $gettext_php_translateStrings[$key] = $match[1];
108 $gettext_php_translateStrings[$key] =
109 stripslashes($gettext_php_translateStrings[$key]);
115 $gettext_php_loaded = true;
116 $gettext_php_loaded_language = $squirrelmail_language;
120 global $gettext_php_loaded, $gettext_php_translateStrings,
121 $squirrelmail_language, $gettext_php_loaded_language;
123 if (! $gettext_php_loaded ||
124 $gettext_php_loaded_language != $squirrelmail_language)
125 gettext_php_load_strings();
127 // Try finding the exact string
128 if (isset($gettext_php_translateStrings[$str]))
129 return $gettext_php_translateStrings[$str];
131 // Look for a string that is very close to the one we want
132 // Very computationally expensive
136 foreach ($gettext_php_translateStrings as $k => $v) {
137 similar_text($str, $k, $newPercent);
138 if ($newPercent > $oldPercent) {
140 $oldPercent = $newPercent;
143 // Require 80% match or better
144 // Adjust to suit your needs
145 if ($oldPercent > 80) {
146 // Remember this so we don't need to search again
147 $gettext_php_translateStrings[$str] = $oldStr;
151 // Remember this so we don't need to search again
152 $gettext_php_translateStrings[$str] = $str;
156 function bindtextdomain($name, $dir) {
157 global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded;
159 if ($gettext_php_domain != $name) {
160 $gettext_php_domain = $name;
161 $gettext_php_loaded = false;
163 if ($gettext_php_dir != $dir) {
164 $gettext_php_dir = $dir;
165 $gettext_php_loaded = false;
171 function textdomain($name = false) {
172 global $gettext_php_domain, $gettext_php_loaded;
174 if ($name != false && $gettext_php_domain != $name) {
175 $gettext_php_domain = $name;
176 $gettext_php_loaded = false;
178 return $gettext_php_domain;