diff options
author | Christian Schneider | 2007-10-11 00:39:30 +0000 |
---|---|---|
committer | Christian Schneider | 2007-10-11 00:39:30 +0000 |
commit | 35fe33f7364329dacf415c950bff01b6de9ef88e (patch) | |
tree | b0e6b018b50038ca20266723c53750268f508df5 /it_text.class | |
parent | 1f95711ff3e9697cd85a54545ab42e5fd3611317 (diff) | |
download | itools-35fe33f7364329dacf415c950bff01b6de9ef88e.tar.gz itools-35fe33f7364329dacf415c950bff01b6de9ef88e.tar.bz2 itools-35fe33f7364329dacf415c950bff01b6de9ef88e.zip |
Populated release branch
Diffstat (limited to 'it_text.class')
-rw-r--r-- | it_text.class | 245 |
1 files changed, 245 insertions, 0 deletions
diff --git a/it_text.class b/it_text.class new file mode 100644 index 0000000..f8b2583 --- /dev/null +++ b/it_text.class @@ -0,0 +1,245 @@ +<?php +/* +** $Id$ +** +** ITools - the Internet Tools Library +** +** Copyright (C) 1995-2006 by the ITools Authors. +** This program is free software; you can redistribute it and/or +** modify it under the terms of either the GNU General Public License +** or the GNU Lesser General Public License, as published by the Free +** Software Foundation. See http://www.gnu.org/licenses/ for details. +** +*/ + +class it_text +{ + var $actlanguage; # Selected language + var $defaultlanguage; # Browser language + var $languages = array(); # Active languages + var $languages_available = array(); # Available languages + var $statictext = array(); # Text array, read from php file on init + +/** + * Constructor + * @param $p['cookiename'] optional cookie name (default: 'LANGUAGE') + * @param $p['debug'] optional debug mode: display label of unknown texts, set to 'label' to always display labels instead of actual text + * @param $p['fallbacklanguage'] optional language to use for undefined texts (useful for partially translated projects) + * @param $p['forcelanguage'] optional language to use instead of user's preferred language + * @param $p['phpfile'] optional texts file, defaults to service's phpinclude/texts.php + */ +function it_text($p = null) +{ + $this->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 = (array)$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; +} + + + +/** + * INTERNAL function for T(): : Return translated text in the selected language + */ +function text($label, $language = null, $buggy = false) +{ + if ($buggy || (isset($language) && !is_string($language))) + it::error('deprecated usage of it_text::text()'); + + if ($this->p['debug'] === 'label') + return $label; + + if (!isset($language)) + $language = $this->actlanguage; + + if (isset($this->statictext[$label][$language])) + return $this->statictext[$label][$language]; + elseif ($this->p['fallbacklanguage'] && isset($this->statictext[$label][$this->p['fallbacklanguage']])) + return $this->statictext[$label][$this->p['fallbacklanguage']]; + + 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(array('auto_prepend\S+\s+'=>''), it_debug::backtrace()) . "\n"); + fclose($fh); + } + + return $this->p['debug'] ? "<blink>$label ($language)</blink>" : ''; +} + + +/** + * INTERNAL function for ET(): Return translated text with values replaced + */ +function etext($label, $values = null, $language = null) +{ + return $this->transmogrify($this->text($label, $language), $values); +} + + +/** + * INTERNAL function for T_set_language() + */ +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, '/'); + } +} + + +/** + * INTERNAL function for T_lang(): Get active language + */ +function get_language() +{ + return $this->actlanguage; +} + + +/** + * INTERNAL function for T_exists(): Check if a text entry for a specific label exists + */ +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, '<?php $this->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 */ +?> |