1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* The MIT License |
5
|
|
|
* |
6
|
|
|
* Copyright 2016 Alejandro Peña Florentín ([email protected]). |
7
|
|
|
* |
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
10
|
|
|
* in the Software without restriction, including without limitation the rights |
11
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
12
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
13
|
|
|
* furnished to do so, subject to the following conditions: |
14
|
|
|
* |
15
|
|
|
* The above copyright notice and this permission notice shall be included in |
16
|
|
|
* all copies or substantial portions of the Software. |
17
|
|
|
* |
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
23
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
24
|
|
|
* THE SOFTWARE. |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace Tight\Modules\Localize; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Localize module for translations |
31
|
|
|
* |
32
|
|
|
* @author Alejandro Peña Florentín ([email protected]) |
33
|
|
|
*/ |
34
|
|
|
class Localize extends \Tight\Modules\AbstractModule |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string Resource file type |
39
|
|
|
*/ |
40
|
|
|
private $resourceFileType; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string Current locale |
44
|
|
|
*/ |
45
|
|
|
private $locale; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var array Associative array for the current locale |
49
|
|
|
*/ |
50
|
|
|
private $values; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Constructor |
54
|
|
|
* |
55
|
|
|
* @param array|\Tight\Modules\Localize\LocalizeConfig $config |
56
|
|
|
* @throws \InvalidArgumentException If $config is not an array or an object |
57
|
|
|
* of \Tight\Modules\Localize\LocalizeConfig class |
58
|
|
|
*/ |
59
|
|
|
public function __construct($config = []) { |
60
|
|
|
if (is_array($config)) { |
61
|
|
|
$config = new \Tight\Modules\Localize\LocalizeConfig($config); |
62
|
|
|
} else if (!$config instanceof \Tight\Modules\Localize\LocalizeConfig) { |
63
|
|
|
throw new \InvalidArgumentException("Argument 1 passed to " . get_class($this) . " must be an array or an instance of Tight\Modules\Localize\LocalizeConfig"); |
64
|
|
|
} |
65
|
|
|
parent::__construct("LocalizeModule"); |
66
|
|
|
$this->setConfig($config); |
67
|
|
|
$this->setResourceFileType($this->getConfig()->resourceFileType); |
|
|
|
|
68
|
|
|
$this->checkDependences(); |
69
|
|
|
$this->setLocale($this->getConfig()->defaultLocale); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Sets the resource file type |
74
|
|
|
* @param string $resourceFileType Resource file type |
75
|
|
|
* @return \Tight\Modules\Localize\Localize Fluent setter |
76
|
|
|
*/ |
77
|
|
|
public function setResourceFileType($resourceFileType) { |
78
|
|
|
$this->resourceFileType = $resourceFileType; |
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Gets all the defined values for the current locale |
84
|
|
|
* @return array Associative array of values |
85
|
|
|
*/ |
86
|
|
|
public function getValues() { |
87
|
|
|
return $this->values; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Reloads the class with the new locale |
92
|
|
|
*/ |
93
|
|
|
public function reloadConfig() { |
94
|
|
|
$this->locale = $this->getConfig()->defaultLocale; |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Checks the dependences for the class |
99
|
|
|
* @throws \Tight\Modules\ModuleException If resource directory cant be |
100
|
|
|
* found |
101
|
|
|
*/ |
102
|
|
|
private function checkDependences() { |
103
|
|
|
if (!is_dir($this->getConfig()->resourceFolder)) { |
|
|
|
|
104
|
|
|
throw new \Tight\Modules\ModuleException("Resource directory not found"); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Sets a new locale |
110
|
|
|
* @param string $locale New defined locale |
111
|
|
|
* @throws \Tight\Modules\ModuleException If resource default resource file |
112
|
|
|
* cant be found |
113
|
|
|
*/ |
114
|
|
|
public function setLocale($locale) { |
115
|
|
|
$this->locale = $locale; |
116
|
|
|
$folder = \Tight\Utils::addTrailingSlash($this->getConfig()->resourceFolder); |
|
|
|
|
117
|
|
|
$fileName = $this->getConfig()->resourceFileName . $this->getConfig()->langSeparator . $locale . "." . $this->getConfig()->resourceFileType; |
|
|
|
|
118
|
|
|
$file = $folder . $fileName; |
119
|
|
|
if (is_file($file)) { |
120
|
|
|
$this->values = json_decode(file_get_contents($file), JSON_FORCE_OBJECT); |
121
|
|
|
} else { |
122
|
|
|
$fileName = $this->getConfig()->resourceFileName . "." . $this->getConfig()->resourceFileType; |
123
|
|
|
$file = $folder . $fileName; |
124
|
|
|
if (is_file($file)) { |
125
|
|
|
$this->values = json_decode(file_get_contents($file), JSON_FORCE_OBJECT); |
126
|
|
|
} else { |
127
|
|
|
throw new \Tight\Modules\ModuleException("Resource file <strong>" . $file . "</strong> not found"); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Gets the available locales |
134
|
|
|
* @return array Available locales |
135
|
|
|
*/ |
136
|
|
|
public function getLocales() { |
137
|
|
|
$output = []; |
138
|
|
|
$directory = $this->getConfig()->resourceFolder; |
|
|
|
|
139
|
|
|
$fileName = $this->getConfig()->resourceFileName; |
|
|
|
|
140
|
|
|
$dir = opendir($directory); |
141
|
|
|
$files = []; |
142
|
|
|
while ($entry = readdir($dir)) { |
143
|
|
|
if (strpos($entry, $fileName) !== false) { |
144
|
|
|
$files[] = $entry; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
foreach ($files as $element) { |
148
|
|
|
$file = \Tight\Utils::getSlicedFile($directory . $element); |
149
|
|
|
//Removes extension |
150
|
|
|
$name = $file["name"]; |
151
|
|
|
$explode = explode($this->getConfig()->langSeparator, $name); |
|
|
|
|
152
|
|
|
// Get the locale of the defined file type |
153
|
|
|
if ($file["ext"] == $this->getConfig()->resourceFileType) { |
|
|
|
|
154
|
|
|
if (count($explode) > 1) { |
155
|
|
|
$output[] = $explode[count($explode) - 1]; |
156
|
|
|
} else { |
157
|
|
|
$output[] = $this->getConfig()->defaultLocale; |
|
|
|
|
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
return $output; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Gets a value from a defined key |
166
|
|
|
* @param string $key Key |
167
|
|
|
* @return string Value defined for the key $key or an empty string if the |
168
|
|
|
* key is not defined |
169
|
|
|
*/ |
170
|
|
|
public function get($key) { |
171
|
|
|
if (isset($this->values[$key])) { |
172
|
|
|
return $this->values[$key]; |
173
|
|
|
} else { |
174
|
|
|
return ""; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function onConfigChange() { |
179
|
|
|
$this->reloadConfig(); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function onLoad() { |
183
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function onRemove() { |
187
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
} |
191
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.