1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Locales class file |
4
|
|
|
* |
5
|
|
|
* @package EBloodBank |
6
|
|
|
* @since 1.0 |
7
|
|
|
*/ |
8
|
|
|
namespace EBloodBank; |
9
|
|
|
|
10
|
|
|
use GlobIterator; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Locales class |
14
|
|
|
* |
15
|
|
|
* @since 1.0 |
16
|
|
|
*/ |
17
|
|
|
class Locales |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* The default locale. |
21
|
|
|
* |
22
|
|
|
* @var Locale |
23
|
|
|
* @since 1.0 |
24
|
|
|
* @static |
25
|
|
|
*/ |
26
|
|
|
protected static $defaultLocale; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The current locale. |
30
|
|
|
* |
31
|
|
|
* @var Locale |
32
|
|
|
* @since 1.0 |
33
|
|
|
* @static |
34
|
|
|
*/ |
35
|
|
|
protected static $currentLocale; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get the default locale. |
39
|
|
|
* |
40
|
|
|
* @return Locale |
41
|
|
|
* @since 1.0 |
42
|
|
|
* @static |
43
|
|
|
*/ |
44
|
|
|
public static function getDefaultLocale() |
45
|
|
|
{ |
46
|
|
|
return self::$defaultLocale; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get the current locale, or the default locale if it's not specified. |
51
|
|
|
* |
52
|
|
|
* @return Locale |
53
|
|
|
* @since 1.0 |
54
|
|
|
* @static |
55
|
|
|
*/ |
56
|
|
|
public static function getCurrentLocale() |
57
|
|
|
{ |
58
|
|
|
if (empty(self::$currentLocale)) { |
59
|
|
|
return self::$defaultLocale; |
60
|
|
|
} else { |
61
|
|
|
return self::$currentLocale; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Whether the given locale is the default. |
67
|
|
|
* |
68
|
|
|
* @return bool |
69
|
|
|
* @since 1.0 |
70
|
|
|
* @static |
71
|
|
|
*/ |
72
|
|
|
public static function isDefaultLocale(Locale $locale) |
73
|
|
|
{ |
74
|
|
|
$defaultLocale = self::getDefaultLocale(); |
75
|
|
|
|
76
|
|
|
if (empty($defaultLocale)) { |
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return ($defaultLocale->getCode() === $locale->getCode()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Whether the given locale is the current. |
85
|
|
|
* |
86
|
|
|
* @return bool |
87
|
|
|
* @since 1.0 |
88
|
|
|
* @static |
89
|
|
|
*/ |
90
|
|
|
public static function isCurrentLocale(Locale $locale) |
91
|
|
|
{ |
92
|
|
|
$currentLocale = self::getCurrentLocale(); |
93
|
|
|
|
94
|
|
|
if (empty($currentLocale)) { |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return ($currentLocale->getCode() === $locale->getCode()); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Set the default locale. |
103
|
|
|
* |
104
|
|
|
* @return void |
105
|
|
|
* @since 1.0 |
106
|
|
|
* @static |
107
|
|
|
*/ |
108
|
|
|
public static function setDefaultLocale(Locale $locale) |
109
|
|
|
{ |
110
|
|
|
self::$defaultLocale = $locale; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set the current locale. |
115
|
|
|
* |
116
|
|
|
* @return void |
117
|
|
|
* @since 1.0 |
118
|
|
|
* @static |
119
|
|
|
*/ |
120
|
|
|
public static function setCurrentLocale(Locale $locale) |
121
|
|
|
{ |
122
|
|
|
self::$currentLocale = $locale; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Find a locale. |
127
|
|
|
* |
128
|
|
|
* @return Locale|null |
129
|
|
|
* @since 1.3 |
130
|
|
|
* @static |
131
|
|
|
*/ |
132
|
|
|
public static function findLocale($code, $dirPath = '') |
133
|
|
|
{ |
134
|
|
|
if (empty($code)) { |
135
|
|
|
return; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if (empty($dirPath)) { |
139
|
|
|
$dirPath = EBB_LOCALES_DIR; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$dirPath = trimTrailingSlash($dirPath); |
143
|
|
|
$localePath = realpath("{$dirPath}/{$code}.mo"); |
144
|
|
|
|
145
|
|
|
if (! $localePath || ! is_readable($localePath)) { |
146
|
|
|
return; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if (dirname($localePath) !== realpath($dirPath)) { |
150
|
|
|
return; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return new Locale($code, $localePath); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get the available locales. |
158
|
|
|
* |
159
|
|
|
* @return Locale[] |
160
|
|
|
* @since 1.0 |
161
|
|
|
* @static |
162
|
|
|
*/ |
163
|
|
|
public static function getAvailableLocales($dirPath = '') |
164
|
|
|
{ |
165
|
|
|
$locales = []; |
166
|
|
|
|
167
|
|
|
if (empty($dirPath)) { |
168
|
|
|
$dirPath = EBB_LOCALES_DIR; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$dirPath = trimTrailingSlash($dirPath); |
172
|
|
|
|
173
|
|
|
foreach (new GlobIterator("{$dirPath}/*.mo") as $moFile) { |
174
|
|
|
if ($moFile->isFile() && $moFile->isReadable()) { |
175
|
|
|
$moFilePath = $moFile->getRealPath(); |
176
|
|
|
$localeCode = $moFile->getBasename('.mo'); |
177
|
|
|
$locales[$localeCode] = new Locale($localeCode, $moFilePath); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $locales; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|