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, $options);
  11.  
  12. $this->tr = Translation2::factory($driver$options);
  13. $tr =Translation2::factory($driver$options);
  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''/tmp/');
  24. $tr->setOption('lifeTime'3600*24);
  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 \$options)
  36. \$tr = & Translation2::factory("\$driver", \$options);
  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', '/tmp/');  //default is '/tmp/'
  59. \$tr->setOption('lifeTime', 3600*24);   //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$options);
  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''/tmp/');
  129. $tr->setOption('lifeTime'3600*24);
  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", \$options);
  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', '/tmp/');  //default is '/tmp/'
  161. \$tr->setOption('lifeTime', 3600*24);   //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('$tr->translate(\'gennaio\', \'en\', \'calendar\');');
  264. writeValue('gennaio', $tr->translate('gennaio', 'en', 'calendar'));
  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. writeTitle('NASTY AMPERSAND');
  354. writeValue('ist there an ampersand?'$tr->get('nasty ampersand''calendar'));
  355.  
  356. writeTitle('DEBUG INFO');
  357. debug('NUMBER OF DB QUERIES: '.(isset($tr->storage->_queries)?$tr->storage->_queries:'0 (gettext)'));
  358. ?>

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