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

Source for file Translation2_gettext_example.php

Documentation is available at Translation2_gettext_example.php

  1. <?php
  2. /**
  3.  * require file with settings and Translation2 class,
  4.  * set parameters and options
  5.  */
  6. require_once './gettext_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''/tmp/');
  23. $tr->setOption('lifeTime'3600*24);
  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', '/tmp/');  //default is '/tmp/'
  58. \$tr->setOption('lifeTime', 3600*24);   //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''/tmp/');
  128. $tr->setOption('lifeTime'3600*24);
  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', '/tmp/');  //default is '/tmp/'
  160. \$tr->setOption('lifeTime', 3600*24);   //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('$tr->translate(\'gennaio\', \'en\', \'calendar\');');
  263. writeValue('gennaio', $tr->translate('gennaio', 'en', 'calendar'));
  264. */
  265.  
  266.  
  267.  
  268.  
  269. //-------------------------------------------------------
  270.  
  271.  
  272. writeTitle('TEST STRINGS WITH pageID NOT NULL');
  273. debug('$tr->get(\'alone\', \'alone\');');
  274. writeValue('[IT] alone'$tr->get('alone''alone'));
  275. debug('$tr->get(\'alone\', \'alone\', \'en\');');
  276. writeValue('[EN] alone'$tr->get('alone''alone''en'));
  277.  
  278.  
  279. //-------------------------------------------------------
  280.  
  281.  
  282. writeTitle('HANDLE CONFLICTS');
  283. $tr->setLang('en');
  284. $tr->setOption('fallbackLang''it');
  285. $tr->setPageID('in_page');
  286.  
  287. // =[DEBUG INFO]======================================
  288. $str = <<<EOT
  289. \$tr->setLang('en');
  290. \$tr->setOption('fallbackLang', 'it');
  291. \$tr->setPageID('in_page');
  292. EOT;
  293. // ====================================================
  294. debug($str);
  295.  
  296. debug('$tr->get(\'prova_conflitto\'); //pageID=TRANSLATION2_DEFAULT_PAGEID => get current pageID');
  297. writeValue('[EN] (in page) string'$tr->get('prova_conflitto'));
  298. debug('$tr->get(\'prova_conflitto\', null); //pageID=null => get strings with pageID = NULL');
  299. writeValue('[EN] (global)  string'$tr->get('prova_conflitto'null));
  300. debug('$tr->get(\'prova_conflitto\', \'in_page\'); //force pageID');
  301. writeValue('[EN] (in page) string'$tr->get('prova_conflitto''in_page'));
  302.  
  303.  
  304. //-------------------------------------------------------
  305.  
  306.  
  307. writeTitle('USE A DefaultText DECORATOR TO DEAL WITH EMPTY STRINGS');
  308. $tr $tr->getDecorator('DefaultText');
  309. $tr->setOption('emptyPrefix''[');
  310. $tr->setOption('emptyPostfix'']');
  311.  
  312. // =[DEBUG INFO]======================================
  313. $str = <<<EOT
  314. \$tr = & \$tr->getDecorator('DefaultText');
  315. \$tr->setOption('emptyPrefix', '[');  //mark empty strings
  316. \$tr->setOption('emptyPostfix', ']'); //enclose them within brackets
  317. EOT;
  318. // ====================================================
  319. debug($str);
  320.  
  321. debug('$tr->get(\'isempty\'); //get stringID when the string is empty');
  322. writeValue('[EN] empty string'$tr->get('isempty'));
  323.  
  324. debug('$tr->get(\'isempty\', null, \'en\', \'show this default text\'); //use a custom fallback text');
  325. writeValue('[EN] empty string'$tr->get('isempty'null'en''show this default text'));
  326.  
  327.  
  328.  
  329.  
  330. /*
  331. writeTitle('Use error_text when default and fallback lang and defaultText are EMPTY');
  332. writeValue('[EN] empty string', $tr->get('isempty'));
  333. */
  334.  
  335.  
  336.  
  337. if (strtolower(get_class($tr)) == 'translation2_admin'{
  338.  
  339.     writeTitle('TEST ADMIN');
  340.     $res $tr->add('smallTest'nullarray('it' => 'piccolo test',
  341.                                              'en' => 'small test')
  342.             );
  343.     writeValue('add(smallTest)'$res);
  344.  
  345.     $res $tr->add('smallTest'nullarray('de' => 'kinder'));
  346.     writeValue('add(smallTest)'$res);
  347.  
  348.     $res $tr->remove('smallTest'null);
  349.     writeValue('remove(smallTest)'$res);
  350. }
  351.  
  352.  
  353. writeTitle('DEBUG INFO');
  354. debug('NUMBER OF DB QUERIES: '.(isset($tr->storage->_queries)?$tr->storage->_queries:'0 (gettext)'));
  355. ?>

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