diff options
author | Urban Müller | 2007-08-30 16:41:39 +0000 |
---|---|---|
committer | Urban Müller | 2007-08-30 16:41:39 +0000 |
commit | 941e3752edc6b09777b5388a07ae093c05538dcd (patch) | |
tree | 3d8784c74178a140250bb2f9248948e7ae73a6ba | |
parent | 16d4ad60320f96de6807a3826f3d2e99018b9272 (diff) | |
download | itools-941e3752edc6b09777b5388a07ae093c05538dcd.tar.gz itools-941e3752edc6b09777b5388a07ae093c05538dcd.tar.bz2 itools-941e3752edc6b09777b5388a07ae093c05538dcd.zip |
major cleanup
-rw-r--r-- | it_text.class | 196 |
1 files changed, 34 insertions, 162 deletions
diff --git a/it_text.class b/it_text.class index ab378ac..a35f701 100644 --- a/it_text.class +++ b/it_text.class @@ -14,14 +14,11 @@ class it_text { - var $defaultlanguage; # Preferred language (without cookie) var $actlanguage; # Selected language - var $actlanguagename; # Name of selected language + var $defaultlanguage; # Browser language var $languages = array(); # Active languages var $languages_available = array(); # Available languages - var $language_failsafe; # First available language. Must be complete (used as fallback) var $statictext = array(); # Text array, read from php file on init - var $unknown_labels = array(); # array for spooling unknown labels in debug mode /** * Constructor @@ -39,84 +36,49 @@ function it_text($p = null) include($this->p['phpfile']); # Get array of supported languages and their names - foreach(array_keys($this->statictext['_']) as $code) + $this->languages_available = $this->statictext['_']; + foreach($this->languages_available as $code => $languagename) { - # If a language's name in '_' is unset, ignore it - if ($languagename = $this->statictext['_'][$code]) + # Only use a language in browser/cookie detection below if it's not disabled by a leading '-' + if (substr($languagename, 0, 1) != '-') { - $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; - } + $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 $lang) - { - if (isset($this->languages[$lang])) - { - $this->defaultlanguage = $lang; - debug("Got language from browser: {$this->defaultlanguage}", 6); + 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; - } - 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; - - # language override in URL like page.fr.html - if (preg_match('/\.([a-z]{2})\.[^.\/]+$/i',$_SERVER['PHP_SELF'], $match) && $this->languages[$match[1]]) - $this->set_language($match[1], FALSE); # do not set a cookie - - # 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); } + $this->defaultlanguage = $this->actlanguage; - # 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]; + $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; - debug("Used language is {$this->actlanguagename}, default language is {$this->defaultlanguage}.", 6); + return $this->languages[$code]; } + /** * Instanciate singleton if necessary */ @@ -126,6 +88,8 @@ function init() new it_text; } + + /** * Return a text in the selected language. * @param $label Label of text to return @@ -134,17 +98,22 @@ function init() * @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] !== '') + if (isset($this->statictext[$label][$language])) return $this->statictext[$label][$language]; - $this->unknown_labels[] = $label; + 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'] ? "<blink>$label ($language)</blink>" : $this->statictext[$label][$this->language_failsafe]; } @@ -200,16 +169,6 @@ function get_language() /** - * 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. @@ -266,93 +225,6 @@ function transmogrify($text, $values = null) /** - * when running it_text in debug mode, you can use this to die - * in case there were unknown labels with a list of all unknown labels - */ -function checkout_unknown_labels() -{ - if ($this->unknown_labels) - it::fatal('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) */ |