7883fd8ada1551bcb3171cc36b04818af75a031f
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)
8 if (defined('gettext_php'))
10 define('gettext_php', true);
12 global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
13 $gettext_php_translateStrings;
15 if (! isset($gettext_php_loaded)) {
16 $gettext_php_loaded = false;
17 session_register('gettext_php_loaded');
20 function gettext_php_load_strings() {
21 global $sm_language, $gettext_php_translateStrings,
22 $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded;
24 // $sm_language gives 'en' for English, 'de' for German, etc.
25 // I didn't wanna use getenv or similar.
27 $gettext_php_translateStrings = array();
28 session_register('gettext_php_translateStrings');
30 $filename = $gettext_php_dir;
31 if (substr($filename, -1) != '/')
33 $filename .= $sm_language . '/LC_MESSAGES/' . $gettext_php_domain . '.po';
35 $file = fopen($filename, 'r');
41 while (! feof($file)) {
43 $line = trim(fgets($file, 4096));
47 if (ereg('^msgid "(.*)"$', $line, $match)) {
48 if ($match[1] == '') {
49 // Potential multi-line
51 $line = trim(fgets($file, 4096));
52 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
54 $line = trim(fgets($file, 4096));
59 } elseif (ereg('^msgstr "(.*)"$', $line, $match)) {
60 if ($match[1] == '') {
61 // Potential multi-line
62 $gettext_php_translateStrings[$key] = '';
63 $line = trim(fgets($file, 4096));
64 while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) {
65 $gettext_php_translateStrings[$key] .= $match[1];
66 $line = trim(fgets($file, 4096));
69 $gettext_php_translateStrings[$key] = $match[1];
76 $gettext_php_loaded = true;
80 global $gettext_php_loaded;
82 if (! $gettext_php_loaded)
83 gettext_php_load_strings();
85 if (isset($gettext_php_translateStrings[$str]))
86 return $gettext_php_translateStrings[$str];
91 function bindtextdomain($name, $dir) {
92 global $gettext_php_domain;
94 $gettext_php_domain = $name;
95 $gettext_php_dir = $dir;
96 $gettext_php_loaded = false;
101 function textdomain($name = false) {
102 global $gettext_php_domain;
106 $gettext_php_domain = $name;
107 $gettext_php_loaded = false;
109 return $gettext_php_domain;