diff options
Diffstat (limited to 'text.class')
-rw-r--r-- | text.class | 226 |
1 files changed, 125 insertions, 101 deletions
@@ -4,7 +4,7 @@ ** ** ITools - the Internet Tools Library ** -** Copyright (C) 1995-2003 by the ITools Authors. +** 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 @@ -12,18 +12,18 @@ ** */ -class it_text extends it_db_record +class it_text { - var $defaultlanguage; /* Preferred language (without cookie) */ - var $actlanguage; /* Selected language */ - var $actlanguagename; /* Name of selected language */ - var $languages = array(); /* Active languages */ - var $languages_available = array(); /* Available languages */ - var $language_failsafe; /* First available language */ - var $debug; /* Non-empty if debug mode is desired */ - var $cookiename; /* Name of the language cookie, default is "LANGUAGE" */ - var $unknown_labels = array(); /* array for spooling unknown labels in - debug mode */ + var $defaultlanguage; # Preferred language (without cookie) + var $actlanguage; # Selected language + var $actlanguagename; # Name of selected language + var $languages = array(); # Active languages + var $languages_available = array(); # Available languages + var $language_failsafe; # First available language + var $statictext = array(); # Text array, read from php file on init + var $debug; # Non-empty if debug mode is desired + var $cookiename; # Name of the language cookie, default is "LANGUAGE" + var $unknown_labels = array(); # array for spooling unknown labels in debug mode /** * Constructor @@ -33,47 +33,52 @@ class it_text extends it_db_record * @param $db Optional database object, defaults to global $it_db * @param $cookiename Optional cookie name (default: 'LANGUAGE') */ -function it_text($defaultlanguage='de', $tablename='it_texts', $debug='', $db=0, $cookiename='LANGUAGE') +function it_text($defaultlanguage = 'de', $tablename = 'it_texts', $debug = '', $db = 0, $cookiename = 'LANGUAGE') { + $this->phpfile = isset($phpfile) ? $phpfile : ($GLOBALS['ULTRAHOME'] . '/phpinclude/texts.php'); $this->debug = $debug; $this->cookiename = $cookiename; - /* Create database objects */ - if (is_object($db)) + # Read $this->statictext from php file if it's not defined yet + if (!$this->statictext) + include($this->phpfile); + + # $$$ MIGRATION: Try to create texts.php from database if it doesn't exist + if (!$this->statictext) + { + $db = is_object($db) ? $db : $GLOBALS['it_db']; $table = new it_db_table($db, $tablename); - else - $table = new it_db_table($GLOBALS['it_db'], $tablename); - $this->it_db_record($table, 'Label'); - - /* Get array of supported languages and their names */ - if (!$this->read('_')) - internal_error("Can't read language names"); - $languages = explode(',', $table->field_names()); - while (list($index, $code) = each($languages)) + $record = new it_db_record($table, 'Label'); + for ($record->select(); $record->fetch_next();) + { + $data = $record->data; + unset($data['Label']); + $this->statictext[$record->data['Label']] = $data; + } + $this->dump_php(); + } + + # Get array of supported languages and their names + foreach(array_keys($this->statictext['_']) as $code) { - if ($code != 'Label') + # If a language's name in '_' is unset, ignore it + if ($languagename = $this->statictext['_'][$code]) { - /* If a language's "_" field is unset, ignore it */ - if ($languagename = $this->data[$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_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 (!isset($this->language_failsafe)) + $this->language_failsafe = $code; } } } - /* Set our default language according to browser preference */ + # Set our default language according to browser preference if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { - /* SQL field names can't contain "-" so we change these to "_" */ - $languages = explode(',', strtr($_SERVER['HTTP_ACCEPT_LANGUAGE'], '-', '_')); - while (list($key, $lang) = each($languages)) + foreach(explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $lang) { if (isset($this->languages[$lang])) { @@ -93,35 +98,35 @@ function it_text($defaultlanguage='de', $tablename='it_texts', $debug='', $db=0, } } if (count($this->languages) == 0) - debug("No active languages found!", 5); + debug("No active languages found!", 5); } - /* If no language matched, use the one that was supplied by our caller */ + # If no language matched, use the one that was supplied by our caller if (!isset($this->defaultlanguage)) $this->defaultlanguage = $defaultlanguage; - /* If language is still invalid, use the first one from our database */ + # If language is still invalid, use failsafe language if (!$this->languages[$this->defaultlanguage]) { debug("Bad language \"{$this->defaultlanguage}\", using first entry \"{$this->languages[$this->language_failsafe]}\"", 5); $this->defaultlanguage = $this->language_failsafe; } - /* If a cookie is set, we use its value as our active language */ + # If a cookie is set, we use its value as our active language if (isset($_COOKIE[$this->cookiename]) && ($cookie = $_COOKIE[$this->cookiename])) { - if ($this->languages[$cookie]) /* Is this language available? */ + if ($this->languages[$cookie]) # Is this language available? { $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 the cookie supplied no valid language, we use our calculated default language if (!isset($this->actlanguage)) $this->actlanguage = $this->defaultlanguage; - /* And finally, record the name of our active language. */ + # 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); @@ -135,31 +140,19 @@ function it_text($defaultlanguage='de', $tablename='it_texts', $debug='', $db=0, * @param $language Optional language to return text in. * @return Localized text string */ -function text($label, $raw=0, $language='') +function text($label, $raw = null, $language = null) { if ($this->debug === 'label') return $label; - if ($this->read($label)) - { - if ($language == '') - $language = $this->actlanguage; - - /* - ** If field is NULL, consider it undefined (and return label if in - ** debug mode). That's why we can't call safe_get_field() here. - */ - if (isset($this->data[$language])) - return $this->data[$language]; - } + if (!isset($language)) + $language = $this->actlanguage; - if ($this->debug) - { - $this->unknown_labels[] = $label; - return "<blink>$label</blink>"; - } - else - return $this->label_unknown($label); + if (isset($this->statictext[$label][$language])) + return $this->statictext[$label][$language]; + + $this->unknown_labels[] = $label; + return $this->debug ? "<blink>$label ($language)</blink>" : $this->label_unknown($label); } @@ -170,7 +163,7 @@ function text($label, $raw=0, $language='') */ function label_unknown($label) { - internal_error("No text found for label \"$label\""); + internal_error("No text found for label \"$label\""); } @@ -183,9 +176,9 @@ function label_unknown($label) * @param $language Optional language to return text in. * @return Localized text string with variables replaced by their values */ -function etext($label, $values=null, $language='') +function etext($label, $values = null, $language = null) { - return it_text_transmogrify($this->text($label, false, $language), is_array($values) ? $values : null); + return it_text_transmogrify($this->text($label, null, $language), $values); } @@ -194,22 +187,22 @@ function etext($label, $values=null, $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) +function set_language($language, $setcookie = true) { /* If set to an invalid language (via forged GET?) we 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 ($this->languages_available[$language]) + { + if (!$this->languages[$language]) + debug('Selected an existing but currently disabled language!', 6); + $this->actlanguage = $language; if ($setcookie) - SetCookie($this->cookiename, $this->actlanguage, time() + 31536000, '/'); /* 1 Year */ + SetCookie($this->cookiename, $this->actlanguage, time() + 31536000, '/'); # 1 year } - else + else { $this->actlanguage = $this->defaultlanguage; if ($setcookie) - SetCookie($this->cookiename, '', time() + 31536000, '/'); + SetCookie($this->cookiename, '', time() + 31536000, '/'); } } @@ -237,12 +230,26 @@ function get_default_language() /** * 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 text_exists($label) +function set($label, $text = null, $language = null) { - if ($this->read($label)) - return 1; - return 0; + if (!isset($language)) + $language = $this->actlanguage; + + $this->statictext[$label][$language] = $text; } @@ -252,17 +259,8 @@ function text_exists($label) */ function checkout_unknown_labels() { - if (count($this->unknown_labels) > 0) - { - $debug_text = 'No text found for labels:<br>'; - reset ($this->unknown_labels); - while($a = each($this->unknown_labels)) - $debug_text .= $a[1]."<br>"; - - internal_error($debug_text); - } - else - return 0; + if ($this->unknown_labels) + internal_error('No text found for labels: ' . implode(',', $this->unknown_labels)); } @@ -342,6 +340,32 @@ function texify($text, $convertlinebreaks = true) 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('_' => $text['_'], $text); + + $oldmask = umask(002); + if ($f = fopen($this->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 */ @@ -349,15 +373,15 @@ function texify($text, $convertlinebreaks = true) * Globally available functions without need for object */ -/* +/** * 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 it_text_transmogrify($text, $values=array()) +function it_text_transmogrify($text, $values = null) { while (preg_match('/{([\w.]+)}/', $text, $regs)) { - $path = explode('.', $regs[1]); + $path = explode('.', $regs[1]); if ($values) $value =& $values; else @@ -382,7 +406,7 @@ function it_text_transmogrify($text, $values=array()) /* * Shortcut to $it_text->Text() */ -function T($label, $raw=0, $language='') +function T($label, $raw = null, $language = null) { return $GLOBALS['it_text']->text($label, $raw, $language); } @@ -391,7 +415,7 @@ function T($label, $raw=0, $language='') /* * Shortcut to $it_text->etext() */ -function ET($label, $values=null, $language='') +function ET($label, $values = null, $language = null) { return $GLOBALS['it_text']->etext($label, $values, $language); } @@ -407,7 +431,7 @@ function T_lang() /** * Shortcut to $it_text->get_language() */ -function T_set_language($language, $setcookie=true) +function T_set_language($language, $setcookie = true) { return $GLOBALS['it_text']->set_language($language, $setcookie); } @@ -415,8 +439,8 @@ function T_set_language($language, $setcookie=true) /** * Shortcut to $it_text->text_exists() */ -function T_exists($label) +function T_exists($label, $language = null) { - return $GLOBALS['it_text']->text_exists($label); + return $GLOBALS['it_text']->text_exists($label, $language); } ?> |