1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Locale; |
4
|
|
|
|
5
|
|
|
use Locale as PhpLocale; |
6
|
|
|
use Nip\Locale\Traits\HasSupportedTrait; |
7
|
|
|
use Nip_File_System; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Locale |
11
|
|
|
* @package Nip\Locale |
12
|
|
|
*/ |
13
|
|
|
class Locale |
14
|
|
|
{ |
15
|
|
|
use HasSupportedTrait; |
16
|
|
|
|
17
|
|
|
protected $data = []; |
18
|
|
|
|
19
|
|
|
protected $default = 'en_US'; |
20
|
|
|
|
21
|
|
|
protected $current; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
2 |
|
protected function getDataFolder() |
28
|
|
|
{ |
29
|
2 |
|
return dirname(dirname(__FILE__)) . '/data/'; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string[] $path |
34
|
|
|
* @param bool $locale |
35
|
|
|
* @return bool|mixed |
36
|
|
|
*/ |
37
|
1 |
|
public function getOption($path = [], $locale = false) |
38
|
|
|
{ |
39
|
1 |
|
$data = $this->getData($locale); |
40
|
1 |
|
$value = $data; |
41
|
1 |
|
$pathFlat = ''; |
42
|
1 |
|
foreach ($path as $key) { |
43
|
1 |
|
$pathFlat .= $key; |
44
|
1 |
|
if (isset($value[$key])) { |
45
|
1 |
|
$value = $value[$key]; |
46
|
|
|
} else { |
47
|
|
|
trigger_error("invalid path [{$pathFlat}] for " . __CLASS__ . "->" . __METHOD__, E_USER_WARNING); |
48
|
1 |
|
return false; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
return $value; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param bool $locale |
57
|
|
|
* @return mixed |
58
|
|
|
*/ |
59
|
1 |
|
public function getData($locale = false) |
60
|
|
|
{ |
61
|
1 |
|
$locale = $locale ? $locale : $this->getCurrent(); |
62
|
1 |
|
if (!isset($this->data[$locale])) { |
63
|
1 |
|
$data = $this->getDataFromFile($locale); |
64
|
1 |
|
$this->data[$locale] = $data; |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
return $this->data[$locale]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getCurrent() |
71
|
|
|
{ |
72
|
|
|
if (!$this->current) { |
73
|
|
|
$this->initCurrent(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $this->current; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $locale |
81
|
|
|
*/ |
82
|
|
|
public function setCurrent($locale) |
83
|
|
|
{ |
84
|
|
|
if ($this->isSupported($locale)) { |
85
|
|
|
$this->current = $locale; |
86
|
|
|
} else { |
87
|
|
|
$this->current = $this->default; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function initCurrent() |
92
|
|
|
{ |
93
|
|
|
$locale = $this->getFromINI(); |
94
|
|
|
if ($this->isSupported($locale)) { |
95
|
|
|
$this->setCurrent($locale); |
96
|
|
|
} else { |
97
|
|
|
$this->setCurrent($this->default); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function getFromINI() |
105
|
|
|
{ |
106
|
|
|
if (class_exists('Locale', false)) { |
107
|
|
|
return PhpLocale::getDefault(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return setlocale(LC_TIME, 0); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param $name |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
|
|
protected function hasDataFile($name) |
118
|
|
|
{ |
119
|
|
|
return is_file($this->getDataFile($name)); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param $name |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
1 |
|
protected function getDataFile($name) |
127
|
|
|
{ |
128
|
1 |
|
return $this->getDataFolder() . $name . '.php'; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param $name |
133
|
|
|
* @param array $data |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
1 |
|
protected function getDataFromFile($name, $data = []) |
137
|
|
|
{ |
138
|
1 |
|
$file = $this->getDataFile($name); |
139
|
|
|
|
140
|
1 |
|
if (is_file($file)) { |
141
|
1 |
|
include $file; |
142
|
1 |
|
if (isset($_import)) { |
|
|
|
|
143
|
1 |
|
$data = $this->getDataFromFile($_import); |
144
|
|
|
} |
145
|
1 |
|
if (isset($_data)) { |
|
|
|
|
146
|
1 |
|
$data = \Nip\HelperBroker::get('Arrays')->merge_distinct($data, $_data); |
|
|
|
|
147
|
|
|
} |
148
|
|
|
} else { |
149
|
|
|
trigger_error("no locale data file at [{$file}]", E_USER_NOTICE); |
150
|
|
|
} |
151
|
|
|
|
152
|
1 |
|
return $data; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|