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
10 if (defined('gettext_php'))
12 define('gettext_php', true);
14 global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
15 $gettext_php_translateStrings;
17 if (! isset($gettext_php_loaded)) {
18 $gettext_php_loaded = false;
19 session_register('gettext_php_loaded');
21 if (! isset($gettext_php_domain)) {
22 $gettext_php_domain = '';
23 session_register('gettext_php_translateStrings');
25 if (! isset($gettext_php_dir)) {
26 $gettext_php_dir = '';
27 session_register('gettext_php_translateStrings');
29 if (! isset($gettext_php_translateStrings)) {
30 $gettext_php_translateStrings = array();
31 session_register('gettext_php_translateStrings');
34 function gettext_php_load_strings() {
35 global $sm_language, $gettext_php_translateStrings,
36 $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded;
38 // $sm_language gives 'en' for English, 'de' for German, etc.
39 // I didn't wanna use getenv or similar, but you easily could change
40 // my code to do that.
42 $gettext_php_translateStrings = array();
44 $filename = $gettext_php_dir;
45 if (substr($filename, -1) != '/')
47 $filename .= $sm_language . '/LC_MESSAGES/' . $gettext_php_domain . '.po';
49 $file = fopen($filename, 'r');
56 while (! feof($file)) {
58 $line = trim(fgets($file, 4096));
62 if (ereg('^msgid "(.*)"$', $line, $match)) {
63 if ($match[1] == '') {
64 // Potential multi-line
69 $line = trim(fgets($file, 4096));
70 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
72 $line = trim(fgets($file, 4096));
75 // msgid "string string"
78 } elseif (ereg('^msgstr "(.*)"$', $line, $match)) {
79 if ($match[1] == '') {
80 // Potential multi-line
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));
91 // msgstr "string string"
92 $gettext_php_translateStrings[$key] = $match[1];
99 $gettext_php_loaded = true;
103 global $gettext_php_loaded;
105 if (! $gettext_php_loaded)
106 gettext_php_load_strings();
108 if (isset($gettext_php_translateStrings[$str]))
109 return $gettext_php_translateStrings[$str];
114 function bindtextdomain($name, $dir) {
115 global $gettext_php_domain;
117 $gettext_php_domain = $name;
118 $gettext_php_dir = $dir;
119 $gettext_php_loaded = false;
124 function textdomain($name = false) {
125 global $gettext_php_domain;
129 $gettext_php_domain = $name;
130 $gettext_php_loaded = false;
132 return $gettext_php_domain;