|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Locale class file |
|
4
|
|
|
* |
|
5
|
|
|
* @package EBloodBank |
|
6
|
|
|
* @since 1.0 |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace EBloodBank; |
|
9
|
|
|
|
|
10
|
|
|
use Gettext; |
|
11
|
|
|
use InvalidArgumentException; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Locale class |
|
15
|
|
|
* |
|
16
|
|
|
* @since 1.0 |
|
17
|
|
|
*/ |
|
18
|
|
|
class Locale |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* The locale code. |
|
22
|
|
|
* |
|
23
|
|
|
* @var string |
|
24
|
|
|
* @since 1.0 |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $code; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The locale MO file path. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
* @since 1.0 |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $moFilePath; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The locale PO file path. |
|
38
|
|
|
* |
|
39
|
|
|
* @var string |
|
40
|
|
|
* @since 1.0 |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $poFilePath; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The locale translations. |
|
46
|
|
|
* |
|
47
|
|
|
* @var \Gettext\Translations |
|
48
|
|
|
* @since 1.0 |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $translations; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* The locale language code. |
|
54
|
|
|
* |
|
55
|
|
|
* @var string |
|
56
|
|
|
* @since 1.0 |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $languageCode; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* The locale text direction. |
|
62
|
|
|
* |
|
63
|
|
|
* @var string |
|
64
|
|
|
* @since 1.0 |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $textDirection; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @throws \InvalidArgumentException |
|
70
|
|
|
* @return void |
|
71
|
|
|
* @since 1.0 |
|
72
|
|
|
*/ |
|
73
|
|
|
public function __construct($code, $moFile, $poFile = '') |
|
74
|
|
|
{ |
|
75
|
|
|
if (! empty($code) && strlen($code) >= 2) { |
|
76
|
|
|
$this->code = $code; |
|
77
|
|
|
} else { |
|
78
|
|
|
throw new InvalidArgumentException(__('Invalid locale code.')); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if (! empty($moFile) && is_readable($moFile)) { |
|
82
|
|
|
$this->moFilePath = $moFile; |
|
83
|
|
|
} else { |
|
84
|
|
|
throw new InvalidArgumentException(__('Locale MO file is not readable or not exists.')); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if (! empty($poFile)) { |
|
88
|
|
|
if (is_readable($poFile)) { |
|
89
|
|
|
$this->poFilePath = $poFile; |
|
90
|
|
|
} else { |
|
91
|
|
|
throw new InvalidArgumentException(__('Locale PO file is not readable or not exists.')); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Get the locale code. |
|
98
|
|
|
* |
|
99
|
|
|
* @return string |
|
100
|
|
|
* @since 1.0 |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getCode() |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->code; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Get the locale MO file path. |
|
109
|
|
|
* |
|
110
|
|
|
* @return string |
|
111
|
|
|
* @since 1.0 |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getMOFilePath() |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->moFilePath; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Get the locale PO file path. |
|
120
|
|
|
* |
|
121
|
|
|
* @return string |
|
122
|
|
|
* @since 1.0 |
|
123
|
|
|
*/ |
|
124
|
|
|
public function getPOFilePath() |
|
125
|
|
|
{ |
|
126
|
|
|
if (is_null($this->poFilePath)) { |
|
|
|
|
|
|
127
|
|
|
$moFilePath = $this->getMOFilePath(); |
|
128
|
|
|
$poFilePath = dirname($moFilePath) . '/' . basename($moFilePath, '.mo') . '.po'; |
|
129
|
|
|
if (is_readable($poFilePath)) { |
|
130
|
|
|
$this->poFilePath = $poFilePath; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
return $this->poFilePath; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Get the locale translations. |
|
138
|
|
|
* |
|
139
|
|
|
* @return \Gettext\Translations |
|
140
|
|
|
* @since 1.0 |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getTranslations() |
|
143
|
|
|
{ |
|
144
|
|
|
if (is_null($this->translations)) { |
|
145
|
|
|
$this->translations = Gettext\Extractors\Mo::fromFile($this->getMOFilePath()); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return $this->translations; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Get the language code. |
|
153
|
|
|
* |
|
154
|
|
|
* @return string |
|
155
|
|
|
* @since 1.0 |
|
156
|
|
|
*/ |
|
157
|
|
|
public function getLanguageCode() |
|
158
|
|
|
{ |
|
159
|
|
|
if (is_null($this->languageCode)) { |
|
|
|
|
|
|
160
|
|
|
$translations = $this->getTranslations(); |
|
161
|
|
|
|
|
162
|
|
|
if (! empty($translations)) { |
|
163
|
|
|
$translation = $translations->find('language code', 'en'); |
|
164
|
|
|
if (! empty($translation) && $translation->hasTranslation()) { |
|
165
|
|
|
$this->languageCode = $translation->getTranslation(); |
|
166
|
|
|
} elseif ($translations->hasLanguage()) { |
|
167
|
|
|
$this->languageCode = $translations->getLanguage(); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
return $this->languageCode; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Get the text direction. |
|
177
|
|
|
* |
|
178
|
|
|
* @return string |
|
179
|
|
|
* @since 1.0 |
|
180
|
|
|
*/ |
|
181
|
|
|
public function getTextDirection() |
|
182
|
|
|
{ |
|
183
|
|
|
if (is_null($this->textDirection)) { |
|
|
|
|
|
|
184
|
|
|
$translations = $this->getTranslations(); |
|
185
|
|
|
|
|
186
|
|
|
if (! empty($translations)) { |
|
187
|
|
|
$translation = $translations->find('text direction', 'ltr'); |
|
188
|
|
|
if (! empty($translation) && $translation->hasTranslation()) { |
|
189
|
|
|
$this->textDirection = $translation->getTranslation(); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
if (empty($this->textDirection)) { |
|
194
|
|
|
$this->textDirection = 'ltr'; // The default text direction. |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
return $this->textDirection; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|