p = (array)$p + array('cookiename' => 'LANGUAGE', 'debug' => false, 'phpfile' => $GLOBALS['ULTRAHOME'] . '/phpinclude/texts.php'); # Read $this->statictext from php file if it's not defined yet if (!$this->statictext) include($this->p['phpfile']); # Get array of supported languages and their names $this->languages_available = $this->statictext['_']; foreach($this->languages_available as $code => $languagename) { # Only use a language in browser/cookie detection below if it's not disabled by a leading '-' if (substr($languagename, 0, 1) != '-') { $this->languages[$code] = $languagename; if (!$this->actlanguage) $this->initlang($code, "setting failsafe language"); } } # Set our default language according to browser preference if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { foreach(explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $code) if ($this->initlang($code, "setting language from browser") || $this->initlang(substr($code, 0, 2), "setting language family from browser")) break; } $this->defaultlanguage = $this->actlanguage; $this->initlang(it::match('\.([a-z]{2})\.[^./]+$', $_SERVER['PHP_SELF']), "setting language from url override"); $this->initlang($this->languages[$_COOKIE[$this->p['cookiename']]], "setting language from cookie"); $this->initlang($this->p['forcelanguage'], "setting language from programmer override"); # Make this object available under $GLOBALS['it_text'], or add my texts to $GLOBALS['it_text'] if it exists if (!$GLOBALS['it_text']) $GLOBALS['it_text'] =& $this; else $GLOBALS['it_text']->statictext += $this->statictext; } # internal: overwrite language setting if code is valid, return success function initlang($code, $debugmsg) { if ($this->languages[$code]) $this->actlanguage = $code; return $this->languages[$code]; } /** * Instanciate singleton if necessary */ function init() { if (!$GLOBALS['it_text']) new it_text; } /** * Return a text in the selected language. * @param $label Label of text to return * @param $raw Optional unused obsolete parameter * @param $language Optional language to return text in. * @return Localized text string */ function text($label, $raw = null, $language = null) { if ($this->p['debug'] === 'label') return $label; if (!isset($language)) $language = $this->actlanguage; if (isset($this->statictext[$label][$language])) return $this->statictext[$label][$language]; if ($fh = fopen($GLOBALS['ULTRAHOME'] . "/log/text_log", "a")) { fputs($fh, date("Y-m-d H:i:s") . "\t" . $_SERVER['REQUEST_URI'] . "\t" . $label . "\t" . $language . "\t" . it::replace('auto_prepend\S+\s+'=>'', it_debug::backtrace()) . "\n"); fclose($fh); } return $this->p['debug'] ? "$label ($language)" : $this->statictext[$label][$this->language_failsafe]; } /** * Return a text in the selected language, with correctly encoded umlauts. * Replaces variables of the form {obj.var} with value, e.g. {user.name} * NOTE: Invalid object names or non-existing variables are simply deleted. * @param $label Label of text to return * @param $values Associative array containing values to fill in * @param $language Optional language to return text in. * @return Localized text string with variables replaced by their values */ function etext($label, $values = null, $language = null) { return $this->transmogrify($this->text($label, null, $language), $values); } /** * Change language * @param $language New language to set * @param $setcookie Optional flag if a cookie is to be set (default: true) */ function set_language($language, $setcookie = true) { # If $language is unknown, delete the cookie if ($this->languages_available[$language]) { if (!$this->languages[$language]) debug('Selected an existing but currently disabled language!', 6); $this->actlanguage = $language; if ($setcookie) SetCookie($this->p['cookiename'], $this->actlanguage, time() + 31536000, '/'); # 1 year } else { $this->actlanguage = $this->defaultlanguage; if ($setcookie) SetCookie($this->p['cookiename'], '', time() + 31536000, '/'); } } /** * Get active language * @return currently active language */ function get_language() { return $this->actlanguage; } /** * Check if a text entry for a specific label exists * @param $label Label to check * @return true if text exists in actual (or supplied) language, false otherwise. */ function text_exists($label, $language = null) { return isset($this->statictext[$label][isset($language) ? $language : $this->actlanguage]); } /** * Create / overwrite a text in the selected language. Call dump_php() to make the change permanent. * @param $label Label of text to change * @param $text New text to set * @param $language Optional language that is to be manipulated */ function set($label, $text = null, $language = null) { if (!isset($language)) $language = $this->actlanguage; $this->statictext[$label][$language] = $text; } /** * Replaces variables of the form {obj.var} with value, e.g. {user.name} * NOTE: Invalid object names or non-existing variables are simply deleted. */ function transmogrify($text, $values = null) { while (preg_match('/{([\w.]+)}/', $text, $regs)) { $path = explode('.', $regs[1]); if ($values) $value =& $values; else $value =& $GLOBALS; # Recurse into nested arrays/object members foreach ($path as $key) { if (is_object($value)) $value =& $value->$key; else $value =& $value[$key]; } $text = str_replace($regs[0], $value, $text); } return $text; } /** * Re-create php text file from $this->statictext * @return true if successful, false if not (usually if file is not writeable by user www) */ function dump_php() { $result = false; # Special sorting: natural, but _ is the first entry $keys = array_keys($this->statictext); natsort($keys); foreach($keys as $key) $text[$key] = $this->statictext[$key]; $this->statictext = array_merge(array('_' => $text['_']), $text); $oldmask = umask(002); if ($f = fopen($this->p['phpfile'], 'w')) { $result = (fputs($f, 'statictext = ' . strtr(var_export($this->statictext, true), array("=> \n array (" => "=> array(", "array (\n '_'" => "array(\n'_'", "\n ),\n " => "\n),\n", "\n ),\n" => "\n)\n")) . ";\n?>\n") !== false); fclose($f); } umask($oldmask); return $result; } } /* End class it_text */ ?>