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

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