1<?php
2/**
3 * require file with settings and Translation2 class,
4 * set parameters and options
5 */
6require_once './settings.php';
7//require_once './settings_multitable.php';  //this one uses one table per lang
8require_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
15writeTitle('ITALIANO');
16$err = $tr->setLang('it');
17if (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
42if (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');
68EOT;
69// ====================================================
70debug($str);
71
72
73debug('$test = $tr->get(\'test\');
74if (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}
80echo $test');
81writeValue('test', $tr->get('test'));
82debug('$tr->get(\'only_english\'); //test fallback language for a string not translated in Italian');
83writeValue('only_english', $tr->get('only_english'));
84debug('$tr->getRawPage();');
85writeValue('all the page (raw)', $tr->getRawPage());
86debug('$tr->getPage();');
87writeValue('all the page (with fallback langs)', $tr->getPage());
88
89
90//-------------------------------------------------------
91
92writeTitle('GET LANG INFO');
93debug('$tr->getLang(); //no langID => get current lang');
94writeValue('[IT] LANG_NAME', $tr->getLang()); //no langID => get current lang
95debug('$tr->getLang(\'it\', \'error_text\'); //use 2nd parameter to choose the lang info you need');
96writeValue('[IT] LANG_ERRTXT', $tr->getLang('it', 'error_text'));
97debug('$tr->getLang(\'it\', \'meta\'); //use 2nd parameter to choose the lang info you need');
98writeValue('[EN] LANG_META', $tr->getLang('it', 'meta'));
99debug('$tr->getLang(\'en\'); //default format is \'name\'');
100writeValue('[EN] LANG_NAME', $tr->getLang('en'));
101debug('$tr->getLang(\'en\', \'error_text\');');
102writeValue('[EN] LANG_ERRTXT', $tr->getLang('en', 'error_text'));
103debug('$tr->getLang(\'en\', \'meta\');');
104writeValue('[EN] LANG_META', $tr->getLang('en', 'meta'));
105
106
107//-------------------------------------------------------
108
109
110writeTitle('DEBUG INFO');
111debug('NUMBER OF DB QUERIES: '.(isset($tr->storage->_queries)?$tr->storage->_queries:'0 (gettext)'));
112unset($tr);
113
114
115//-------------------------------------------------------
116
117//new example
118
119writeTitle('ENGLISH');
120$tr = & Translation2::factory($driver, $dbinfo, $params);
121$err = $tr->setLang('en');
122if (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
144if (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');
166EOT;
167// ====================================================
168debug($str);
169
170
171debug('$tr->get(\'test\');');
172writeValue('test', $tr->get('test'));
173debug('get(\'only_italian\'); //test fallback language for a string not translated in English');
174writeValue('only_italian', $tr->get('only_italian'));
175debug('getRawPage();');
176writeValue('all the page (raw)', $tr->getRawPage());
177debug('getPage();');
178writeValue('all the page (with fallback langs)', $tr->getPage());
179
180
181//-------------------------------------------------------
182
183
184writeTitle('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));
203EOT;
204// ====================================================
205debug($str);
206
207debug('$tr->get(\'hello_user\');');
208writeValue('[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));
234EOT;
235// ====================================================
236debug($str);
237writeValue('[IT] hello, user', $tr->get('hello_user'));
238
239
240//-------------------------------------------------------
241
242
243writeTitle('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
251EOT;
252// ====================================================
253debug($str);
254debug('$tr->get(\'day_5\', \'calendar\', \'it\');');
255
256writeValue('venerd�', $tr->get('day_5', 'calendar', 'it'));
257
258
259//-------------------------------------------------------
260
261
262writeTitle('TRANSLATION (STRING TO STRING)');
263debug('$stringID = $tr->getStringID(\'gennaio\', \'calendar\');');
264$stringID = $tr->getStringID('gennaio', 'calendar');
265writeValue('gennaio', $tr->get($stringID, 'calendar', 'en'));
266
267
268
269
270
271//-------------------------------------------------------
272
273
274writeTitle('TEST STRINGS WITH pageID NOT NULL');
275debug('$tr->get(\'alone\', \'alone\');');
276writeValue('[IT] alone', $tr->get('alone', 'alone'));
277debug('$tr->get(\'alone\', \'alone\', \'en\');');
278writeValue('[EN] alone', $tr->get('alone', 'alone', 'en'));
279
280
281//-------------------------------------------------------
282
283
284writeTitle('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');
294EOT;
295// ====================================================
296debug($str);
297
298debug('$tr->get(\'prova_conflitto\'); //pageID=TRANSLATION2_DEFAULT_PAGEID => get current pageID');
299writeValue('[EN] (in page) string', $tr->get('prova_conflitto'));
300debug('$tr->get(\'prova_conflitto\', null); //pageID=null => get strings with pageID = NULL');
301writeValue('[EN] (global)  string', $tr->get('prova_conflitto', null));
302debug('$tr->get(\'prova_conflitto\', \'in_page\'); //force pageID');
303writeValue('[EN] (in page) string', $tr->get('prova_conflitto', 'in_page'));
304
305
306//-------------------------------------------------------
307
308
309writeTitle('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
319EOT;
320// ====================================================
321debug($str);
322
323debug('$tr->get(\'isempty\'); //get stringID when the string is empty');
324writeValue('[EN] empty string', $tr->get('isempty'));
325
326debug('$tr->get(\'isempty\', null, \'en\', \'show this default text\'); //use a custom fallback text');
327writeValue('[EN] empty string', $tr->get('isempty', null, 'en', 'show this default text'));
328
329
330
331
332/*
333writeTitle('Use error_text when default and fallback lang and defaultText are EMPTY');
334writeValue('[EN] empty string', $tr->get('isempty'));
335*/
336
337
338
339if (strtolower(get_class($tr)) == 'translation2_admin') {
340
341    writeTitle('TEST ADMIN');
342    $res = $tr->add('smallTest', null, array('it' => 'piccolo test',
343                                             'en' => 'small test')
344            );
345    writeValue('add(smallTest)', $res);
346
347    $res = $tr->add('smallTest', null, array('de' => 'kinder'));
348    writeValue('add(smallTest)', $res);
349
350    $res = $tr->remove('smallTest', null);
351    writeValue('remove(smallTest)', $res);
352}
353
354
355writeTitle('DEBUG INFO');
356debug('NUMBER OF DB QUERIES: '.(isset($tr->storage->_queries)?$tr->storage->_queries:'0 (gettext)'));
357?>