cvs: pear /Translation2 Container.php Decorator.php Translation2.php package.xml /Translation2/Container db.php gettext.php mdb.php
/Translation2/Decorator CacheMemory.php DefaultText.php Lang.php SpecialChars.php /Translation2/docs/examples Translation2_example.php
Translation2_example.sql

From: Date: Thu, 11 Mar 2004 17:06:55 +0000
Subject: cvs: pear /Translation2 Container.php Decorator.php Translation2.php package.xml /Translation2/Container db.php gettext.php mdb.php
/Translation2/Decorator CacheMemory.php DefaultText.php Lang.php SpecialChars.php /Translation2/docs/examples Translation2_example.php
Translation2_example.sql
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-17310@lists.php.net to get a copy of this message
quipo Thu Mar 11 12:06:55 2004 EDT Added files: /pear/Translation2 Decorator.php /pear/Translation2/Decorator CacheMemory.php DefaultText.php Lang.php SpecialChars.php Modified files: /pear/Translation2 Container.php Translation2.php package.xml /pear/Translation2/Container db.php gettext.php mdb.php /pear/Translation2/docs/examples Translation2_example.php Translation2_example.sql Log: refactoring in progress: - added Decorator base class; - added some decorators, that can be layered one on top of the other, allowing an indefinite number of cache layers, fallback languages and string manipulations; - getPage(null) will now get a string group with a NULL pageID; - getPage('') will now get a string group with an EMPTY pageID; - similarly for the other get*() methods - updated the example to show the new features

