* Removed one language (it wasn't in the CVS so I commented it out)
[squirrelmail.git] / functions / gettext.php
CommitLineData
bb48d62e 1<?PHP
2
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)
829a33b6 6 * Possible use in other PHP scripts? The only SM-specific thing is
7 * $sm_language, I think
bb48d62e 8 */
9
10 if (defined('gettext_php'))
11 return;
12 define('gettext_php', true);
13
14 global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
15 $gettext_php_translateStrings;
16
17 if (! isset($gettext_php_loaded)) {
18 $gettext_php_loaded = false;
19 session_register('gettext_php_loaded');
20 }
829a33b6 21 if (! isset($gettext_php_domain)) {
22 $gettext_php_domain = '';
23 session_register('gettext_php_translateStrings');
24 }
25 if (! isset($gettext_php_dir)) {
26 $gettext_php_dir = '';
27 session_register('gettext_php_translateStrings');
28 }
29 if (! isset($gettext_php_translateStrings)) {
30 $gettext_php_translateStrings = array();
31 session_register('gettext_php_translateStrings');
32 }
33
bb48d62e 34 function gettext_php_load_strings() {
35 global $sm_language, $gettext_php_translateStrings,
36 $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded;
37
38 // $sm_language gives 'en' for English, 'de' for German, etc.
829a33b6 39 // I didn't wanna use getenv or similar, but you easily could change
40 // my code to do that.
bb48d62e 41
42 $gettext_php_translateStrings = array();
bb48d62e 43
44 $filename = $gettext_php_dir;
45 if (substr($filename, -1) != '/')
46 $filename .= '/';
47 $filename .= $sm_language . '/LC_MESSAGES/' . $gettext_php_domain . '.po';
48
49 $file = fopen($filename, 'r');
50 if ($file === false)
829a33b6 51 // Uh-ho
bb48d62e 52 return;
53
54 $key = '';
55 $SkipRead = false;
56 while (! feof($file)) {
57 if (! $SkipRead)
58 $line = trim(fgets($file, 4096));
59 else
60 $SkipRead = false;
61
62 if (ereg('^msgid "(.*)"$', $line, $match)) {
63 if ($match[1] == '') {
64 // Potential multi-line
829a33b6 65 // msgid ""
66 // "string string "
67 // "string string"
bb48d62e 68 $key = '';
69 $line = trim(fgets($file, 4096));
70 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
71 $key .= $match[1];
72 $line = trim(fgets($file, 4096));
73 }
74 } else {
829a33b6 75 // msgid "string string"
bb48d62e 76 $key = $match[1];
77 }
78 } elseif (ereg('^msgstr "(.*)"$', $line, $match)) {
79 if ($match[1] == '') {
80 // Potential multi-line
829a33b6 81 // msgstr ""
82 // "string string "
83 // "string string"
bb48d62e 84 $gettext_php_translateStrings[$key] = '';
85 $line = trim(fgets($file, 4096));
86 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
87 $gettext_php_translateStrings[$key] .= $match[1];
88 $line = trim(fgets($file, 4096));
89 }
90 } else {
829a33b6 91 // msgstr "string string"
bb48d62e 92 $gettext_php_translateStrings[$key] = $match[1];
93 }
94 $key = '';
95 }
96 }
97 fclose($file);
98
99 $gettext_php_loaded = true;
100 }
101
102 function _($str) {
103 global $gettext_php_loaded;
104
105 if (! $gettext_php_loaded)
106 gettext_php_load_strings();
107
108 if (isset($gettext_php_translateStrings[$str]))
109 return $gettext_php_translateStrings[$str];
110
111 return $str;
112 }
113
114 function bindtextdomain($name, $dir) {
115 global $gettext_php_domain;
116
117 $gettext_php_domain = $name;
118 $gettext_php_dir = $dir;
119 $gettext_php_loaded = false;
120
121 return $dir;
122 }
123
124 function textdomain($name = false) {
125 global $gettext_php_domain;
126
127 if ($name != false)
128 {
129 $gettext_php_domain = $name;
130 $gettext_php_loaded = false;
131 }
132 return $gettext_php_domain;
133 }
134