diff options
author | Christian Schneider | 2006-12-12 13:38:57 +0000 |
---|---|---|
committer | Christian Schneider | 2006-12-12 13:38:57 +0000 |
commit | fd9171e0df775da9c4a3ea22e8f95ec75d3b3897 (patch) | |
tree | 53e93e18851d1c780f73e5464b89b1aad87f598c /text.class | |
parent | ee2fcb5bbc9b9db282a4668c50d87447b2114886 (diff) | |
download | itools-fd9171e0df775da9c4a3ea22e8f95ec75d3b3897.tar.gz itools-fd9171e0df775da9c4a3ea22e8f95ec75d3b3897.tar.bz2 itools-fd9171e0df775da9c4a3ea22e8f95ec75d3b3897.zip |
Merged revisions 13944-15067 via svnmerge from
svn+ssh://rdc.rim.ch/rdc/svn/itools/devel-redesign
........
r13945 | weber | 2006-10-26 16:13:44 +0200 (Thu, 26 Oct 2006) | 2 lines
omit null attributes, true means add name only. select() supports optgroups
........
r13946 | weber | 2006-10-26 16:22:42 +0200 (Thu, 26 Oct 2006) | 2 lines
Removed cruft
........
r13989 | cschneid | 2006-10-27 18:32:11 +0200 (Fri, 27 Oct 2006) | 1 line
Removed more cruft
........
r13991 | cschneid | 2006-10-28 19:36:35 +0200 (Sat, 28 Oct 2006) | 1 line
Make adserver output xhtml compliant tags
........
r14093 | weber | 2006-11-06 17:13:23 +0100 (Mon, 06 Nov 2006) | 2 lines
title tag can be omitted completely
........
r14096 | weber | 2006-11-06 18:00:43 +0100 (Mon, 06 Nov 2006) | 2 lines
Q() doesn't encode newlines, tag() encodes newlines within attributes
........
r14135 | weber | 2006-11-09 02:30:03 +0100 (Thu, 09 Nov 2006) | 2 lines
Migrate DB to texts.php on the fly (this functionality will be removed later), use only statictext, not DB
........
r14164 | mueller | 2006-11-09 19:23:40 +0100 (Thu, 09 Nov 2006) | 1 line
disable empty options in select
........
r14194 | mueller | 2006-11-13 14:34:25 +0100 (Mon, 13 Nov 2006) | 1 line
imply choose() in show()
........
r14212 | weber | 2006-11-13 18:14:33 +0100 (Mon, 13 Nov 2006) | 2 lines
Debug output is left-aligned
........
r14231 | cschneid | 2006-11-14 17:09:32 +0100 (Tue, 14 Nov 2006) | 1 line
Load js files before dom is ready and prevent loops on static=
........
r14288 | cschneid | 2006-11-15 13:16:54 +0100 (Wed, 15 Nov 2006) | 1 line
More compact serialization of arrays
........
r14388 | weber | 2006-11-22 18:19:34 +0100 (Wed, 22 Nov 2006) | 2 lines
Add optional IE PNG fix to it_html::img()
........
r14442 | mueller | 2006-11-27 23:22:23 +0100 (Mon, 27 Nov 2006) | 1 line
correctly handle <option value="">
........
r14444 | cschneid | 2006-11-28 12:35:43 +0100 (Tue, 28 Nov 2006) | 1 line
Handle null separately to preserve 0 == '0'
........
r14450 | cschneid | 2006-11-28 15:36:45 +0100 (Tue, 28 Nov 2006) | 1 line
Style commit (mainly done to test commit-email)
........
r14451 | cschneid | 2006-11-28 15:44:49 +0100 (Tue, 28 Nov 2006) | 1 line
Test commit for commit-email
........
r14638 | cschneid | 2006-12-01 14:20:45 +0100 (Fri, 01 Dec 2006) | 1 line
Suppress warning in case src file was removed by race
........
r14970 | weber | 2006-12-09 03:07:22 +0100 (Sat, 09 Dec 2006) | 2 lines
Added the highly useful timerlog() method from searchlib to justify the supporting code in auto_append.php ;)
........
r14971 | weber | 2006-12-09 03:08:49 +0100 (Sat, 09 Dec 2006) | 2 lines
Added set(), made dump_php's return value more convincing
........
r14993 | cschneid | 2006-12-11 14:05:29 +0100 (Mon, 11 Dec 2006) | 1 line
Added itjs::send_headers() to send HTTP headers suitable for JSON data
........
r14997 | cschneid | 2006-12-11 14:28:52 +0100 (Mon, 11 Dec 2006) | 1 line
Set JSON expiration date for IE
........
r15057 | weber | 2006-12-12 14:02:43 +0100 (Tue, 12 Dec 2006) | 2 lines
documentation update
........
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); } ?> |