|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* PayPal Donation extension for the phpBB Forum Software package. |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright (c) 2015-2020 Skouat |
|
7
|
|
|
* @license GNU General Public License, version 2 (GPL-2.0) |
|
8
|
|
|
* |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace skouat\ppde\actions; |
|
12
|
|
|
|
|
13
|
|
|
use phpbb\config\config; |
|
14
|
|
|
use phpbb\template\template; |
|
15
|
|
|
use phpbb\user; |
|
16
|
|
|
|
|
17
|
|
|
class locale_icu |
|
18
|
|
|
{ |
|
19
|
|
|
protected $config; |
|
20
|
|
|
protected $template; |
|
21
|
|
|
protected $user; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* locale_icu constructor. |
|
25
|
|
|
* |
|
26
|
|
|
* @param config $config |
|
27
|
|
|
* @param template $template Template object |
|
28
|
|
|
* @param user $user User object |
|
29
|
|
|
* |
|
30
|
|
|
* @access public |
|
31
|
|
|
*/ |
|
32
|
|
|
|
|
33
|
|
|
public function __construct(config $config, template $template, user $user) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->config = $config; |
|
36
|
|
|
$this->template = $template; |
|
37
|
|
|
$this->user = $user; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Build pull down menu options of available locales |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $config_value Locale identifier; default: '' |
|
44
|
|
|
* |
|
45
|
|
|
* @return void |
|
46
|
|
|
* @access public |
|
47
|
|
|
*/ |
|
48
|
|
|
public function build_locale_select_menu($config_value = ''): void |
|
49
|
|
|
{ |
|
50
|
|
|
if (!$this->icu_requirements()) |
|
51
|
|
|
{ |
|
52
|
|
|
return; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
// Grab the list of all available locales |
|
56
|
|
|
$locale_list = $this->get_locale_list(); |
|
57
|
|
|
|
|
58
|
|
|
$this->process_locales($locale_list, $config_value); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Process each locale item for pull-down |
|
63
|
|
|
* |
|
64
|
|
|
* @param array $locale_list |
|
65
|
|
|
* @param string $config_value |
|
66
|
|
|
*/ |
|
67
|
|
|
private function process_locales(array $locale_list, string $config_value): void |
|
68
|
|
|
{ |
|
69
|
|
|
foreach ($locale_list as $locale => $locale_name) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->assign_locale_to_template($locale, $locale_name, $config_value); |
|
72
|
|
|
} |
|
73
|
|
|
unset ($locale); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Assign locale options to the template |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $locale |
|
80
|
|
|
* @param string $locale_name |
|
81
|
|
|
* @param string $config_value |
|
82
|
|
|
*/ |
|
83
|
|
|
private function assign_locale_to_template(string $locale, string $locale_name, string $config_value): void |
|
84
|
|
|
{ |
|
85
|
|
|
$this->template->assign_block_vars('locale_options', [ |
|
86
|
|
|
'LOCALE_ID' => $locale, |
|
87
|
|
|
'LOCALE_NAME' => $locale_name, |
|
88
|
|
|
'S_LOCALE_DEFAULT' => $config_value === $locale, |
|
89
|
|
|
]); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Checks if the PHP PECL intl extension is fully available |
|
94
|
|
|
* |
|
95
|
|
|
* @return bool |
|
96
|
|
|
* @access public |
|
97
|
|
|
*/ |
|
98
|
|
|
public function icu_requirements(): bool |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->config['ppde_intl_version_valid'] && $this->config['ppde_intl_detected']; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Build an array of all locales |
|
105
|
|
|
* |
|
106
|
|
|
* @return array |
|
107
|
|
|
* @access private |
|
108
|
|
|
*/ |
|
109
|
|
|
private function get_locale_list(): array |
|
110
|
|
|
{ |
|
111
|
|
|
$locale_items = \ResourceBundle::getLocales(''); |
|
112
|
|
|
foreach ($locale_items as $locale) |
|
113
|
|
|
{ |
|
114
|
|
|
$locale_ary[$locale] = \Locale::getDisplayName($locale, $this->user->lang_name); |
|
115
|
|
|
} |
|
116
|
|
|
unset ($locale_items); |
|
117
|
|
|
|
|
118
|
|
|
natsort($locale_ary); |
|
119
|
|
|
|
|
120
|
|
|
return $locale_ary; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Gets the default Locale |
|
125
|
|
|
* |
|
126
|
|
|
* @return string A string with the current Locale. |
|
127
|
|
|
*/ |
|
128
|
|
|
public function locale_get_default(): string |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->icu_requirements() ? \locale_get_default() : ''; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Gets the currency symbol based on ISO code |
|
135
|
|
|
* |
|
136
|
|
|
* @param $currency_iso_code |
|
137
|
|
|
* |
|
138
|
|
|
* @return string |
|
139
|
|
|
* @access public |
|
140
|
|
|
*/ |
|
141
|
|
|
public function get_currency_symbol($currency_iso_code): string |
|
142
|
|
|
{ |
|
143
|
|
|
$fmt = new \NumberFormatter($this->config['ppde_default_locale'] . '@currency=' . $currency_iso_code, \NumberFormatter::CURRENCY); |
|
144
|
|
|
return $fmt->getSymbol(\NumberFormatter::CURRENCY_SYMBOL); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Checks if the PPDE locale feature is configured |
|
149
|
|
|
* |
|
150
|
|
|
* @return bool |
|
151
|
|
|
* @access public |
|
152
|
|
|
*/ |
|
153
|
|
|
public function is_locale_configured(): bool |
|
154
|
|
|
{ |
|
155
|
|
|
return $this->icu_requirements() && !empty($this->config['ppde_default_locale']); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Creates a number formatter |
|
160
|
|
|
* |
|
161
|
|
|
* @return \NumberFormatter NumberFormatter object or FALSE on error. |
|
162
|
|
|
* @access public |
|
163
|
|
|
*/ |
|
164
|
|
|
public function numfmt_create() |
|
165
|
|
|
{ |
|
166
|
|
|
return numfmt_create($this->config['ppde_default_locale'], \NumberFormatter::CURRENCY); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Format a currency value |
|
171
|
|
|
* |
|
172
|
|
|
* @param \NumberFormatter $fmt |
|
173
|
|
|
* @param float $value |
|
174
|
|
|
* @param string $currency_iso_code |
|
175
|
|
|
* |
|
176
|
|
|
* @return false|string |
|
177
|
|
|
* @access public |
|
178
|
|
|
*/ |
|
179
|
|
|
public function numfmt_format_currency($fmt, $value, $currency_iso_code) |
|
180
|
|
|
{ |
|
181
|
|
|
return numfmt_format_currency($fmt, (float) $value, (string) $currency_iso_code); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Sets config value for PHP Intl extension version |
|
186
|
|
|
* |
|
187
|
|
|
* @return void |
|
188
|
|
|
* @throws \ReflectionException |
|
189
|
|
|
* @access public |
|
190
|
|
|
*/ |
|
191
|
|
|
public function set_intl_info(): void |
|
192
|
|
|
{ |
|
193
|
|
|
$this->config->set('ppde_intl_version', $this->get_php_extension_version('intl', $this->icu_available_features())); |
|
194
|
|
|
$this->config->set('ppde_intl_version_valid', (int) $this->icu_version_compare()); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Gets extension version |
|
199
|
|
|
* |
|
200
|
|
|
* @param string $name |
|
201
|
|
|
* @param bool $proceed |
|
202
|
|
|
* |
|
203
|
|
|
* @return string |
|
204
|
|
|
* @throws \ReflectionException |
|
205
|
|
|
* @access private |
|
206
|
|
|
*/ |
|
207
|
|
|
private function get_php_extension_version($name, $proceed): string |
|
208
|
|
|
{ |
|
209
|
|
|
$version = ''; |
|
210
|
|
|
if ($proceed) |
|
211
|
|
|
{ |
|
212
|
|
|
$ext = new \ReflectionExtension($name); |
|
213
|
|
|
$version = $ext->getVersion(); |
|
214
|
|
|
} |
|
215
|
|
|
return $version; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Checks if the required function/class are available. |
|
220
|
|
|
* |
|
221
|
|
|
* @return bool |
|
222
|
|
|
* @access private |
|
223
|
|
|
*/ |
|
224
|
|
|
private function icu_available_features(): bool |
|
225
|
|
|
{ |
|
226
|
|
|
return class_exists(\ResourceBundle::class) && function_exists('\locale_get_default'); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Checks if ICU version matches with requirement |
|
231
|
|
|
* |
|
232
|
|
|
* @return bool|int |
|
233
|
|
|
* @throws \ReflectionException |
|
234
|
|
|
* @access private |
|
235
|
|
|
*/ |
|
236
|
|
|
private function icu_version_compare() |
|
237
|
|
|
{ |
|
238
|
|
|
$icu_min_version = '1.1.0'; |
|
239
|
|
|
$icu_version = $this->get_php_extension_version('intl', $this->icu_available_features()); |
|
240
|
|
|
return version_compare($icu_version, $icu_min_version, '>='); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* Sets config value for cURL and fsockopen |
|
245
|
|
|
* |
|
246
|
|
|
* @return void |
|
247
|
|
|
* @access public |
|
248
|
|
|
*/ |
|
249
|
|
|
public function set_intl_detected(): void |
|
250
|
|
|
{ |
|
251
|
|
|
$this->config->set('ppde_intl_detected', $this->icu_available_features()); |
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
|