|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Giggsey\Locale; |
|
4
|
|
|
|
|
5
|
|
|
class Locale |
|
6
|
|
|
{ |
|
7
|
|
|
protected static $dataDir = '../data/'; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Gets the primary language for the input locale |
|
11
|
|
|
* |
|
12
|
|
|
* @param string $locale Input locale (e.g. en-GB) |
|
13
|
|
|
* @return string Primary Language (e.g. en) |
|
14
|
|
|
*/ |
|
15
|
|
|
public static function getPrimaryLanguage($locale) |
|
16
|
|
|
{ |
|
17
|
|
|
$parts = explode('-', str_replace('_', '-', $locale)); |
|
18
|
|
|
|
|
19
|
|
|
return strtolower($parts[0]); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Get the region for the input locale |
|
24
|
|
|
* |
|
25
|
|
|
* @param string $locale Input locale (e.g. de-CH-1991) |
|
26
|
|
|
* @return string Region (e.g. CH) |
|
27
|
|
|
*/ |
|
28
|
|
|
public static function getRegion($locale) |
|
29
|
|
|
{ |
|
30
|
|
|
$parts = explode('-', str_replace('_', '-', $locale)); |
|
31
|
|
|
|
|
32
|
|
|
if (count($parts) === 1) { |
|
33
|
|
|
return ''; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$region = end($parts); |
|
37
|
|
|
|
|
38
|
|
|
if (strlen($region) === 4) { |
|
39
|
|
|
return ''; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
if ($region === 'POSIX') { |
|
43
|
|
|
$region = 'US'; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return strtoupper($region); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get the localised display name for the region of the input locale |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $locale The locale to return a display region for |
|
53
|
|
|
* @param string $inLocale Format locale to display the region name |
|
54
|
|
|
* @return string Display name for the region, or an empty string if no result could be found |
|
55
|
|
|
*/ |
|
56
|
|
|
public static function getDisplayRegion($locale, $inLocale) |
|
57
|
|
|
{ |
|
58
|
|
|
$dataDir = __DIR__ . DIRECTORY_SEPARATOR . static::$dataDir; |
|
59
|
|
|
|
|
60
|
|
|
// Convert $locale into a region |
|
61
|
|
|
$region = static::getRegion($locale); |
|
62
|
|
|
|
|
63
|
|
|
$regionList = require $dataDir . '_list.php'; |
|
64
|
|
|
|
|
65
|
|
|
/* |
|
66
|
|
|
* Loop through each part of the $inLocale, and see if we have data for that locale |
|
67
|
|
|
* |
|
68
|
|
|
* E.g zh-Hans-HK will look for zh-Hanks-HK, zh-Hanks, then finally zh |
|
69
|
|
|
*/ |
|
70
|
|
|
$fallbackParts = explode('-', str_replace('_', '-', $inLocale)); |
|
71
|
|
|
$filesToSearch = array(); |
|
72
|
|
|
|
|
73
|
|
|
$i = count($fallbackParts); |
|
74
|
|
|
while ($i > 0) { |
|
75
|
|
|
$searchLocale = strtolower(implode('-', $fallbackParts)); |
|
76
|
|
|
|
|
77
|
|
|
if (isset($regionList[$searchLocale])) { |
|
78
|
|
|
$filesToSearch[] = $searchLocale; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
array_pop($fallbackParts); |
|
82
|
|
|
$i--; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/* |
|
86
|
|
|
* Load data files, and load the region (if it exists) from it |
|
87
|
|
|
*/ |
|
88
|
|
|
|
|
89
|
|
|
foreach ($filesToSearch as $fileToSearch) { |
|
90
|
|
|
// Load data file |
|
91
|
|
|
$data = require $dataDir . $fileToSearch . '.php'; |
|
92
|
|
|
|
|
93
|
|
|
if (isset($data[$region])) { |
|
94
|
|
|
return $data[$region]; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return ''; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public static function getVersion() |
|
102
|
|
|
{ |
|
103
|
|
|
$file = __DIR__ . DIRECTORY_SEPARATOR . static::$dataDir . '_version.php'; |
|
104
|
|
|
|
|
105
|
|
|
return require $file; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Return a list of all the supported locales |
|
110
|
|
|
* |
|
111
|
|
|
* @return string[] |
|
112
|
|
|
*/ |
|
113
|
|
|
public static function getSupportedLocales() |
|
114
|
|
|
{ |
|
115
|
|
|
$dataDir = __DIR__ . DIRECTORY_SEPARATOR . static::$dataDir; |
|
116
|
|
|
$regionList = require $dataDir . '_list.php'; |
|
117
|
|
|
|
|
118
|
|
|
return array_keys($regionList); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Load a list of all countries supported by a particular Locale |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $locale |
|
125
|
|
|
* @return string[] Associative array of Country Code => Country Name |
|
126
|
|
|
* @throws \RuntimeException On an invalid region |
|
127
|
|
|
*/ |
|
128
|
|
|
public static function getAllCountriesForLocale($locale) |
|
129
|
|
|
{ |
|
130
|
|
|
$dataDir = __DIR__ . DIRECTORY_SEPARATOR . static::$dataDir; |
|
131
|
|
|
$regionList = require $dataDir . '_list.php'; |
|
132
|
|
|
|
|
133
|
|
|
if (!isset($regionList[$locale])) { |
|
134
|
|
|
throw new \RuntimeException("Locale is not supported"); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/* |
|
138
|
|
|
* Loop through each part of the $locale, and load data for that locale |
|
139
|
|
|
* |
|
140
|
|
|
* E.g zh-Hans-HK will look for zh-Hanks-HK, zh-Hanks, then finally zh |
|
141
|
|
|
*/ |
|
142
|
|
|
$fallbackParts = explode('-', str_replace('_', '-', $locale)); |
|
143
|
|
|
$filesToSearch = array(); |
|
144
|
|
|
|
|
145
|
|
|
$i = count($fallbackParts); |
|
146
|
|
|
while ($i > 0) { |
|
147
|
|
|
$searchLocale = strtolower(implode('-', $fallbackParts)); |
|
148
|
|
|
|
|
149
|
|
|
if (isset($regionList[$searchLocale])) { |
|
150
|
|
|
$filesToSearch[] = $searchLocale; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
array_pop($fallbackParts); |
|
154
|
|
|
$i--; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/* |
|
158
|
|
|
* Load data files, and load the region (if it exists) from it |
|
159
|
|
|
*/ |
|
160
|
|
|
|
|
161
|
|
|
$returnData = array(); |
|
162
|
|
|
|
|
163
|
|
|
foreach ($filesToSearch as $fileToSearch) { |
|
164
|
|
|
// Load data file |
|
165
|
|
|
$data = require $dataDir . $fileToSearch . '.php'; |
|
166
|
|
|
|
|
167
|
|
|
$returnData += $data; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
ksort($returnData); |
|
171
|
|
|
|
|
172
|
|
|
return $returnData; |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|