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 foreach(array_keys($this->statictext['_']) as $code) { # If a language's name in '_' is unset, ignore it if ($languagename = $this->statictext['_'][$code]) { $this->languages_available[$code] = $languagename; # Only use a language in browser/cookie detection below if it's not diasbled by a leading '-' if (substr($languagename, 0, 1) != '-') { $this->languages[$code] = $languagename; if (!isset($this->language_failsafe)) $this->language_failsafe = $code; } } } # Set our default language according to browser preference if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { foreach(explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $lang) { if (isset($this->languages[$lang])) { $this->defaultlanguage = $lang; debug("Got language from browser: {$this->defaultlanguage}", 6); break; } else { $short = substr($lang, 0, 2); if (isset($this->languages[$short])) { $this->defaultlanguage = $short; debug("Got language family from browser: {$this->defaultlanguage}", 6); break; } } } if (count($this->languages) == 0) debug("No active languages found!", 5); } # If no language matched, use failsafe language if (!$this->languages[$this->defaultlanguage]) $this->defaultlanguage = $this->language_failsafe; # If a cookie is set, use its value as our active language if (isset($_COOKIE[$this->p['cookiename']]) && ($this->languages[$cookie = $_COOKIE[$this->p['cookiename']]])) { $this->actlanguage = $cookie; debug("Got language from cookie: {$this->actlanguage}", 6); } # If the cookie supplied no valid language, we use our calculated default language if (!isset($this->actlanguage)) $this->actlanguage = $this->defaultlanguage; # If the user wants something else, comply if (isset($this->p['forcelanguge']) && isset($this->languages[$this->p['forcelanguage']])) $this->actlanguage = $this->p['forcelanguage']; # And finally, record the name of our active language. $this->actlanguagename = $this->languages[$this->actlanguage]; debug("Used language is {$this->actlanguagename}, default language is {$this->defaultlanguage}.", 6); } /** * 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 ($this->statictext[$label][$language] !== '') return $this->statictext[$label][$language]; $this->unknown_labels[] = $label; 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; } /** * Get default language * @return default language */ function get_default_language() { return $this->defaultlanguage; } /** * 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; } /** * when running it_text in debug mode, you can use this to die (internal_error) * in case there were unknown labels with a list of all unknown labels */ function checkout_unknown_labels() { if ($this->unknown_labels) internal_error('No text found for labels: ' . implode(',', $this->unknown_labels)); } /** * Replaces special chars with their TeX equivalent * @param $text Text to convert from ISO-8859-1 to TeX encoding * @param $convertlinebreaks Whether to convert LFs to TeX linebreaks * @return TeX encoded text to be inserted into TeX template */ function texify($text, $convertlinebreaks = true) { $translation = array ( 'À' => '{\`A}', 'Á' => "{\'A}", 'Â' => '{\^A}', 'Ã' => '{\~A}', 'Ä' => '{\"A}', 'Ç' => '{\c C}', 'È' => '{\`E}', 'É' => "{\'E}", 'Ê' => '{\^E}', 'Ë' => '{\"E}', 'Ì' => '{\`I}', 'Í' => "{\'I}", 'Î' => '{\^I}', 'Ï' => '{\"I}', 'Ñ' => '{\~N}', 'Ò' => '{\`O}', 'Ó' => "{\'O}", 'Ô' => '{\^O}', 'Õ' => '{\~O}', 'Ö' => '{\"O}', 'Ù' => '{\`U}', 'Ú' => "{\'U}", 'Û' => '{\^U}', 'Ü' => '{\"U}', 'ß' => '{\ss}', 'à' => '{\`a}', 'á' => "{\'a}", 'â' => '{\^a}', 'ã' => '{\~a}', 'ä' => '{\"a}', 'ç' => '{\c c}', 'è' => '{\`e}', 'é' => "{\'e}", 'ê' => '{\^e}', 'ë' => '{\"e}', 'ì' => '{\`i}', 'í' => "{\'i}", 'î' => '{\^i}', 'ï' => '{\"i}', 'ñ' => '{\~n}', 'ò' => '{\`o}', 'ó' => "{\'o}", 'ô' => '{\^o}', 'õ' => '{\~o}', 'ö' => '{\"o}', 'ù' => '{\`u}', 'ú' => "{\'u}", 'û' => '{\^u}', 'ü' => '{\"u}', 'ÿ' => '{\"y}', '"' => "''", '\\' => '/', '%' => '\%', '$' => '\$', '#' => '\#', '&' => '\&', '_' => '\_', '^' => '', ); if ($convertlinebreaks) $translation["\n"] = "\\\\\n"; return strtr($text, $translation); } /** * 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 */ /* * Globally available functions without need for object */ /* * Shortcut to $it_text->Text() */ function T($label, $raw = null, $language = null) { return $GLOBALS['it_text']->text($label, $raw, $language); } /* * Shortcut to $it_text->etext() */ function ET($label, $values = null, $language = null) { return $GLOBALS['it_text']->etext($label, $values, $language); } /** * Shortcut to $it_text->get_language() */ function T_lang() { return isset($GLOBALS['it_text']) ? $GLOBALS['it_text']->get_language() : "de"; } /** * Shortcut to $it_text->get_language() */ function T_set_language($language, $setcookie = true) { return $GLOBALS['it_text']->set_language($language, $setcookie); } /** * Shortcut to $it_text->text_exists() */ function T_exists($label, $language = null) { return $GLOBALS['it_text']->text_exists($label, $language); } ?>