|
1
|
|
|
<?php |
|
2
|
|
|
namespace Agavi\Translation; |
|
3
|
|
|
|
|
4
|
|
|
// +---------------------------------------------------------------------------+ |
|
5
|
|
|
// | This file is part of the Agavi package. | |
|
6
|
|
|
// | Copyright (c) 2005-2011 the Agavi Project. | |
|
7
|
|
|
// | | |
|
8
|
|
|
// | For the full copyright and license information, please view the LICENSE | |
|
9
|
|
|
// | file that was distributed with this source code. You can also view the | |
|
10
|
|
|
// | LICENSE file online at http://www.agavi.org/LICENSE.txt | |
|
11
|
|
|
// | vi: set noexpandtab: | |
|
12
|
|
|
// | Local Variables: | |
|
13
|
|
|
// | indent-tabs-mode: t | |
|
14
|
|
|
// | End: | |
|
15
|
|
|
// +---------------------------------------------------------------------------+ |
|
16
|
|
|
use Agavi\Core\Context; |
|
17
|
|
|
use Agavi\Exception\AgaviException; |
|
18
|
|
|
use Agavi\Util\Toolkit; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* SimpleTranslator defines the translator which loads the data from its |
|
22
|
|
|
* parameters. |
|
23
|
|
|
* |
|
24
|
|
|
* @package agavi |
|
25
|
|
|
* @subpackage translation |
|
26
|
|
|
* |
|
27
|
|
|
* @author Dominik del Bondio <[email protected]> |
|
28
|
|
|
* @copyright Authors |
|
29
|
|
|
* @copyright The Agavi Project |
|
30
|
|
|
* |
|
31
|
|
|
* @since 0.11.0 |
|
32
|
|
|
* |
|
33
|
|
|
* @version $Id$ |
|
34
|
|
|
*/ |
|
35
|
|
|
class SimpleTranslator extends BasicTranslator |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* @var array The data for each domain |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $domainData = array(); |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var array The data for the currently active locale |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $currentData = array(); |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var Locale The currently set locale |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $locale = null; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Initialize this Translator. |
|
54
|
|
|
* |
|
55
|
|
|
* @param Context $context An Context instance. |
|
56
|
|
|
* @param array $parameters An associative array of initialization parameters. |
|
57
|
|
|
* |
|
58
|
|
|
* @author Dominik del Bondio <[email protected]> |
|
59
|
|
|
* @since 0.11.0 |
|
60
|
|
|
*/ |
|
61
|
|
|
public function initialize(Context $context, array $parameters = array()) |
|
62
|
|
|
{ |
|
63
|
|
|
parent::initialize($context); |
|
64
|
|
|
|
|
65
|
|
|
$domainData = array(); |
|
66
|
|
|
|
|
67
|
|
|
foreach ((array)$parameters as $domain => $locales) { |
|
68
|
|
|
foreach ((array)$locales as $locale => $translations) { |
|
69
|
|
|
foreach ((array)$translations as $key => $translation) { |
|
70
|
|
|
if (is_array($translation)) { |
|
71
|
|
|
$domainData[$locale][$domain][$translation['from']] = $translation['to']; |
|
72
|
|
|
} else { |
|
73
|
|
|
$domainData[$locale][$domain][$key] = $translation; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$this->domainData = $domainData; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Translates a message into the defined language. |
|
84
|
|
|
* |
|
85
|
|
|
* @param mixed $message The message to be translated. |
|
86
|
|
|
* @param string $domain The domain of the message. |
|
87
|
|
|
* @param Locale $locale The locale to which the message should be |
|
88
|
|
|
* translated. |
|
89
|
|
|
* |
|
90
|
|
|
* @return string The translated message. |
|
91
|
|
|
* |
|
92
|
|
|
* @author Dominik del Bondio <[email protected]> |
|
93
|
|
|
* @since 0.11.0 |
|
94
|
|
|
*/ |
|
95
|
|
|
public function translate($message, $domain, Locale $locale = null) |
|
96
|
|
|
{ |
|
97
|
|
|
if ($locale && $locale !== $this->locale) { |
|
98
|
|
|
$oldCurrentData = $this->currentData; |
|
99
|
|
|
$oldLocale = $this->locale; |
|
100
|
|
|
$this->localeChanged($locale); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
if (is_array($message)) { |
|
104
|
|
|
throw new AgaviException('The simple translator doesn\'t support pluralized input'); |
|
105
|
|
|
} else { |
|
106
|
|
|
$data = isset($this->currentData[(string)$domain][$message]) ? $this->currentData[(string)$domain][$message] : $message; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
if ($locale && $locale !== $this->locale) { |
|
110
|
|
|
$this->currentData = $oldCurrentData; |
|
|
|
|
|
|
111
|
|
|
$this->locale = $oldLocale; |
|
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $data; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* This method gets called by the translation manager when the default locale |
|
119
|
|
|
* has been changed. |
|
120
|
|
|
* |
|
121
|
|
|
* @param Locale $newLocale The new default locale. |
|
122
|
|
|
* |
|
123
|
|
|
* @author David Zülke <[email protected]> |
|
124
|
|
|
* @since 0.11.0 |
|
125
|
|
|
*/ |
|
126
|
|
|
public function localeChanged($newLocale) |
|
127
|
|
|
{ |
|
128
|
|
|
$this->locale = $newLocale; |
|
129
|
|
|
$this->currentData = Toolkit::getValueByKeyList($this->domainData, Locale::getLookupPath($this->locale->getIdentifier()), array()); |
|
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: