Translation2
[ class tree: Translation2 ] [ index: Translation2 ] [ all elements ]

Source for file Translation2_example.php

Documentation is available at Translation2_example.php

  1. <?php
  2. /**
  3.  * require file with settings and Translation2 class,
  4.  * set parameters and options
  5.  */
  6. require_once './settings.php';
  7. //require_once './settings_multitable.php';  //this one uses one table per lang
  8. require_once 'Translation2.php';
  9.  
  10. //require_once 'Translation2/Admin.php';
  11. //$tr =& Translation2_Admin::factory($driver, $dbinfo, $params);
  12.  
  13. $tr =Translation2::factory($driver$dbinfo$params);
  14.  
  15. writeTitle('ITALIANO');
  16. $err $tr->setLang('it');
  17. if (PEAR::isError($err)) {
  18.     die($err->getMessage());
  19. }
  20. $tr->setPageID();
  21. //$tr =& $tr->getDecorator('CacheMemory');
  22. $tr =$tr->getDecorator('CacheLiteFunction');
  23. $tr->setOption('cacheDir'$cache_options['cacheDir']);
  24. $tr->setOption('lifeTime'$cache_options['lifeTime']);
  25. //$tr->setOption('prefetch', false);
  26. $tr =$tr->getDecorator('Lang');
  27. $tr->setOption('fallbackLang''en');
  28. $tr =$tr->getDecorator('Lang');
  29. $tr->setOption('fallbackLang''de');
  30.  
  31.  
  32. // =[DEBUG INFO]======================================
  33. $str = <<<EOT
  34. // new Translation2 instance
  35. // (look at settings.php for an example of \$dbinfo and \$params)
  36. \$tr = & Translation2::factory("\$driver", \$dbinfo, \$params);
  37.  
  38. // set Italian as default lang
  39. \$err = \$tr->setLang('it');
  40.  
  41. // check for problems
  42. if (PEAR::isError(\$err)) {
  43.     die(\$err->getMessage());
  44. }
  45.  
  46. // get global strings (pageID = NULL)
  47. \$tr->setPageID();
  48.  
  49. // add a 'CacheMemory Decorator', i.e. add a memory-cache layer
  50. // to avoid multiple queries to the db
  51. \$tr = & \$tr->getDecorator('CacheMemory');
  52.  
  53. // you can also add a file-based cache layer, which uses
  54. // the *fast* Cache_Lite_Function class. With this decorator,
  55. // the number of db queries will drop to zero.
  56. // Warning: a lot of cache files will be created!
  57. \$tr =& \$tr->getDecorator('CacheLiteFunction');
  58. \$tr->setOption('cacheDir', \$cache_options['cacheDir']);  //default is '/tmp/'
  59. \$tr->setOption('lifeTime', \$cache_options['lifeTime']);   //default is 3600 (= 1 minute)
  60.  
  61. // set an 'English Decorator', i.e. add English as a fallback language
  62. \$tr = & \$tr->getDecorator('Lang');
  63. \$tr->setOption('fallbackLang', 'en');
  64.  
  65. // add a 'German Decorator', i.e. add German as a third fallback language
  66. \$tr = & \$tr->getDecorator('Lang');
  67. \$tr->setOption('fallbackLang', 'de');
  68. EOT;
  69. // ====================================================
  70. debug($str);
  71.  
  72.  
  73. debug('$test = $tr->get(\'test\');
  74. if (PEAR::isError($test)) {
  75.     //you must check for errors on the first $tr->get*() call,
  76.     //since the db connection is not estabilished beforehand.
  77.  
  78.     //handle error
  79. }
  80. echo $test');
  81. writeValue('test'$tr->get('test'));
  82. debug('$tr->get(\'only_english\'); //test fallback language for a string not translated in Italian');
  83. writeValue('only_english'$tr->get('only_english'));
  84. debug('$tr->getRawPage();');
  85. writeValue('all the page (raw)'$tr->getRawPage());
  86. debug('$tr->getPage();');
  87. writeValue('all the page (with fallback langs)'$tr->getPage());
  88.  
  89.  
  90. //-------------------------------------------------------
  91.  
  92. writeTitle('GET LANG INFO');
  93. debug('$tr->getLang(); //no langID => get current lang');
  94. writeValue('[IT] LANG_NAME'$tr->getLang())//no langID => get current lang
  95. debug('$tr->getLang(\'it\', \'error_text\'); //use 2nd parameter to choose the lang info you need');
  96. writeValue('[IT] LANG_ERRTXT'$tr->getLang('it''error_text'));
  97. debug('$tr->getLang(\'it\', \'meta\'); //use 2nd parameter to choose the lang info you need');
  98. writeValue('[EN] LANG_META'$tr->getLang('it''meta'));
  99. debug('$tr->getLang(\'en\'); //default format is \'name\'');
  100. writeValue('[EN] LANG_NAME'$tr->getLang('en'));
  101. debug('$tr->getLang(\'en\', \'error_text\');');
  102. writeValue('[EN] LANG_ERRTXT'$tr->getLang('en''error_text'));
  103. debug('$tr->getLang(\'en\', \'meta\');');
  104. writeValue('[EN] LANG_META'$tr->getLang('en''meta'));
  105.  
  106.  
  107. //-------------------------------------------------------
  108.  
  109.  
  110. writeTitle('DEBUG INFO');
  111. debug('NUMBER OF DB QUERIES: '.(isset($tr->storage->_queries)?$tr->storage->_queries:'0 (gettext)'));
  112. unset($tr);
  113.  
  114.  
  115. //-------------------------------------------------------
  116.  
  117. //new example
  118.  
  119. writeTitle('ENGLISH');
  120. $tr Translation2::factory($driver$dbinfo$params);
  121. $err $tr->setLang('en');
  122. if (PEAR::isError($err)) {
  123.     die($err->getMessage());
  124. }
  125. $tr->setPageID();
  126. $tr $tr->getDecorator('CacheMemory');
  127. $tr =$tr->getDecorator('CacheLiteFunction');
  128. $tr->setOption('cacheDir'$cache_options['cacheDir']);
  129. $tr->setOption('lifeTime'$cache_options['lifeTime']);
  130. //$tr->prefetch = false;
  131. $tr $tr->getDecorator('Lang');
  132. $tr->setOption('fallbackLang''it');
  133.  
  134.  
  135. // =[DEBUG INFO]======================================
  136. $str = <<<EOT
  137. // new Translation2 instance
  138. \$tr = & Translation2::factory("\$driver", \$dbinfo, \$params);
  139.  
  140. // set English as default lang
  141. \$tr->setLang('en');
  142.  
  143. // check for problems
  144. if (PEAR::isError(\$err)) {
  145.     die(\$err->getMessage());
  146. }
  147.  
  148. // get global strings (empty pageID)
  149. \$tr->setPageID();
  150.  
  151. // add a 'CacheMemory Decorator', i.e. add a memory-cache layer
  152. // to avoid multiple queries to the db
  153. \$tr = & \$tr->getDecorator('CacheMemory');
  154.  
  155. // you can also add a file-based cache layer, which uses
  156. // the *fast* Cache_Lite_Function class. With this decorator,
  157. // the number of db queries will drop to zero.
  158. // Warning: a lot of cache files will be created!
  159. \$tr =& \$tr->getDecorator('CacheLiteFunction');
  160. \$tr->setOption('cacheDir', \$cache_options['cacheDir']);  //default is '/tmp/'
  161. \$tr->setOption('lifeTime', \$cache_options['lifeTime']);   //default is 3600 (= 1 minute)
  162.  
  163. // set an 'Italian Decorator', i.e. add Italian as a fallback language
  164. \$tr = & \$tr->getDecorator('Lang');
  165. \$tr->setOption('fallbackLang', 'it');
  166. EOT;
  167. // ====================================================
  168. debug($str);
  169.  
  170.  
  171. debug('$tr->get(\'test\');');
  172. writeValue('test'$tr->get('test'));
  173. debug('get(\'only_italian\'); //test fallback language for a string not translated in English');
  174. writeValue('only_italian'$tr->get('only_italian'));
  175. debug('getRawPage();');
  176. writeValue('all the page (raw)'$tr->getRawPage());
  177. debug('getPage();');
  178. writeValue('all the page (with fallback langs)'$tr->getPage());
  179.  
  180.  
  181. //-------------------------------------------------------
  182.  
  183.  
  184. writeTitle('TEST PARAMETER SUBSTITUTION');
  185. $tr->setParams(array(
  186.     0         => '',
  187.     'user'    => 'Joe',
  188.     'day'     => '15',
  189.     'month'   => $tr->get('month_01''calendar''en'),
  190.     'year'    => '2004',
  191.     'weekday' => $tr->get('day_5''calendar''en')
  192. ));
  193. // =[DEBUG INFO]======================================
  194. $str = <<<EOT
  195. \$tr->setParams(array(
  196.     0         => '',
  197.     'user'    => 'Joe',
  198.     'day'     => '15',
  199.     'month'   => \$tr->get('month_01', 'calendar', 'en'),
  200.     'year'    => '2004',
  201.     'weekday' => \$tr->get('day_5', 'calendar', 'en')
  202. ));
  203. EOT;
  204. // ====================================================
  205. debug($str);
  206.  
  207. debug('$tr->get(\'hello_user\');');
  208. writeValue('[EN] hello, user'$tr->get('hello_user'));
  209.  
  210.  
  211.  
  212. $tr->setLang('it');
  213. $tr->setOption('fallbackLang''en');
  214. $tr->setParams(array(
  215.     0         => '',
  216.     'user'    => 'Joe',
  217.     'day'     => '15',
  218.     'month'   => $tr->get('month_01''calendar'),
  219.     'year'    => '2004',
  220.     'weekday' => $tr->get('day_5''calendar')
  221. ));
  222. // =[DEBUG INFO]======================================
  223. $str = <<<EOT
  224. \$tr->setLang('it');
  225. \$tr->setOption('fallbackLang', 'en');
  226. \$tr->setParams(array(
  227.     0         => '',
  228.     'user'    => 'Joe',
  229.     'day'     => '15',
  230.     'month'   => \$tr->get('month_01', 'calendar', 'it'),
  231.     'year'    => '2004',
  232.     'weekday' => \$tr->get('day_5', 'calendar', 'it')
  233. ));
  234. EOT;
  235. // ====================================================
  236. debug($str);
  237. writeValue('[IT] hello, user'$tr->get('hello_user'));
  238.  
  239.  
  240. //-------------------------------------------------------
  241.  
  242.  
  243. writeTitle('SPECIAL CHARS DECORATOR');
  244. $tr $tr->getDecorator('SpecialChars');
  245.  
  246. // =[DEBUG INFO]======================================
  247. $str = <<<EOT
  248. // set a 'SpecialChars Decorator' to replace htmlentities
  249. \$tr = & \$tr->getDecorator('SpecialChars');
  250. \$tr->setOptions(array('charset' => 'ISO-8859-1'); //default
  251. EOT;
  252. // ====================================================
  253. debug($str);
  254. debug('$tr->get(\'day_5\', \'calendar\', \'it\');');
  255.  
  256. writeValue($tr->get('day_5''calendar''it'));
  257.  
  258.  
  259. //-------------------------------------------------------
  260.  
  261.  
  262. writeTitle('TRANSLATION (STRING TO STRING)');
  263. debug('$stringID = $tr->getStringID(\'gennaio\', \'calendar\');');
  264. $stringID $tr->getStringID('gennaio''calendar');
  265. writeValue('gennaio'$tr->get($stringID'calendar''en'));
  266.  
  267.  
  268.  
  269.  
  270.  
  271. //-------------------------------------------------------
  272.  
  273.  
  274. writeTitle('TEST STRINGS WITH pageID NOT NULL');
  275. debug('$tr->get(\'alone\', \'alone\');');
  276. writeValue('[IT] alone'$tr->get('alone''alone'));
  277. debug('$tr->get(\'alone\', \'alone\', \'en\');');
  278. writeValue('[EN] alone'$tr->get('alone''alone''en'));
  279.  
  280.  
  281. //-------------------------------------------------------
  282.  
  283.  
  284. writeTitle('HANDLE CONFLICTS');
  285. $tr->setLang('en');
  286. $tr->setOption('fallbackLang''it');
  287. $tr->setPageID('in_page');
  288.  
  289. // =[DEBUG INFO]======================================
  290. $str = <<<EOT
  291. \$tr->setLang('en');
  292. \$tr->setOption('fallbackLang', 'it');
  293. \$tr->setPageID('in_page');
  294. EOT;
  295. // ====================================================
  296. debug($str);
  297.  
  298. debug('$tr->get(\'prova_conflitto\'); //pageID=TRANSLATION2_DEFAULT_PAGEID => get current pageID');
  299. writeValue('[EN] (in page) string'$tr->get('prova_conflitto'));
  300. debug('$tr->get(\'prova_conflitto\', null); //pageID=null => get strings with pageID = NULL');
  301. writeValue('[EN] (global)  string'$tr->get('prova_conflitto'null));
  302. debug('$tr->get(\'prova_conflitto\', \'in_page\'); //force pageID');
  303. writeValue('[EN] (in page) string'$tr->get('prova_conflitto''in_page'));
  304.  
  305.  
  306. //-------------------------------------------------------
  307.  
  308.  
  309. writeTitle('USE A DefaultText DECORATOR TO DEAL WITH EMPTY STRINGS');
  310. $tr $tr->getDecorator('DefaultText');
  311. $tr->setOption('emptyPrefix''[');
  312. $tr->setOption('emptyPostfix'']');
  313.  
  314. // =[DEBUG INFO]======================================
  315. $str = <<<EOT
  316. \$tr = & \$tr->getDecorator('DefaultText');
  317. \$tr->setOption('emptyPrefix', '[');  //mark empty strings
  318. \$tr->setOption('emptyPostfix', ']'); //enclose them within brackets
  319. EOT;
  320. // ====================================================
  321. debug($str);
  322.  
  323. debug('$tr->get(\'isempty\'); //get stringID when the string is empty');
  324. writeValue('[EN] empty string'$tr->get('isempty'));
  325.  
  326. debug('$tr->get(\'isempty\', null, \'en\', \'show this default text\'); //use a custom fallback text');
  327. writeValue('[EN] empty string'$tr->get('isempty'null'en''show this default text'));
  328.  
  329.  
  330.  
  331.  
  332. /*
  333. writeTitle('Use error_text when default and fallback lang and defaultText are EMPTY');
  334. writeValue('[EN] empty string', $tr->get('isempty'));
  335. */
  336.  
  337.  
  338.  
  339. if (strtolower(get_class($tr)) == 'translation2_admin'{
  340.  
  341.     writeTitle('TEST ADMIN');
  342.     $res $tr->add('smallTest'nullarray('it' => 'piccolo test',
  343.                                              'en' => 'small test')
  344.             );
  345.     writeValue('add(smallTest)'$res);
  346.  
  347.     $res $tr->add('smallTest'nullarray('de' => 'kinder'));
  348.     writeValue('add(smallTest)'$res);
  349.  
  350.     $res $tr->remove('smallTest'null);
  351.     writeValue('remove(smallTest)'$res);
  352. }
  353.  
  354.  
  355. writeTitle('DEBUG INFO');
  356. debug('NUMBER OF DB QUERIES: '.(isset($tr->storage->_queries)?$tr->storage->_queries:'0 (gettext)'));
  357. ?>

Documentation generated on Mon, 11 Mar 2019 15:41:51 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.