| 1 |
lars |
1 |
<?php
|
|
|
2 |
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
|
|
3 |
|
|
|
4 |
/**
|
|
|
5 |
* Contains the Translation2_Decorator_Lang class
|
|
|
6 |
*
|
|
|
7 |
* PHP versions 4 and 5
|
|
|
8 |
*
|
|
|
9 |
* LICENSE: Redistribution and use in source and binary forms, with or without
|
|
|
10 |
* modification, are permitted provided that the following conditions are met:
|
|
|
11 |
* 1. Redistributions of source code must retain the above copyright
|
|
|
12 |
* notice, this list of conditions and the following disclaimer.
|
|
|
13 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
14 |
* notice, this list of conditions and the following disclaimer in the
|
|
|
15 |
* documentation and/or other materials provided with the distribution.
|
|
|
16 |
* 3. The name of the author may not be used to endorse or promote products
|
|
|
17 |
* derived from this software without specific prior written permission.
|
|
|
18 |
*
|
|
|
19 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
|
|
|
20 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
21 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
22 |
* IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
|
|
|
23 |
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
24 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
25 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
26 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
27 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
28 |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
29 |
*
|
|
|
30 |
* @category Internationalization
|
|
|
31 |
* @package Translation2
|
|
|
32 |
* @author Lorenzo Alberton <l.alberton@quipo.it>
|
|
|
33 |
* @copyright 2004-2007 Lorenzo Alberton
|
|
|
34 |
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
|
|
|
35 |
* @version CVS: $Id: Lang.php 305985 2010-12-05 22:55:33Z clockwerx $
|
|
|
36 |
* @link http://pear.php.net/package/Translation2
|
|
|
37 |
*/
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Load Translation2 decorator base class
|
|
|
41 |
*/
|
|
|
42 |
require_once 'Translation2/Decorator.php';
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Decorator to provide a fallback language for empty strings.
|
|
|
46 |
*
|
|
|
47 |
* @category Internationalization
|
|
|
48 |
* @package Translation2
|
|
|
49 |
* @author Lorenzo Alberton <l.alberton@quipo.it>
|
|
|
50 |
* @copyright 2004-2007 Lorenzo Alberton
|
|
|
51 |
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
|
|
|
52 |
* @version CVS: $Id: Lang.php 305985 2010-12-05 22:55:33Z clockwerx $
|
|
|
53 |
* @link http://pear.php.net/package/Translation2
|
|
|
54 |
*/
|
|
|
55 |
class Translation2_Decorator_Lang extends Translation2_Decorator
|
|
|
56 |
{
|
|
|
57 |
// {{{ class vars
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* fallback lang
|
|
|
61 |
* @var string
|
|
|
62 |
* @access protected
|
|
|
63 |
*/
|
|
|
64 |
var $fallbackLang;
|
|
|
65 |
|
|
|
66 |
// }}}
|
|
|
67 |
// {{{ setOption()
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* set Decorator option (intercept 'fallbackLang' option).
|
|
|
71 |
* I don't know why it's needed, but it doesn't work without.
|
|
|
72 |
*
|
|
|
73 |
* @param string $option option name
|
|
|
74 |
* @param mixed $value option value
|
|
|
75 |
*
|
|
|
76 |
* @return self
|
|
|
77 |
*/
|
|
|
78 |
function setOption($option, $value=null)
|
|
|
79 |
{
|
|
|
80 |
if ($option == 'fallbackLang') {
|
|
|
81 |
$this->fallbackLang = $value;
|
|
|
82 |
return $this;
|
|
|
83 |
}
|
|
|
84 |
return parent::setOption($option, $value);
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
// }}}
|
|
|
88 |
// {{{ get()
|
|
|
89 |
|
|
|
90 |
/**
|
|
|
91 |
* Get translated string
|
|
|
92 |
*
|
|
|
93 |
* If the string is empty, check the fallback language
|
|
|
94 |
*
|
|
|
95 |
* @param string $stringID string ID
|
|
|
96 |
* @param string $pageID page/group ID
|
|
|
97 |
* @param string $langID language ID
|
|
|
98 |
* @param string $defaultText Text to display when the strings in both
|
|
|
99 |
* the default and the fallback lang are empty
|
|
|
100 |
*
|
|
|
101 |
* @return string
|
|
|
102 |
*/
|
|
|
103 |
function get($stringID, $pageID = TRANSLATION2_DEFAULT_PAGEID, $langID = null, $defaultText = '')
|
|
|
104 |
{
|
|
|
105 |
$str = $this->translation2->get($stringID, $pageID, $langID, $defaultText);
|
|
|
106 |
if (empty($str)) {
|
|
|
107 |
$str = $this->translation2->get($stringID, $pageID, $this->fallbackLang);
|
|
|
108 |
}
|
|
|
109 |
return $str;
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
// }}}
|
|
|
113 |
// {{{ getPage()
|
|
|
114 |
|
|
|
115 |
/**
|
|
|
116 |
* Same as getRawPage, but resort to fallback language and
|
|
|
117 |
* replace parameters when needed
|
|
|
118 |
*
|
|
|
119 |
* @param string $pageID page/group ID
|
|
|
120 |
* @param string $langID language ID
|
|
|
121 |
*
|
|
|
122 |
* @return array
|
|
|
123 |
*/
|
|
|
124 |
function getPage($pageID = TRANSLATION2_DEFAULT_PAGEID, $langID = null)
|
|
|
125 |
{
|
|
|
126 |
$data1 = $this->translation2->getPage($pageID, $langID);
|
|
|
127 |
if (PEAR::isError($data1)) {
|
|
|
128 |
return $data1;
|
|
|
129 |
}
|
|
|
130 |
$data2 = $this->translation2->getPage($pageID, $this->fallbackLang);
|
|
|
131 |
if (PEAR::isError($data2)) {
|
|
|
132 |
return $data2;
|
|
|
133 |
}
|
|
|
134 |
foreach ($data1 as $key => $val) {
|
|
|
135 |
if (empty($val)) {
|
|
|
136 |
$data1[$key] = $data2[$key];
|
|
|
137 |
}
|
|
|
138 |
}
|
|
|
139 |
// append keys when fallback lang contains more than current
|
|
|
140 |
$diff = array_diff(array_keys($data2), array_keys($data1));
|
|
|
141 |
foreach ($diff as $key) {
|
|
|
142 |
$data1[$key] = $data2[$key];
|
|
|
143 |
}
|
|
|
144 |
return $data1;
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
// }}}
|
|
|
148 |
}
|
|
|
149 |
?>
|