Source for file Translation2_gettext_example.php
Documentation is available at Translation2_gettext_example.php
* require file with settings and Translation2 class,
* set parameters and options
require_once './gettext_settings.php';
require_once 'Translation2.php';
//require_once 'Translation2/Admin.php';
//$tr =& Translation2_Admin::factory($driver, $options);
$err = $tr->setLang ('it');
if (PEAR ::isError ($err)) {
//$tr =& $tr->getDecorator('CacheMemory');
$tr = & $tr->getDecorator ('CacheLiteFunction');
$tr->setOption ('cacheDir', '/tmp/');
$tr->setOption ('lifeTime', 3600*24 );
//$tr->setOption('prefetch', false);
$tr = & $tr->getDecorator ('Lang');
$tr->setOption ('fallbackLang', 'en');
$tr = & $tr->getDecorator ('Lang');
$tr->setOption ('fallbackLang', 'de');
// =[DEBUG INFO]======================================
// new Translation2 instance
// (look at settings.php for an example of \$options)
\$tr = & Translation2::factory("\$driver", \$options);
// set Italian as default lang
\$err = \$tr->setLang('it');
if (PEAR::isError(\$err)) {
die(\$err->getMessage());
// get global strings (pageID = NULL)
// add a 'CacheMemory Decorator', i.e. add a memory-cache layer
// to avoid multiple queries to the db
\$tr = & \$tr->getDecorator('CacheMemory');
// you can also add a file-based cache layer, which uses
// the *fast* Cache_Lite_Function class. With this decorator,
// the number of db queries will drop to zero.
// Warning: a lot of cache files will be created!
\$tr =& \$tr->getDecorator('CacheLiteFunction');
\$tr->setOption('cacheDir', '/tmp/'); //default is '/tmp/'
\$tr->setOption('lifeTime', 3600*24); //default is 3600 (= 1 minute)
// set an 'English Decorator', i.e. add English as a fallback language
\$tr = & \$tr->getDecorator('Lang');
\$tr->setOption('fallbackLang', 'en');
// add a 'German Decorator', i.e. add German as a third fallback language
\$tr = & \$tr->getDecorator('Lang');
\$tr->setOption('fallbackLang', 'de');
// ====================================================
debug('$test = $tr->get(\'test\');
if (PEAR::isError($test)) {
//you must check for errors on the first $tr->get*() call,
//since the db connection is not estabilished beforehand.
debug('$tr->get(\'only_english\'); //test fallback language for a string not translated in Italian');
writeValue('only_english', $tr->get ('only_english'));
debug('$tr->getRawPage();');
writeValue('all the page (raw)', $tr->getRawPage ());
debug('$tr->getPage();');
writeValue('all the page (with fallback langs)', $tr->getPage ());
//-------------------------------------------------------
debug('$tr->getLang(); //no langID => get current lang');
writeValue('[IT] LANG_NAME', $tr->getLang ()); //no langID => get current lang
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('$tr->getLang(\'it\', \'meta\'); //use 2nd parameter to choose the lang info you need');
writeValue('[EN] LANG_META', $tr->getLang ('it', 'meta'));
debug('$tr->getLang(\'en\'); //default format is \'name\'');
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'));
//-------------------------------------------------------
debug('NUMBER OF DB QUERIES: '. (isset ($tr->storage ->_queries )? $tr->storage ->_queries: '0 (gettext)'));
//-------------------------------------------------------
$err = $tr->setLang ('en');
if (PEAR ::isError ($err)) {
$tr = & $tr->getDecorator ('CacheMemory');
$tr = & $tr->getDecorator ('CacheLiteFunction');
$tr->setOption ('cacheDir', '/tmp/');
$tr->setOption ('lifeTime', 3600*24 );
$tr = & $tr->getDecorator ('Lang');
$tr->setOption ('fallbackLang', 'it');
// =[DEBUG INFO]======================================
// new Translation2 instance
\$tr = & Translation2::factory("\$driver", \$options);
// set English as default lang
if (PEAR::isError(\$err)) {
die(\$err->getMessage());
// get global strings (empty pageID)
// add a 'CacheMemory Decorator', i.e. add a memory-cache layer
// to avoid multiple queries to the db
\$tr = & \$tr->getDecorator('CacheMemory');
// you can also add a file-based cache layer, which uses
// the *fast* Cache_Lite_Function class. With this decorator,
// the number of db queries will drop to zero.
// Warning: a lot of cache files will be created!
\$tr =& \$tr->getDecorator('CacheLiteFunction');
\$tr->setOption('cacheDir', '/tmp/'); //default is '/tmp/'
\$tr->setOption('lifeTime', 3600*24); //default is 3600 (= 1 minute)
// set an 'Italian Decorator', i.e. add Italian as a fallback language
\$tr = & \$tr->getDecorator('Lang');
\$tr->setOption('fallbackLang', 'it');
// ====================================================
debug('$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 (raw)', $tr->getRawPage ());
writeValue('all the page (with fallback langs)', $tr->getPage ());
//-------------------------------------------------------
'month' => $tr->get ('month_01', 'calendar', 'en'),
'weekday' => $tr->get ('day_5', 'calendar', 'en')
// =[DEBUG INFO]======================================
'month' => \$tr->get('month_01', 'calendar', 'en'),
'weekday' => \$tr->get('day_5', 'calendar', 'en')
// ====================================================
debug('$tr->get(\'hello_user\');');
writeValue('[EN] hello, user', $tr->get ('hello_user'));
$tr->setOption ('fallbackLang', 'en');
'month' => $tr->get ('month_01', 'calendar'),
'weekday' => $tr->get ('day_5', 'calendar')
// =[DEBUG INFO]======================================
\$tr->setOption('fallbackLang', 'en');
'month' => \$tr->get('month_01', 'calendar', 'it'),
'weekday' => \$tr->get('day_5', 'calendar', 'it')
// ====================================================
writeValue('[IT] hello, user', $tr->get ('hello_user'));
//-------------------------------------------------------
$tr = & $tr->getDecorator ('SpecialChars');
// =[DEBUG INFO]======================================
// set a 'SpecialChars Decorator' to replace htmlentities
\$tr = & \$tr->getDecorator('SpecialChars');
\$tr->setOptions(array('charset' => 'ISO-8859-1'); //default
// ====================================================
debug('$tr->get(\'day_5\', \'calendar\', \'it\');');
writeValue(, $tr->get ('day_5', 'calendar', 'it'));
//-------------------------------------------------------
writeTitle('TRANSLATION (STRING TO STRING)');
debug('$tr->translate(\'gennaio\', \'en\', \'calendar\');');
writeValue('gennaio', $tr->translate('gennaio', 'en', 'calendar'));
//-------------------------------------------------------
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'));
//-------------------------------------------------------
$tr->setOption ('fallbackLang', 'it');
$tr->setPageID ('in_page');
// =[DEBUG INFO]======================================
\$tr->setOption('fallbackLang', 'it');
\$tr->setPageID('in_page');
// ====================================================
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 = & $tr->getDecorator ('DefaultText');
$tr->setOption ('emptyPrefix', '[');
$tr->setOption ('emptyPostfix', ']');
// =[DEBUG INFO]======================================
\$tr = & \$tr->getDecorator('DefaultText');
\$tr->setOption('emptyPrefix', '['); //mark empty strings
\$tr->setOption('emptyPostfix', ']'); //enclose them within brackets
// ====================================================
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'));
$res = $tr->add ('smallTest', null , array ('it' => 'piccolo test',
$res = $tr->add ('smallTest', null , array ('de' => 'kinder'));
$res = $tr->remove ('smallTest', null );
writeValue('ist there an ampersand?', $tr->get ('nasty ampersand', 'calendar'));
debug('NUMBER OF DB QUERIES: '. (isset ($tr->storage ->_queries )? $tr->storage ->_queries: '0 (gettext)'));
Documentation generated on Mon, 11 Mar 2019 14:41:45 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|