http://cvs.php.net/diff.php/pear/Translation2/Container.php?r1=1.3&r2=1.4&ty=u Index: pear/Translation2/Container.php diff -u pear/Translation2/Container.php:1.3 pear/Translation2/Container.php:1.4 --- pear/Translation2/Container.php:1.3 Sun Feb 15 17:45:00 2004 +++ pear/Translation2/Container.php Thu Mar 11 12:06:50 2004 @@ -16,7 +16,7 @@ // | Authors: Lorenzo Alberton <l dot alberton at quipo dot it> | // +----------------------------------------------------------------------+ // -// $Id: Container.php,v 1.3 2004/02/15 22:45:00 quipo Exp $ +// $Id: Container.php,v 1.4 2004/03/11 17:06:50 quipo Exp $ // /** @@ -170,7 +170,7 @@ * @param string $pageID * @return array */ - function getPage($pageID) + function getPage($pageID, $langID) { return $this->raiseError('method "getPage" not supported', TRANSLATION_ERROR_METHOD_NOT_SUPPORTED); http://cvs.php.net/diff.php/pear/Translation2/Translation2.php?r1=1.5&r2=1.6&ty=u Index: pear/Translation2/Translation2.php diff -u pear/Translation2/Translation2.php:1.5 pear/Translation2/Translation2.php:1.6 --- pear/Translation2/Translation2.php:1.5 Fri Mar 5 12:00:54 2004 +++ pear/Translation2/Translation2.php Thu Mar 11 12:06:50 2004 @@ -16,11 +16,11 @@ // | Authors: Lorenzo Alberton <l dot alberton at quipo dot it> | // +----------------------------------------------------------------------+ // -// $Id: Translation2.php,v 1.5 2004/03/05 17:00:54 quipo Exp $ +// $Id: Translation2.php,v 1.6 2004/03/11 17:06:50 quipo Exp $ // /** * @package Translation2 - * @version $Id: Translation2.php,v 1.5 2004/03/05 17:00:54 quipo Exp $ + * @version $Id: Translation2.php,v 1.6 2004/03/11 17:06:50 quipo Exp $ */ /** @@ -29,12 +29,14 @@ require_once 'PEAR.php'; /** - * Allows redefinition of alternate key for empty pageID + * Allows redefinition of the default pageID. + * This constant is needed to allow both NULL and EMPTY pageID values + * and to have them match + * */ -if (!defined('TRANSLATION2_EMPTY_PAGEID_KEY')) { - define('TRANSLATION2_EMPTY_PAGEID_KEY', 'array_key_4_empty_pageID'); +if (!defined('TRANSLATION2_DEFAULT_PAGEID')) { + define('TRANSLATION2_DEFAULT_PAGEID', 'translation2_default_pageID'); } - /** * Class Error codes */ @@ -64,13 +66,6 @@ var $options = array(); /** - * Translated strings array - * Used for cache purposes - * @var array - */ - var $data = array(); - - /** * Default lang * @var array */ @@ -83,12 +78,6 @@ var $currentPageID = null; /** - * Fallback lang - * @var array - */ - var $langFallback = array(); - - /** * Array parameters * @var array */ @@ -104,7 +93,9 @@ * @param mixed $options Additional options for the storage driver * (example: if you are using DB as the storage * driver, you have to pass the dsn string here) - * @param array $params + * @param array $params Array of parameters for the adapter class + * (i.e. you can set here the mappings between your + * table/field names and the ones used by this class) */ function Translation2($storageDriver, $options='', $params=array()) { @@ -184,34 +175,6 @@ function setLang($langID) { $this->lang = $this->storage->setLang($langID); - //prepare cache container - if (!array_key_exists($langID, $this->data)) { - $this->data[$langID] = array(); - } - } - - // }}} - // {{{ setLangFallback() - - /** - * Set default fallback lang - * - * When a string in the default lang is empty, - * fetch the string in the fallback language - * @param string $langID - */ - function setLangFallback($langID=null) - { - if (empty($langID)) { - //unset fallback language - $this->langFallback = array(); - } else { - $this->langFallback = $this->storage->getLangData($langID); - //prepare cache container - if (!array_key_exists($langID, $this->data)) { - $this->data[$langID] = array(); - } - } } // }}} @@ -219,16 +182,11 @@ /** * Set default page - * Prefetch strings for this page * @param string $langID */ function setPageID($pageID=null) { $this->currentPageID = $pageID; - if ($this->options['prefetch']) { - $key = empty($pageID) ? TRANSLATION2_EMPTY_PAGEID_KEY : $pageID; - $this->data[$this->lang['id']][$key] = $this->storage->getPage($pageID); - } } // }}} @@ -253,8 +211,8 @@ } elseif (isset($lang[$format])) { return $lang[$format]; } else { - return $lang['name']; -        } + return $lang['name']; + } } // }}} @@ -289,6 +247,61 @@ } // }}} + // {{{ _replaceParams() + + /** + * Replace parameters in strings + * @param mixed $params + * @access protected + */ + function _replaceParams($strings) + { + if (empty($strings) || is_object($strings) || !count($this->params)) { + return $strings; + } + if (is_array($strings)) { + foreach ($strings as $key => $string) { + $strings[$key] = $this->_replaceParams($string); + } + } else { + if (strpos($strings, $this->options['ParameterPrefix']) !== false) { + foreach ($this->params as $name => $value) { + $strings = str_replace($this->options['ParameterPrefix'] + . $name . $this->options['ParameterPostfix'], + $value, + $strings); + } + if ($this->options['ParameterAutoFree']) { + $this->params = array(); + } + } + } + return $strings; + } + + // }}} + // {{{ replaceEmptyStringsWithKeys() + + /** + * Replace empty strings with their stringID + * @param mixed $params + * @static + * @access public + */ + function replaceEmptyStringsWithKeys($strings) + { + if (!is_array($strings)) { + return $strings; + } + foreach ($strings as $key => $string) { + if (empty($string)) { + $strings[$key] = $key; + } + } + return $strings; + } + + // }}} // {{{ get() /** @@ -306,49 +319,11 @@ * the default and the fallback lang are empty * @return string */ - function get($stringID, $pageID=null, $langID=null, $defaultText='') + function get($stringID, $pageID=TRANSLATION2_DEFAULT_PAGEID, $langID=null, $defaultText='') { - if (is_null($pageID)) { - $pageID = $this->currentPageID; - } - if ($this->options['prefetch']) { - $this->getRawPage($pageID, $langID); - } - $pageID_key = empty($pageID) ? TRANSLATION2_EMPTY_PAGEID_KEY : $pageID; - $langID_key = empty($langID) ? $this->lang['id'] : $langID; - - if (!array_key_exists($langID_key, $this->data) || - !array_key_exists($pageID_key, $this->data[$langID_key]) - ) { - $str = $this->storage->getOne($stringID, $pageID, $langID); - } else { - if (!array_key_exists($stringID, $this->data[$langID_key][$pageID_key])) { - $str = ''; - } else { - $str = $this->data[$langID_key][$pageID_key][$stringID]; - } - } - - if (empty($str)) { - if ($langID != $this->langFallback['id']) { - return $this->get($stringID, $pageID, $this->langFallback['id'], $defaultText); - } else { - $str = empty($defaultText) ? $this->lang['error_text'] : $defaultText; - } - } - - if (count($this->params)) { - while (list($name, $value) = each($this->params)) { - $str = str_replace($this->options['ParameterPrefix'] - . $name . $this->options['ParameterPostfix'], - $value, - $str); - } - if ($this->options['ParameterAutoFree']) { - $this->params = array(); - } - } - return $str; + $pageID = ($pageID == TRANSLATION2_DEFAULT_PAGEID ? $this->currentPageID : $pageID); + $str = $this->storage->getOne($stringID, $pageID, $langID); + return $this->_replaceParams($str); } // }}} @@ -364,26 +339,10 @@ * @param string $langID * @return array */ - function getRawPage($pageID=null, $langID=null) + function getRawPage($pageID=TRANSLATION2_DEFAULT_PAGEID, $langID=null) { - if (is_null($pageID)) { - $pageID = $this->currentPageID; - } - $pageID_key = empty($pageID) ? TRANSLATION2_EMPTY_PAGEID_KEY : $pageID; - $langID_key = empty($langID) ? $this->lang['id'] : $langID; - - $notDefaultLang = (!is_null($langID) && ($langID != $this->lang['id'])) ? true : false; - if ($notDefaultLang) { - $bkp_lang = $this->lang['id']; - $this->setLang($langID); - } - if (!array_key_exists($pageID_key, $this->data[$langID_key])) { - $this->data[$langID_key][$pageID_key] = $this->storage->getPage($pageID); - } - if ($notDefaultLang) { - $this->setLang($bkp_lang); - } - return $this->data[$langID_key][$pageID_key]; + $pageID = ($pageID == TRANSLATION2_DEFAULT_PAGEID ? $this->currentPageID : $pageID); + return $this->storage->getPage($pageID, $langID); } // }}} @@ -397,15 +356,10 @@ * @param string $langID * @return array */ - function getPage($pageID=null, $langID=null, $defaultText='') + function getPage($pageID=TRANSLATION2_DEFAULT_PAGEID, $langID=null, $defaultText='') { $pageData = $this->getRawPage($pageID, $langID); - foreach ($pageData as $stringID => $string) { - if (empty($string) || strpos($string, $this->options['ParameterPrefix']) !== false) { - $pageData[$stringID] = $this->get($stringID, $pageID, $this->lang['id'], $defaultText); - } - } - return $pageData; + return $this->_replaceParams($pageData); } // }}} @@ -420,9 +374,9 @@ * @param string $langID * @return string */ - function translate($string, $langID, $pageID=null) + function translate($string, $langID, $pageID=TRANSLATION2_DEFAULT_PAGEID) { - //is a search in the cache (before the db query) worth it? + $pageID = ($pageID == TRANSLATION2_DEFAULT_PAGEID ? $this->currentPageID : $pageID); $stringID = $this->storage->getStringID($string, $pageID); if (PEAR::isError($stringID) || empty($stringID)) { return $this->lang['error_text']; http://cvs.php.net/diff.php/pear/Translation2/package.xml?r1=1.7&r2=1.8&ty=u Index: pear/Translation2/package.xml diff -u pear/Translation2/package.xml:1.7 pear/Translation2/package.xml:1.8 --- pear/Translation2/package.xml:1.7 Sun Feb 22 15:54:22 2004 +++ pear/Translation2/package.xml Thu Mar 11 12:06:50 2004 @@ -17,17 +17,17 @@ </maintainer> </maintainers> <release> - <version>2.0.0alpha2</version> - <date>2004-02-22</date> + <version>2.0.0alpha3</version> + <date>2004-03-22</date> <license>PHP License</license> <state>alpha</state> <notes> -- added an experimental GNU gettext driver -- translate() now accepts a third parameter ($pageID) -- PHP5 fix -- renamed old getPage() to getRawPage() -- new getPage() resorts to fallback lang and replaces parameters when needed -- added error checking/codes +- BC break! Run the example to see what's new +- refactoring in progress: + added a Decorator class and some subclasses to control + the output (now you can set a stack of fallback languages, + a filter to deal with empty strings, one or more cache layers...) +- improved gettext support (Michael Wallner) </notes> <deps>       <dep type="pkg" rel="has" optional="yes">DB</dep> @@ -38,22 +38,30 @@ <dir name="/" baseinstalldir="Translation2"> <file role="php" name="Translation2.php" /> <file role="php" name="Container.php" /> + <file role="php" name="Decorator.php" /> <file role="php" name="Utils.php" /> <dir name="Admin" role="php"> - <file name="Admin.php" /> - <file name="Container/db.php" /> - <file name="Container/mdb.php" /> + <file role="php" name="Admin.php" /> + <file role="php" name="Container/db.php" /> + <file role="php" name="Container/mdb.php" /> </dir> <dir name="Container" role="php"> <file name="db.php" /> <file name="gettext.php" /> <file name="mdb.php" /> </dir> + <dir name="Decorator" role="php"> + <file name="CacheMemory.php" /> + <file name="DefaultText.php" /> + <file name="Lang.php" /> + <file name="SpecialChars.php" /> + </dir> <dir name="docs" role="doc"> <file name="gettext_readme.txt" /> <dir name="examples" role="doc"> - <file name="Translation2_example.php" /> - <file name="Translation2_example.sql" /> + <file role="doc" name="settings.php" /> + <file role="doc" name="Translation2_example.php" /> + <file role="doc" name="Translation2_example.sql" /> </dir> </dir> </dir> @@ -61,15 +69,29 @@ </release> <changelog> <release> + <version>2.0.0alpha3</version> + <date>2004-03-22</date> + <license>PHP License</license> + <state>alpha</state> + <notes> +- refactoring in progress: + added a Decorator class and some subclasses to control + the output (now you can set a stack of fallback languages, + a filter to deal with empty strings, one or more cache layers, + and define your own decorators...) +- improved gettext support (Michael Wallner) + </notes> + </release> + <release> <version>2.0.0alpha2</version> - <date>2004-02-22</date> + <date>2004-02-05</date> <license>PHP License</license> <state>alpha</state> <notes> - added an experimental GNU gettext driver - translate() now accepts a third parameter ($pageID) - PHP5 fix -- renamed old getPage() to getRawPage() +- renamed old getPage() to getRawPage() - new getPage() resorts to fallback lang and replaces parameters when needed - added error checking/codes </notes> http://cvs.php.net/diff.php/pear/Translation2/Container/db.php?r1=1.3&r2=1.4&ty=u Index: pear/Translation2/Container/db.php diff -u pear/Translation2/Container/db.php:1.3 pear/Translation2/Container/db.php:1.4 --- pear/Translation2/Container/db.php:1.3 Sun Feb 15 17:47:22 2004 +++ pear/Translation2/Container/db.php Thu Mar 11 12:06:53 2004 @@ -16,7 +16,7 @@ // | Author: Lorenzo Alberton <l dot alberton at quipo dot it> | // +----------------------------------------------------------------------+ // -// $Id: db.php,v 1.3 2004/02/15 22:47:22 quipo Exp $ +// $Id: db.php,v 1.4 2004/03/11 17:06:53 quipo Exp $ // require_once 'Translation2'.DIRECTORY_SEPARATOR.'Container.php'; @@ -29,7 +29,7 @@ * by the PEAR DB abstraction layer to fetch data. * * @package Translation2 - * @version $Revision: 1.3 $ + * @version $Revision: 1.4 $ */ class Translation2_Container_db extends Translation2_Container { @@ -225,29 +225,34 @@ * NB: table names will be customizable via an option... * * @param string $pageID + * @param string $langID * @return array */ - function getPage($pageID=null) + function getPage($pageID=null, $langID=null) { - $lang_col = str_replace('%s', $this->currentLang['id'], $this->options['string_text_col']); + if (is_null($langID)) { + $langID = $this->currentLang['id']; + } + $lang_col = str_replace('%s', $langID, $this->options['string_text_col']); if (empty($lang_col)) { - $lang_col = $this->currentLang['id']; + $lang_col = $langID; } $query = sprintf('SELECT %s, %s FROM %s', $this->options['string_id_col'], $lang_col, - $this->options['strings_tables'][$this->currentLang['id']]); + $this->options['strings_tables'][$langID]); $where = array(); if (!empty($pageID)) { - $where[] = $this->options['strings_tables'][$this->currentLang['id']]. '.' . + $where[] = $this->options['strings_tables'][$langID]. '.' . $this->options['string_page_id_col']. '=' . $this->db->quote($pageID); - } elseif (!is_null($pageID)) { - $where[] = $this->options['strings_tables'][$this->currentLang['id']]. '.' . + } elseif (is_null($pageID)) { + $where[] = $this->options['strings_tables'][$langID]. '.' . $this->options['string_page_id_col']. ' IS NULL'; + } else { + $where[] = $this->options['strings_tables'][$langID]. '.' . + $this->options['string_page_id_col']. '=""'; } - if (count($where)) { - $query .= ' WHERE ' .implode(' AND ', $where); - } + $query .= ' WHERE ' .implode(' AND ', $where); $res = $this->query($query); if (PEAR::isError($res)) { return $res; @@ -287,9 +292,12 @@ if (!empty($pageID)) { $where[] = $this->options['strings_tables'][$langID]. '.' . $this->options['string_page_id_col']. '='. $this->db->quote($pageID); - } elseif (!is_null($pageID)) { + } elseif (is_null($pageID)) { $where[] = $this->options['strings_tables'][$this->currentLang['id']]. '.' . $this->options['string_page_id_col']. ' IS NULL'; + } else { + $where[] = $this->options['strings_tables'][$langID]. '.' . + $this->options['string_page_id_col']. '=""'; } $where[] = $this->options['strings_tables'][$langID]. '.' . $this->options['string_id_col'] .'='. $this->db->quote($stringID); @@ -328,11 +336,14 @@ $this->db->quote($string) ); if (!empty($pageID)) { - $query .= ' AND '.$this->options['strings_tables'][$langID]. '.' + $query .= ' AND '.$this->options['strings_tables'][$this->currentLang['id']]. '.' . $this->options['string_page_id_col']. '='. $this->db->quote($pageID); - } elseif (!is_null($pageID)) { + } elseif (is_null($pageID)) { $query .= ' AND '.$this->options['strings_tables'][$this->currentLang['id']]. '.' . $this->options['string_page_id_col']. ' IS NULL'; + } else { + $query .= ' AND '.$this->options['strings_tables'][$this->currentLang['id']]. '.' . + $this->options['string_page_id_col']. '=""'; } return $this->query($query, 'getOne'); } http://cvs.php.net/diff.php/pear/Translation2/Container/gettext.php?r1=1.3&r2=1.4&ty=u Index: pear/Translation2/Container/gettext.php diff -u pear/Translation2/Container/gettext.php:1.3 pear/Translation2/Container/gettext.php:1.4 --- pear/Translation2/Container/gettext.php:1.3 Fri Mar 5 12:02:56 2004 +++ pear/Translation2/Container/gettext.php Thu Mar 11 12:06:53 2004 @@ -16,7 +16,7 @@ // | Author: Lorenzo Alberton <l dot alberton at quipo dot it> | // +----------------------------------------------------------------------+ // -// $Id: gettext.php,v 1.3 2004/03/05 17:02:56 quipo Exp $ +// $Id: gettext.php,v 1.4 2004/03/11 17:06:53 quipo Exp $ // require_once 'Translation2'.DIRECTORY_SEPARATOR.'Container.php'; @@ -26,7 +26,7 @@ * * @see /docs/gettext_readme.txt for an usage example * @package Translation2 - * @version $Revision: 1.3 $ + * @version $Revision: 1.4 $ */ class Translation2_Container_gettext extends Translation2_Container { @@ -72,7 +72,7 @@ function Translation2_Container_gettext($options) { $this->_setDefaults(); - $this->_parseOptions($options); + //$this->_parseOptions($options); $this->_domains = @parse_ini_file($this->options['domains_path_file']); if ($this->_domains === false) { @@ -170,7 +170,7 @@ * @param string $pageID * @return array */ - function getPage($pageID=null) + function getPage($pageID=null, $langID=null) { if (array_key_exists($this->currentLang['id'], $this->cachedDomains) && array_key_exists($pageID, $this->cachedDomains[$this->currentLang['id']]) http://cvs.php.net/diff.php/pear/Translation2/Container/mdb.php?r1=1.3&r2=1.4&ty=u Index: pear/Translation2/Container/mdb.php diff -u pear/Translation2/Container/mdb.php:1.3 pear/Translation2/Container/mdb.php:1.4 --- pear/Translation2/Container/mdb.php:1.3 Sun Feb 15 17:47:22 2004 +++ pear/Translation2/Container/mdb.php Thu Mar 11 12:06:53 2004 @@ -16,7 +16,7 @@ // | Author: Lorenzo Alberton <l dot alberton at quipo dot it> | // +----------------------------------------------------------------------+ // -// $Id: mdb.php,v 1.3 2004/02/15 22:47:22 quipo Exp $ +// $Id: mdb.php,v 1.4 2004/03/11 17:06:53 quipo Exp $ // require_once 'Translation2'.DIRECTORY_SEPARATOR.'Container.php'; @@ -29,7 +29,7 @@ * by the PEAR MDB abstraction layer to fetch data. * * @package Translation2 - * @version $Revision: 1.3 $ + * @version $Revision: 1.4 $ */ class Translation2_Container_mdb extends Translation2_Container { @@ -163,7 +163,7 @@ $this->options['lang_meta_col'] = 'meta'; $this->options['lang_errmsg_col'] = 'error_text'; - $this->options['strings_tables'] = array(); // 'lang_id' => 'table_name' + $this->options['strings_tables'] = array(); // 'lang_id' => 'table_name' $this->options['string_id_col'] = 'ID'; $this->options['string_page_id_col'] = 'page_id'; $this->options['string_text_col'] = '%s'; // col_name if one table per lang is used, @@ -218,29 +218,34 @@ * NB: table names will be customizable via an option... * * @param string $pageID + * @param string $langID * @return array */ - function getPage($pageID=null) + function getPage($pageID=null, $langID=null) { - $lang_col = str_replace('%s', $this->currentLang['id'], $this->options['string_text_col']); + if (is_null($langID)) { + $langID = $this->currentLang['id']; + } + $lang_col = str_replace('%s', $langID, $this->options['string_text_col']); if (empty($lang_col)) { - $lang_col = $this->currentLang['id']; + $lang_col = $langID; } $query = sprintf('SELECT %s, %s FROM %s', $this->options['string_id_col'], $lang_col, - $this->options['strings_tables'][$this->currentLang['id']]); + $this->options['strings_tables'][$langID]); $where = array(); if (!empty($pageID)) { - $where[] = $this->options['strings_tables'][$this->currentLang['id']]. '.' . + $where[] = $this->options['strings_tables'][$langID]. '.' . $this->options['string_page_id_col']. '=' . $this->db->getTextValue($pageID); - } elseif (!is_null($pageID)) { - $where[] = $this->options['strings_tables'][$this->currentLang['id']]. '.' . + } elseif (is_null($pageID)) { + $where[] = $this->options['strings_tables'][$langID]. '.' . $this->options['string_page_id_col']. ' IS NULL'; + } else { + $where[] = $this->options['strings_tables'][$langID]. '.' . + $this->options['string_page_id_col']. '=""'; } - if (count($where)) { - $query .= ' WHERE ' .implode(' AND ', $where); - } + $query .= ' WHERE ' .implode(' AND ', $where); $res = $this->query($query); if (PEAR::isError($res)) { return $res; @@ -280,9 +285,12 @@ if (!empty($pageID)) { $where[] = $this->options['strings_tables'][$langID]. '.' . $this->options['string_page_id_col']. '='. $this->db->getTextValue($pageID); - } elseif (!is_null($pageID)) { + } elseif (is_null($pageID)) { $where[] = $this->options['strings_tables'][$this->currentLang['id']]. '.' . $this->options['string_page_id_col']. ' IS NULL'; + } else { + $where[] = $this->options['strings_tables'][$langID]. '.' . + $this->options['string_page_id_col']. '=""'; } $where[] = $this->options['strings_tables'][$langID]. '.' . $this->options['string_id_col'] .'='. $this->db->getTextValue($stringID); @@ -321,11 +329,14 @@ $this->db->getTextValue($string) ); if (!empty($pageID)) { - $query .= ' AND '.$this->options['strings_tables'][$langID]. '.' + $query .= ' AND '.$this->options['strings_tables'][$this->currentLang['id']]. '.' . $this->options['string_page_id_col']. '='. $this->db->getTextValue($pageID); - } elseif (!is_null($pageID)) { + } elseif (is_null($pageID)) { $query .= ' AND '.$this->options['strings_tables'][$this->currentLang['id']]. '.' . $this->options['string_page_id_col']. ' IS NULL'; + } else { + $query .= ' AND '.$this->options['strings_tables'][$this->currentLang['id']]. '.' . + $this->options['string_page_id_col']. '=""'; } return $this->query($query, 'getOne'); } http://cvs.php.net/diff.php/pear/Translation2/docs/examples/Translation2_example.php?r1=1.1&r2=1.2&ty=u Index: pear/Translation2/docs/examples/Translation2_example.php diff -u pear/Translation2/docs/examples/Translation2_example.php:1.1 pear/Translation2/docs/examples/Translation2_example.php:1.2 --- pear/Translation2/docs/examples/Translation2_example.php:1.1 Sun Feb 15 17:43:17 2004 +++ pear/Translation2/docs/examples/Translation2_example.php Thu Mar 11 12:06:53 2004 @@ -1,104 +1,142 @@ <?php -/************************************* - * helper methods: - * debug(), writeTitle(), writeValue() - *************************************/ -function debug($str='') -{ - echo '<pre><div style="background-color: #ccffcc; border: 1px solid red; padding-left: 4px;">'; - print_r($str); - echo '</div></pre>'; -} -function writeTitle($str='') -{ - echo '<br /> <h2 style="padding: 5px; background-color: #ccccff; border: 1px solid black;">'.$str.'</h2>'; -} -function writeValue($desc='', $var) -{ - echo '<div style="background-color: #f8f8f8; border: 1px solid #ccc; margin: 4px; padding: 4px;">'. $desc .' = '; - var_dump($var); - echo '</div>'; -} - - /** - * require class, set parameters and options + * require file with settings and Translation2 class, + * set parameters and options */ -require_once 'Translation2/Translation2.php'; - -define('TABLE_PREFIX', 'mytable_'); - -$dbinfo = array( - 'hostspec' => 'myhost', - 'database' => 'mydb', - 'phptype' => 'mysql', - 'username' => 'myuid', - 'password' => 'mypwd' -); - -$params = array( - 'langs_avail_table' => TABLE_PREFIX.'langs_avail', - 'lang_id_col' => 'ID', - 'lang_name_col' => 'name', - 'lang_meta_col' => 'meta', - 'lang_errmsg_col' => 'error_text', - 'strings_tables' => array( - 'en' => TABLE_PREFIX.'i18n', - 'it' => TABLE_PREFIX.'i18n' - ), - 'string_id_col' => 'ID', - 'string_page_id_col' => 'pageID', - 'string_text_col' => '%s', - //'prefetch' => false //more queries, smaller result sets - //(use when db load is cheaper than network load) -); - +require_once './settings.php'; $tr = new Translation2('MDB', $dbinfo, $params); if (PEAR::isError($tr)) { debug($tr); } - - writeTitle('ITALIANO'); $tr->setLang('it'); -$tr->setLangFallback('en'); $tr->setPageID(); -debug('setLang(\'it\');'); -debug('setLangFallback(\'en\');'); -debug('setPageID(); //get global strings'); -debug('get(\'month_01\');'); -writeValue('month_01', $tr->get('month_01')); -debug('get(\'only_english\'); //test fallback language'); +$tr = &new Translation2_Decorator_CacheMemory(&$tr); +//$tr->prefetch = false; +$tr = &new Translation2_Decorator_Lang(&$tr); +$tr->setDecoratedLang('en'); +$tr = &new Translation2_Decorator_Lang(&$tr); +$tr->setDecoratedLang('de'); + + +// =[DEBUG INFO]====================================== +$str = <<<EOT +// new Translation2 instance +// (look at settings.php for an example of \$dbinfo and \$params) +\$tr = new Translation2('MDB', \$dbinfo, \$params); + +// set Italian as default lang +\$tr->setLang('it'); + +// get global strings (pageID = NULL) +\$tr->setPageID(); + +// add a 'CacheMemory Decorator', i.e. add a memory-cache layer +// to avoid multiple queries to the db +\$tr = & new Translation2_Decorator_CacheMemory(&\$tr); + +// set an 'English Decorator', i.e. add English as a fallback language +\$tr = &new Translation2_Decorator_Lang(&\$tr); +\$tr->setDecoratedLang('en'); + +// add a 'German Decorator', i.e. add German as a third fallback language +\$tr = &new Translation2_Decorator_Lang(&\$tr); +\$tr->setDecoratedLang('en'); +EOT; +// ==================================================== +debug($str); + + +//$tr = &new Translation2_Decorator_CacheMemory(&$tr); +//$tr->prefetch = false; +//$tr = &new Translation2_Decorator_CacheLiteFunction(&$tr); +//$tr->setCacheLite(new Cache_Lite_Function($cache_options)); + +debug('$tr->get(\'test\');'); +writeValue('test', $tr->get('test')); +debug('$tr->get(\'only_english\'); //test fallback language for a string not translated in Italian'); writeValue('only_english', $tr->get('only_english')); -debug('getPage();'); -writeValue('all the page', $tr->getPage()); +debug('$tr->getRawPage();'); +writeValue('all the page (raw)', $tr->getRawPage()); +debug('$tr->getPage();'); +writeValue('all the page (with fallback langs)', $tr->getPage()); + +//------------------------------------------------------- writeTitle('GET LANG INFO'); -debug('getLang(); //no langID => get current lang'); +debug('$tr->getLang(); //no langID => get current lang'); writeValue('[IT] LANG_NAME', $tr->getLang()); //no langID => get current lang -debug('getLang(\'it\', \'error_text\'); //use 2nd parameter to choose the lang info you need'); +debug('$tr->getLang(\'it\', \'error_text\'); //use 2nd parameter to choose the lang info you need'); writeValue('[IT] LANG_ERRTXT', $tr->getLang('it', 'error_text')); -debug('getLang(\'it\', \'meta\'); //use 2nd parameter to choose the lang info you need'); +debug('$tr->getLang(\'it\', \'meta\'); //use 2nd parameter to choose the lang info you need'); writeValue('[EN] LANG_META', $tr->getLang('it', 'meta')); -debug('getLang(\'en\'); //default format is \'name\''); +debug('$tr->getLang(\'en\'); //default format is \'name\''); writeValue('[EN] LANG_NAME', $tr->getLang('en')); +debug('$tr->getLang(\'en\', \'error_text\');'); writeValue('[EN] LANG_ERRTXT', $tr->getLang('en', 'error_text')); +debug('$tr->getLang(\'en\', \'meta\');'); writeValue('[EN] LANG_META', $tr->getLang('en', 'meta')); +//------------------------------------------------------- + + +writeTitle('DEBUG INFO'); +debug('NUMBER OF DB QUERIES: '.$tr->storage->_queries); +unset($tr); + + +//------------------------------------------------------- + +//new example writeTitle('ENGLISH'); +$tr = new Translation2('MDB', $dbinfo, $params); $tr->setLang('en'); -$tr->setLangFallback('it'); -debug('setLang(\'en\');'); -debug('setLangFallback(\'it\');'); -writeValue('month_01', $tr->get('month_01')); -debug('get(\'only_italian\'); //test fallback language'); +$tr->setPageID(); +$tr = &new Translation2_Decorator_CacheMemory(&$tr); +//$tr->prefetch = false; +$tr = &new Translation2_Decorator_Lang(&$tr); +$tr->setDecoratedLang('it'); + + +// =[DEBUG INFO]====================================== +$str = <<<EOT +// new Translation2 instance +\$tr = new Translation2('MDB', \$dbinfo, \$params); + +// set English as default lang +\$tr->setLang('en'); + +// get global strings (empty pageID) +\$tr->setPageID(); + +// add a 'CacheMemory Decorator', i.e. add a memory-cache layer +// to avoid multiple queries to the db +\$tr = & new Translation2_Decorator_CacheMemory(&\$tr); + +// set an 'Italian Decorator', i.e. add Italian as a fallback language +\$tr = &new Translation2_Decorator_Lang(&\$tr); +\$tr->setDecoratedLang('it'); +EOT; +// ==================================================== +debug($str); + + +debug('$tr->get(\'test\');'); +writeValue('test', $tr->get('test')); +debug('get(\'only_italian\'); //test fallback language for a string not translated in English'); writeValue('only_italian', $tr->get('only_italian')); -writeValue('all the page', $tr->getPage()); +debug('getRawPage();'); +writeValue('all the page (raw)', $tr->getRawPage()); +debug('getPage();'); +writeValue('all the page (with fallback langs)', $tr->getPage()); + + +//------------------------------------------------------- writeTitle('TEST PARAMETER SUBSTITUTION'); @@ -106,42 +144,164 @@ 0 => '', 'user' => 'Joe', 'day' => '15', - 'month' => $tr->get('month_01', '', 'en'), + 'month' => $tr->get('month_01', 'calendar', 'en'), + 'year' => '2004', + 'weekday' => $tr->get('day_5', 'calendar', 'en') +)); +// =[DEBUG INFO]====================================== +$str = <<<EOT +\$tr->setParams(array( + 0 => '', + 'user' => 'Joe', + 'day' => '15', + 'month' => \$tr->get('month_01', 'calendar', 'en'), 'year' => '2004', - 'weekday' => $tr->get('day_5', '', 'en') + 'weekday' => \$tr->get('day_5', 'calendar', 'en') )); +EOT; +// ==================================================== +debug($str); + +debug('$tr->get(\'hello_user\');'); writeValue('[EN] hello, user', $tr->get('hello_user')); -$tr->setLang('it'); -$tr->setLangFallback('en'); + +$tr->setLang('it'); +$tr->setDecoratedLang('en'); $tr->setParams(array( 0 => '', 'user' => 'Joe', 'day' => '15', - 'month' => $tr->get('month_01', '', 'it'), - 'year' => '2004', - 'weekday' => $tr->get('day_5', '', 'it') + 'month' => $tr->get('month_01', 'calendar'), + 'year' => '2004', + 'weekday' => $tr->get('day_5', 'calendar') )); +// =[DEBUG INFO]====================================== +$str = <<<EOT +\$tr->setLang('it'); +\$tr->setDecoratedLang('en'); +\$tr->setParams(array( + 0 => '', + 'user' => 'Joe', + 'day' => '15', + 'month' => \$tr->get('month_01', 'calendar', 'it'), + 'year' => '2004', + 'weekday' => \$tr->get('day_5', 'calendar', 'it') +)); +EOT; +// ==================================================== +debug($str); writeValue('[IT] hello, user', $tr->get('hello_user')); + +//------------------------------------------------------- + + +writeTitle('SPECIAL CHARS DECORATOR'); +$tr = &new Translation2_Decorator_SpecialChars(&$tr); + +// =[DEBUG INFO]====================================== +$str = <<<EOT +// set a 'SpecialChars Decorator' to replace htmlentities +\$tr = &new Translation2_Decorator_SpecialChars(&\$tr); +\$tr->setOptions(array('charset' => 'ISO-8859-1'); //default +EOT; +// ==================================================== +debug($str); +debug('$tr->get(\'day_5\', \'calendar\', \'it\');'); + +writeValue('venerdì', $tr->get('day_5', 'calendar', 'it')); + + +//------------------------------------------------------- + + writeTitle('TRANSLATION (STRING TO STRING)'); -writeValue('gennaio', $tr->translate('gennaio', 'en')); +debug('$tr->translate(\'gennaio\', \'en\', \'calendar\');'); +writeValue('gennaio', $tr->translate('gennaio', 'en', 'calendar')); + + + + + +//------------------------------------------------------- + writeTitle('TEST STRINGS WITH pageID NOT NULL'); +debug('$tr->get(\'alone\', \'alone\');'); writeValue('[IT] alone', $tr->get('alone', 'alone')); +debug('$tr->get(\'alone\', \'alone\', \'en\');'); +writeValue('[EN] alone', $tr->get('alone', 'alone', 'en')); + + +//------------------------------------------------------- + + +writeTitle('HANDLE CONFLICTS'); $tr->setLang('en'); -$tr->setLangFallback('it'); -writeValue('[EN] alone', $tr->get('alone', 'alone')); +$tr->setDecoratedLang('it'); +$tr->setPageID('in_page'); + +// =[DEBUG INFO]====================================== +$str = <<<EOT +\$tr->setLang('en'); +\$tr->setDecoratedLang('it'); +\$tr->setPageID('in_page'); +EOT; +// ==================================================== +debug($str); + +debug('$tr->get(\'prova_conflitto\'); //pageID=TRANSLATION2_DEFAULT_PAGEID => get current pageID'); +writeValue('[EN] (in page) string', $tr->get('prova_conflitto')); +debug('$tr->get(\'prova_conflitto\', null); //pageID=null => get strings with pageID = NULL'); +writeValue('[EN] (global) string', $tr->get('prova_conflitto', null)); +debug('$tr->get(\'prova_conflitto\', \'in_page\'); //force pageID'); +writeValue('[EN] (in page) string', $tr->get('prova_conflitto', 'in_page')); + + +//------------------------------------------------------- + + +writeTitle('USE A DefaultText DECORATOR TO DEAL WITH EMPTY STRINGS'); +$tr = &new Translation2_Decorator_DefaultText(&$tr); + +// =[DEBUG INFO]====================================== +$str = <<<EOT +\$tr = &new Translation2_Decorator_DefaultText(&$tr); +EOT; +// ==================================================== + +debug('$tr->get(\'isempty\'); //get stringID when the string is empty'); +writeValue('[EN] empty string', $tr->get('isempty')); + +debug('$tr->get(\'isempty\', null, \'en\', \'show this default text\'); //use a custom fallback text'); +writeValue('[EN] empty string', $tr->get('isempty', null, 'en', 'show this default text')); + + + +/* writeTitle('Use error_text when default and fallback lang and defaultText are EMPTY'); writeValue('[EN] empty string', $tr->get('isempty')); +*/ -$tr->setPageID('in_page'); -writeTitle('HANDLE CONFLICTS'); -writeValue('[EN] (in page) string', $tr->get('prova_conflitto')); //pageID=null => get current pageID -writeValue('[EN] (global) string', $tr->get('prova_conflitto', '')); //pageID='' => get strings with no pageID -writeValue('[EN] (in page) string', $tr->get('prova_conflitto', 'in_page')); // => force pageID + + +if (strtolower(get_class($tr)) == 'translation2_admin') { + + writeTitle('TEST ADMIN'); + $res = $tr->add('smallTest', null, array('it' => 'piccolo test', + 'en' => 'small test') + ); + writeValue('add(smallTest)', $res); + + $res = $tr->add('smallTest', null, array('de' => 'kinder')); + writeValue('add(smallTest)', $res); + + $res = $tr->remove('smallTest', null); + writeValue('remove(smallTest)', $res); +} writeTitle('DEBUG INFO'); http://cvs.php.net/diff.php/pear/Translation2/docs/examples/Translation2_example.sql?r1=1.1&r2=1.2&ty=u Index: pear/Translation2/docs/examples/Translation2_example.sql diff -u pear/Translation2/docs/examples/Translation2_example.sql:1.1 pear/Translation2/docs/examples/Translation2_example.sql:1.2 --- pear/Translation2/docs/examples/Translation2_example.sql:1.1 Sun Feb 15 17:43:17 2004 +++ pear/Translation2/docs/examples/Translation2_example.sql Thu Mar 11 12:06:53 2004 @@ -13,32 +13,34 @@ -INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("month_01", NULL, "january", NULL, NULL, NULL, "gennaio"); -INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("month_02", NULL, "february", NULL, NULL, NULL, "febbraio"); -INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("month_03", NULL, "march", NULL, NULL, NULL, "marzo"); -INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("month_04", NULL, "april", NULL, NULL, NULL, "aprile"); -INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("month_05", NULL, "may", NULL, NULL, NULL, "maggio"); -INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("month_06", NULL, "june", NULL, NULL, NULL, "giugno"); -INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("month_07", NULL, "july", NULL, NULL, NULL, "luglio"); -INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("month_08", NULL, "august", NULL, NULL, NULL, "agosto"); -INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("month_09", NULL, "september", NULL, NULL, NULL, "settembre"); -INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("month_10", NULL, "october", NULL, NULL, NULL, "ottobre"); -INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("month_11", NULL, "november", NULL, NULL, NULL, "novembre"); -INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("month_12", NULL, "december", NULL, NULL, NULL, "dicembre"); -INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("day_0", NULL, "sunday", NULL, NULL, NULL, "domenica"); -INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("day_1", NULL, "monday", NULL, NULL, NULL, "lunedì"); -INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("day_2", NULL, "tuesday", NULL, NULL, NULL, "martedì"); -INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("day_3", NULL, "wednesday", NULL, NULL, NULL, "mercoledì"); -INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("day_4", NULL, "thursday", NULL, NULL, NULL, "giovedì"); -INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("day_5", NULL, "friday", NULL, NULL, NULL, "venerdì"); -INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("day_6", NULL, "saturday", NULL, NULL, NULL, "sabato"); -INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("only_english", NULL, "only english text", NULL, NULL, NULL, NULL); -INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("only_italian", NULL, NULL, NULL, NULL, NULL, "testo solo in italiano"); -INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("hello_user", NULL, "hello &&user&&, today is &&weekday&&, &&day&&th &&month&& &&year&&", NULL, NULL, NULL, "ciao, &&user&&, oggi è il &&day&& &&month&& &&year&& (&&weekday&&)"); -INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("alone", "alone", "all alone", NULL, NULL, NULL, "solo soletto"); -INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("isempty", NULL, NULL, NULL, NULL, NULL, NULL); -INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("prova_conflitto", "in_page", "conflicting text - in page", NULL, NULL, NULL, "testo con conflitto - in page"); -INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("prova_conflitto", NULL, "conflicting text - Global", NULL, NULL, NULL, "testo con conflitto - globale"); +INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("month_01", "calendar", "january", NULL, NULL, NULL, "gennaio"); +INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("month_02", "calendar", "february", NULL, NULL, NULL, "febbraio"); +INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("month_03", "calendar", "march", NULL, NULL, NULL, "marzo"); +INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("month_04", "calendar", "april", NULL, NULL, NULL, "aprile"); +INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("month_05", "calendar", "may", NULL, NULL, NULL, "maggio"); +INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("month_06", "calendar", "june", NULL, NULL, NULL, "giugno"); +INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("month_07", "calendar", "july", NULL, NULL, NULL, "luglio"); +INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("month_08", "calendar", "august", NULL, NULL, NULL, "agosto"); +INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("month_09", "calendar", "september", NULL, NULL, NULL, "settembre"); +INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("month_10", "calendar", "october", NULL, NULL, NULL, "ottobre"); +INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("month_11", "calendar", "november", NULL, NULL, NULL, "novembre"); +INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("month_12", "calendar", "december", NULL, NULL, NULL, "dicembre"); +INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("day_0", "calendar", "sunday", NULL, NULL, NULL, "domenica"); +INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("day_1", "calendar", "monday", NULL, NULL, NULL, "lunedì"); +INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("day_2", "calendar", "tuesday", NULL, NULL, NULL, "martedì"); +INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("day_3", "calendar", "wednesday", NULL, NULL, NULL, "mercoledì"); +INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("day_4", "calendar", "thursday", NULL, NULL, NULL, "giovedì"); +INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("day_5", "calendar", "friday", NULL, NULL, NULL, "venerdì"); +INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("day_6", "calendar", "saturday", NULL, NULL, NULL, "sabato"); +INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("only_english", NULL, "only english text", NULL, NULL, NULL, NULL); +INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("only_italian", NULL, NULL, NULL, NULL, NULL, "testo solo in italiano"); +INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("hello_user", NULL, "hello &&user&&, today is &&weekday&&, &&day&&th &&month&& &&year&&", NULL, NULL, NULL, "ciao, &&user&&, oggi è il &&day&& &&month&& &&year&& (&&weekday&&)"); +INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("alone", "alone", "all alone", NULL, NULL, NULL, "solo soletto"); +INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("isempty", NULL, NULL, "this string is empty in English and Italian, but not in German!", NULL, NULL, NULL); +INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("prova_conflitto", "in_page", "conflicting text - in page", NULL, NULL, NULL, "testo con conflitto - in page"); +INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("prova_conflitto", NULL, "conflicting text - Global", NULL, NULL, NULL, "testo con conflitto - globale"); +INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("pageid_vuoto", "", "string with empty pageID (i.e. NOT NULL)", NULL, NULL, NULL, "stringa con pageID vuoto ma non nullo"); +INSERT INTO mytable_i18n (ID, pageID, en, de, es, fr, it) VALUES("test", NULL, "this is a test string", NULL, NULL, NULL, "stringa di prova"); CREATE TABLE mytable_langs_avail ( @@ -49,5 +51,6 @@ UNIQUE KEY ID (ID) ); -INSERT INTO mytable_langs_avail (ID, name, meta, error_text) VALUES("it", "italiano", "charset=iso-8859-1", "non disponibile"); INSERT INTO mytable_langs_avail (ID, name, meta, error_text) VALUES("en", "english", "my meta info", "not available"); +INSERT INTO mytable_langs_avail (ID, name, meta, error_text) VALUES("de", "deutsch", "iso-8859-1", "kein Text auf Deutsch verfügbar"); +INSERT INTO mytable_langs_avail (ID, name, meta, error_text) VALUES("it", "italiano", "charset=iso-8859-1", "non disponibile"); http://cvs.php.net/co.php/pear/Translation2/Decorator.php?r=1.1&p=1 Index: pear/Translation2/Decorator.php +++ pear/Translation2/Decorator.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Lorenzo Alberton <l dot alberton at quipo dot it> | // +----------------------------------------------------------------------+ // // $Id: Decorator.php,v 1.1 2004/03/11 17:06:50 quipo Exp $ // /** * @package Translation2 * @version $Id: Decorator.php,v 1.1 2004/03/11 17:06:50 quipo Exp $ */ /** * Decorates a Translation2 class. * Create a subclass of this class for your own "decoration". * @abstract * @package Translation2 */ class Translation2_Decorator { // {{{ class vars /** * Translation2 object being decorated * @var object * @access protected */ var $translation2; /** * set prefetch on/off * @var boolean * @access protected */ var $prefetch = true; /** * Translated strings array * Used for cache purposes. * No parameter substitution or fallback langs here. * @var array * @access protected */ var $rawData = array(); /** * Translated strings array * Used for cache purposes. * @var array * @access protected */ var $data = array(); /** * Used for debug only. * @var object * @access protected */ var $storage; /** * @var array * @access protected */ var $lang; /** * decorated lang * @var string * @access protected */ var $currentLang; /** * @var string * @access protected */ var $currentPageID; /** * @var string * @access protected */ var $charset = 'ISO-8859-1'; /** * Cache_Lite_Function object * @var object * @access protected */ //var $cacheLiteFunction; // }}} // {{{ Constructor /** * Constructor * * Constructs the Translation2_Decorator * @param object Translation2 object to decorate */ function Translation2_Decorator(& $translation2) { $this->translation2 = & $translation2; //used for debug only $this->storage = & $translation2->storage; $this->currentPageID = & $translation2->currentPageID; $this->lang = & $translation2->lang; } // }}} // {{{ setOptions() /** * set Decorator options * * @param array options */ function setOptions($options=array()) { if (is_array($options)) { foreach ($options as $option => $value) { if (isset($this->$option)) { $this->$option = $value; } } } } // }}} // {{{ setLang() /** * Set default lang * @param string $langID */ function setLang($langID) { $this->translation2->setLang($langID); } // }}} // {{{ setPageID($pageID) /** * Set default page * @param string $langID */ function setPageID($pageID=null) { $this->translation2->setPageID($pageID); } // }}} // {{{ getLang() /** * get lang info * @param string $langID * @param string $format ['name', 'meta', 'error_text', 'array'] * @return mixed [string | array], depending on $format */ function getLang($langID=null, $format='name') { return $this->translation2->getLang($langID, $format); } // }}} // {{{ getLangs() /** * get langs * @param string $format ['ids', 'names', 'array'] * @return array */ function getLangs($format='name') { return $this->translation2->getLangs($format); } // }}} // {{{ setParams() /** * Set parameters for next string * @param params $params */ function setParams($params=null) { $this->translation2->setParams($params); } // }}} // {{{ setDecoratedLang() /** * Set default lang * @param string $langID */ function setDecoratedLang($langID) { if (isset($this->translation2->currentLang)) { $this->translation2->currentLang = $langID; } } // }}} // {{{ /** * Pass a Cache_Lite_Function object to the decorator * @param object Cache_Lite_Function instance */ /* function setCacheLite(& $cacheLiteFunction) { $this->cacheLiteFunction = & $cacheLiteFunction; } */ // }}} // {{{ get() /** * Get translated string * * First check if the string is cached, if not => fetch the page * from the container and cache it for later use. * If the string is empty, check the fallback language; if * the latter is empty too, then return the $defaultText. * * @param string $stringID * @param string $pageID * @param string $langID * @param string $defaultText Text to display when the strings in both * the default and the fallback lang are empty * @return string */ function get($stringID, $pageID=TRANSLATION2_DEFAULT_PAGEID, $langID=null, $defaultText='') { return $this->translation2->get($stringID, $pageID, $langID, $defaultText); } // }}} // {{{ getRawPage() /** * Get the array of strings in a page * * First check if the strings are cached, if not => fetch the page * from the container and cache it for later use. * * @param string $pageID * @param string $langID * @return array */ function getRawPage($pageID=TRANSLATION2_DEFAULT_PAGEID, $langID=null) { return $this->translation2->getRawPage($pageID, $langID); } // }}} // {{{ getPage() /** * Same as getRawPage, but resort to fallback language and * replace parameters when needed * * @param string $pageID * @param string $langID * @return array */ function getPage($pageID=TRANSLATION2_DEFAULT_PAGEID, $langID=null, $defaultText='') { $this->translation2->getPage($pageID, $langID, $defaultText); } // }}} // {{{ _replaceParams() /** * Replace parameters in strings * @param mixed $params * @access protected */ function _replaceParams($strings) { return $this->translation2->_replaceParams($strings); } // }}} // {{{ replaceEmptyStringsWithKeys() /** * Replace empty strings with their stringID * @param mixed $params * @access public */ function replaceEmptyStringsWithKeys($strings) { return $this->translation2->replaceEmptyStringsWithKeys($strings); } // }}} // {{{ translate() /** * Get translated string * * @param string $string This is NOT the stringID, this is a real string. * The method will search for its matching stringID, and then * it will return the associate string in the selected language. * @param string $langID * @return string */ function translate($string, $langID, $pageID=TRANSLATION2_DEFAULT_PAGEID) { return $this->translation2->translate($string, $langID, $pageID); } // }}} } ?> http://cvs.php.net/co.php/pear/Translation2/Decorator/CacheMemory.php?r=1.1&p=1 Index: pear/Translation2/Decorator/CacheMemory.php +++ pear/Translation2/Decorator/CacheMemory.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Lorenzo Alberton <l dot alberton at quipo dot it> | // +----------------------------------------------------------------------+ // // $Id: CacheMemory.php,v 1.1 2004/03/11 17:06:55 quipo Exp $ // /** * @package Translation2 * @version $Id: CacheMemory.php,v 1.1 2004/03/11 17:06:55 quipo Exp $ */ /** * Load Translation2 decorator base class */ require_once 'Translation2'.DIRECTORY_SEPARATOR.'Decorator.php'; /** * Allows redefinition of alternate key for empty pageID */ if (!defined('TRANSLATION2_EMPTY_PAGEID_KEY')) { define('TRANSLATION2_EMPTY_PAGEID_KEY', 'array_key_4_empty_pageID'); } /** * Allows redefinition of alternate key for null pageID */ if (!defined('TRANSLATION2_NULL_PAGEID_KEY')) { define('TRANSLATION2_NULL_PAGEID_KEY', 'array_key_4_null_pageID'); } /** * Decorator to cache fetched data in memory * @package Translation2 * @access public */ class Translation2_Decorator_CacheMemory extends Translation2_Decorator { // {{{ class vars // }}} // {{{ _getPageIDKey() /** * return a valid array key based on pageID value * * @param mixed $pageID (string or null) * @return string */ function _getPageIDKey($pageID) { if (is_null($pageID)) { return TRANSLATION2_NULL_PAGEID_KEY; } if (empty($pageID)) { return TRANSLATION2_EMPTY_PAGEID_KEY; } if ($pageID == TRANSLATION2_DEFAULT_PAGEID) { return $this->translation2->currentPageID; } return $pageID; } // }}} // {{{ get() /** * Get translated string * * First check if the string is cached, if not => fetch the page * from the container and cache it for later use. * * @param string $stringID * @param string $pageID * @param string $langID * @param string $defaultText Text to display when the strings in both * the default and the fallback lang are empty * @return string */ function get($stringID, $pageID=TRANSLATION2_DEFAULT_PAGEID, $langID=null, $defaultText='') { $pageID_key = $this->_getPageIDKey($pageID); $langID_key = empty($langID) ? $this->translation2->lang['id'] : $langID; if (!array_key_exists($langID_key, $this->data)) { $this->data[$langID_key] = array(); } if ($this->prefetch) { $this->getPage($pageID, $langID); } //$str = $defaultText; if (array_key_exists($pageID_key, $this->data[$langID_key])) { $str = (isset($this->data[$langID_key][$pageID_key][$stringID]) ? $this->data[$langID_key][$pageID_key][$stringID] : ''); } else { $str = $this->translation2->get($stringID, $pageID, $langID); } return $this->_replaceParams($str); } // }}} // {{{ getRawPage() /** * Get the array of strings in a page * * First check if the strings are cached, if not => fetch the page * from the container and cache it for later use. * * @param string $pageID * @param string $langID * @return array */ function getRawPage($pageID=TRANSLATION2_DEFAULT_PAGEID, $langID=null) { $pageID_key = $this->_getPageIDKey($pageID); $langID_key = empty($langID) ? $this->translation2->lang['id'] : $langID; if (!array_key_exists($langID_key, $this->rawData)) { $this->rawData[$langID_key] = array(); } if (!array_key_exists($pageID_key, $this->rawData[$langID_key])) { $this->rawData[$langID_key][$pageID_key] = $this->translation2->getRawPage($pageID, $langID); } return $this->rawData[$langID_key][$pageID_key]; } // }}} // {{{ getPage() /** * Same as getRawPage, but resort to fallback language and * replace parameters when needed * * @param string $pageID * @param string $langID * @return array */ function getPage($pageID=TRANSLATION2_DEFAULT_PAGEID, $langID=null, $defaultText='') { $pageID_key = $this->_getPageIDKey($pageID); $langID_key = empty($langID) ? $this->translation2->lang['id'] : $langID; if (!array_key_exists($langID_key, $this->data)) { $this->data[$langID_key] = array(); } if (!array_key_exists($pageID_key, $this->data[$langID_key])) { $this->data[$langID_key][$pageID_key] = $this->translation2->getPage($pageID, $langID); } $this->data[$langID_key][$pageID_key] = $this->_replaceParams($this->data[$langID_key][$pageID_key]); return $this->data[$langID_key][$pageID_key]; } // }}} } ?> http://cvs.php.net/co.php/pear/Translation2/Decorator/DefaultText.php?r=1.1&p=1 Index: pear/Translation2/Decorator/DefaultText.php +++ pear/Translation2/Decorator/DefaultText.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Lorenzo Alberton <l dot alberton at quipo dot it> | // +----------------------------------------------------------------------+ // // $Id: DefaultText.php,v 1.1 2004/03/11 17:06:55 quipo Exp $ // /** * @package Translation2 * @version $Id: DefaultText.php,v 1.1 2004/03/11 17:06:55 quipo Exp $ */ /** * Load Translation2 decorator base class */ require_once 'Translation2'.DIRECTORY_SEPARATOR.'Decorator.php'; /** * Decorator to provide a fallback text for empty strings. * @package Translation2 */ class Translation2_Decorator_DefaultText extends Translation2_Decorator { // {{{ get() /** * Get translated string * * If the string is empty, return the $defaultText if not empty, * the $stringID otherwise. * * @param string $stringID * @param string $pageID * @param string $langID * @param string $defaultText Text to display when the string is empty * @return string */ function get($stringID, $pageID=TRANSLATION2_DEFAULT_PAGEID, $langID=null, $defaultText='') { $str = $this->translation2->get($stringID, $pageID, $langID); if (empty($str)) { $str = (empty($defaultText) ? $stringID : $defaultText); } return $str; } // }}} // {{{ getPage() /** * Replace empty strings with their $stringID * * @param string $pageID * @param string $langID * @return array */ function getPage($pageID=TRANSLATION2_DEFAULT_PAGEID, $langID=null) { $data = $this->translation2->getPage($pageID, $langID); return $this->replaceEmptyStringsWithKeys($data); } // }}} } ?> http://cvs.php.net/co.php/pear/Translation2/Decorator/Lang.php?r=1.1&p=1 Index: pear/Translation2/Decorator/Lang.php +++ pear/Translation2/Decorator/Lang.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Lorenzo Alberton <l dot alberton at quipo dot it> | // +----------------------------------------------------------------------+ // // $Id: Lang.php,v 1.1 2004/03/11 17:06:55 quipo Exp $ // /** * @package Translation2 * @version $Id: Lang.php,v 1.1 2004/03/11 17:06:55 quipo Exp $ */ /** * Load Translation2 decorator base class */ require_once 'Translation2'.DIRECTORY_SEPARATOR.'Decorator.php'; /** * Decorator to provide a fallback language for empty strings. * @package Translation2 */ class Translation2_Decorator_Lang extends Translation2_Decorator { // {{{ setDecoratedLang() /** * Set default lang * @param string $langID */ function setDecoratedLang($langID) { $this->currentLang = $langID; } // }}} // {{{ get() /** * Get translated string * * First check if the string is cached, if not => fetch the page * from the container and cache it for later use. * If the string is empty, check the fallback language; if * the latter is empty too, then return the $defaultText. * * @param string $stringID * @param string $pageID * @param string $langID * @param string $defaultText Text to display when the strings in both * the default and the fallback lang are empty * @return string */ function get($stringID, $pageID=TRANSLATION2_DEFAULT_PAGEID, $langID=null, $defaultText='') { $str = $this->translation2->get($stringID, $pageID, $langID, $defaultText); if (empty($str)) { $str = $this->translation2->get($stringID, $pageID, $this->currentLang); } return $str; } // }}} // {{{ getRawPage() /** * Get the array of strings in a page * * First check if the strings are cached, if not => fetch the page * from the container and cache it for later use. * * @param string $pageID * @param string $langID * @return array */ function getRawPage($pageID=TRANSLATION2_DEFAULT_PAGEID, $langID=null) { $data1 = $this->translation2->getRawPage($pageID, $langID); return $data1; } // }}} // {{{ getPage() /** * Same as getRawPage, but resort to fallback language and * replace parameters when needed * * @param string $pageID * @param string $langID * @return array */ function getPage($pageID=TRANSLATION2_DEFAULT_PAGEID, $langID=null, $defaultText='') { $data1 = $this->translation2->getPage($pageID, $langID); $data2 = $this->translation2->getPage($pageID, $this->currentLang); foreach ($data1 as $key => $val) { if (empty($val)) { $data1[$key] = $data2[$key]; } } return $data1; } // }}} } ?> http://cvs.php.net/co.php/pear/Translation2/Decorator/SpecialChars.php?r=1.1&p=1 Index: pear/Translation2/Decorator/SpecialChars.php +++ pear/Translation2/Decorator/SpecialChars.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Lorenzo Alberton <l dot alberton at quipo dot it> | // +----------------------------------------------------------------------+ // // $Id: SpecialChars.php,v 1.1 2004/03/11 17:06:55 quipo Exp $ // /** * @package Translation2 * @version $Id: SpecialChars.php,v 1.1 2004/03/11 17:06:55 quipo Exp $ */ /** * Load Translation2 decorator base class */ require_once 'Translation2'.DIRECTORY_SEPARATOR.'Decorator.php'; /** * Decorator to replace special chars with the matching html entities. * You can set the charset to use (the default being 'ISO-8859-1'): * <code> * $tr->setOptions(array('charset' => 'UTF-8')); * </code> * @see http://www.php.net/htmlentities for a list of available charsets. * @package Translation2 */ class Translation2_Decorator_SpecialChars extends Translation2_Decorator { // {{{ get() /** * Get translated string * * replace special chars with the matching html entities * * @param string $stringID * @param string $pageID * @param string $langID * @param string $defaultText Text to display when the string is empty * @return string */ function get($stringID, $pageID=TRANSLATION2_DEFAULT_PAGEID, $langID=null, $defaultText=null) { $str = $this->translation2->get($stringID, $pageID, $langID, $defaultText); if (!empty($str)) { $str = htmlentities($str, ENT_QUOTES, $this->charset); } return $str; } // }}} // {{{ getPage() /** * Same as getRawPage, but apply transformations when needed * * @param string $pageID * @param string $langID * @return array */ function getPage($pageID=TRANSLATION2_DEFAULT_PAGEID, $langID=null) { $data = $this->translation2->getPage($pageID, $langID); foreach ($data as $key => $val) { if (!empty($val)) { $data[$key] = htmlentities($val, ENT_QUOTES, $this->charset); } } return $data; } // }}} } ?>
« previous php.pear.cvs (#17310) next »