|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Locale; |
|
4
|
|
|
|
|
5
|
|
|
use Locale as PhpLocale; |
|
6
|
|
|
use Nip_File_System; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Locale |
|
10
|
|
|
* @package Nip\Locale |
|
11
|
|
|
*/ |
|
12
|
|
|
class Locale |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
protected $supported; |
|
16
|
|
|
|
|
17
|
|
|
protected $data = []; |
|
18
|
|
|
|
|
19
|
|
|
protected $default = 'en_US'; |
|
20
|
|
|
|
|
21
|
|
|
protected $current; |
|
22
|
|
|
|
|
23
|
|
|
public function getSupported() |
|
24
|
|
|
{ |
|
25
|
|
|
if (!$this->supported) { |
|
26
|
|
|
$files = Nip_File_System::instance()->scanDirectory($this->getDataFolder()); |
|
27
|
|
|
foreach ($files as $file) { |
|
28
|
|
|
if (substr($file, 0, 1) != '_') { |
|
29
|
|
|
$name = str_replace('.php', '', $file); |
|
30
|
|
|
$this->supported[] = $name; |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
return $this->supported; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected function getDataFolder() |
|
41
|
|
|
{ |
|
42
|
|
|
return dirname(__FILE__) . '/data/'; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param array $path |
|
47
|
|
|
* @param bool $locale |
|
48
|
|
|
* @return bool|mixed |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getOption($path = [], $locale = false) |
|
51
|
|
|
{ |
|
52
|
|
|
$data = $this->getData($locale); |
|
53
|
|
|
$value = $data; |
|
54
|
|
|
$pathFlat = ''; |
|
55
|
|
|
foreach ($path as $key) { |
|
56
|
|
|
$pathFlat .= $key; |
|
57
|
|
|
if (isset ($value[$key])) { |
|
58
|
|
|
$value = $value[$key]; |
|
59
|
|
|
} else { |
|
60
|
|
|
trigger_error("invalid path [{$pathFlat}] for " . __CLASS__ . "->" . __METHOD__, E_USER_WARNING); |
|
61
|
|
|
return false; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $value; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param bool $locale |
|
70
|
|
|
* @return mixed |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getData($locale = false) |
|
73
|
|
|
{ |
|
74
|
|
|
$locale = $locale ? $locale : $this->getCurrent(); |
|
75
|
|
|
if (!isset($this->data[$locale])) { |
|
76
|
|
|
$data = $this->getDataFromFile($locale); |
|
77
|
|
|
$this->data[$locale] = $data; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $this->data[$locale]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function getCurrent() |
|
84
|
|
|
{ |
|
85
|
|
|
if (!$this->current) { |
|
86
|
|
|
$this->initCurrent(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $this->current; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param $locale |
|
94
|
|
|
*/ |
|
95
|
|
|
public function setCurrent($locale) |
|
96
|
|
|
{ |
|
97
|
|
|
if ($this->isSupported($locale)) { |
|
98
|
|
|
$this->current = $locale; |
|
99
|
|
|
} else { |
|
100
|
|
|
$this->current = $this->default; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function initCurrent() |
|
105
|
|
|
{ |
|
106
|
|
|
$locale = $this->getFromINI(); |
|
107
|
|
|
if ($this->isSupported($locale)) { |
|
108
|
|
|
$this->setCurrent($locale); |
|
109
|
|
|
} else { |
|
110
|
|
|
$this->setCurrent($this->default); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return string |
|
116
|
|
|
*/ |
|
117
|
|
|
public function getFromINI() |
|
118
|
|
|
{ |
|
119
|
|
|
if (class_exists('Locale', false)) { |
|
120
|
|
|
return PhpLocale::getDefault(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return setlocale(LC_TIME, 0); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param $name |
|
128
|
|
|
* @return bool |
|
129
|
|
|
*/ |
|
130
|
|
|
public function isSupported($name) |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->hasDataFile($name); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param $name |
|
137
|
|
|
* @return bool |
|
138
|
|
|
*/ |
|
139
|
|
|
protected function hasDataFile($name) |
|
140
|
|
|
{ |
|
141
|
|
|
return is_file($this->getDataFile($name)); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param $name |
|
146
|
|
|
* @return string |
|
147
|
|
|
*/ |
|
148
|
|
|
protected function getDataFile($name) |
|
149
|
|
|
{ |
|
150
|
|
|
return $this->getDataFolder() . $name . '.php'; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @param $name |
|
155
|
|
|
* @param array $data |
|
156
|
|
|
* @return array |
|
157
|
|
|
*/ |
|
158
|
|
|
protected function getDataFromFile($name, $data = []) |
|
159
|
|
|
{ |
|
160
|
|
|
$file = $this->getDataFile($name); |
|
161
|
|
|
|
|
162
|
|
|
if (is_file($file)) { |
|
163
|
|
|
include $file; |
|
164
|
|
|
if (isset ($_import)) { |
|
|
|
|
|
|
165
|
|
|
$data = $this->getDataFromFile($_import); |
|
166
|
|
|
} |
|
167
|
|
|
if (isset ($_data)) { |
|
|
|
|
|
|
168
|
|
|
$data = \Nip\HelperBroker::get('Arrays')->merge_distinct($data, $_data); |
|
|
|
|
|
|
169
|
|
|
} |
|
170
|
|
|
} else { |
|
171
|
|
|
trigger_error("no locale data file at [{$file}]", E_USER_NOTICE); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
return $data; |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
This check looks for calls to
isset(...)orempty()on variables that are yet undefined. These calls will always produce the same result and can be removed.This is most likely caused by the renaming of a variable or the removal of a function/method parameter.