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

Documentation generated on Mon, 11 Mar 2019 13:56:48 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.