PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.21
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.21
1.6.6 1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 All 49 releases
fluent-cart / vendor / wpfluent / framework / src / WPFluent / Support / Locale.php

Locale.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.21, at vendor/wpfluent/framework/src/WPFluent/Support/Locale.php

532 lines 12.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\Framework\Support;
4
5 class Locale
6 {
7 /**
8 * Locale identifier.
9 *
10 * @var string
11 */
12 protected $locale = 'en_US';
13
14 /**
15 * Locale info.
16 *
17 * @var \stdClass
18 */
19 protected $localeInfo = null;
20
21 /**
22 * English month names for keys.
23 * @var array
24 */
25 protected static $monthNames = [
26 'January', 'February', 'March', 'April', 'May', 'June', 'July',
27 'August', 'September', 'October', 'November', 'December',
28 ];
29
30 /**
31 * English weekday names for keys.
32 * @var array
33 */
34 protected static $weekdayNames = [
35 'Sunday', 'Monday', 'Tuesday', 'Wednesday',
36 'Thursday', 'Friday', 'Saturday'
37 ];
38
39 /**
40 * Construct the class.
41 *
42 * @param int|string|null $userIdOrLocale
43 */
44 public function __construct($userIdOrLocale = null)
45 {
46 $this->localeInfo = $this->populateLocaleInfo($userIdOrLocale);
47 }
48
49 /**
50 * Load the locale info of a user.
51 *
52 * @param int|null $userIdOrLocale
53 * @return \stdClass
54 */
55 protected function populateLocaleInfo($userIdOrLocale = null)
56 {
57 if ($userIdOrLocale) {
58 if (is_int($userIdOrLocale)) {
59 $this->locale = get_user_locale($userIdOrLocale);
60 } else {
61 $this->locale = $userIdOrLocale;
62 }
63 } else {
64 $this->locale = get_user_locale(get_current_user_id());
65 }
66
67 $didSwitch = false;
68 $originalLocale = get_locale();
69
70 if ($originalLocale !== $this->locale) {
71 switch_to_locale($this->locale);
72 $didSwitch = true;
73 }
74
75 try {
76 $userLocale = new \stdClass();
77
78 $wpLocale = $GLOBALS['wp_locale'];
79
80 // Weekdays
81 $userLocale->weekday = $wpLocale->weekday;
82
83 // Rebuild array with weekday names as keys
84 $userLocale->weekday_initial = array_combine(
85 static::$weekdayNames, $wpLocale->weekday_initial
86 );
87
88 // Rebuild array with weekday names as keys
89 $userLocale->weekday_abbrev = array_combine(
90 static::$weekdayNames, $wpLocale->weekday_abbrev
91 );
92
93 // Months with exact keys (01-12)
94 $monthNumbers = array_keys($wpLocale->month);
95
96 $userLocale->month = array_combine(
97 $monthNumbers, $wpLocale->month
98 );
99
100 $userLocale->month_genitive = array_combine(
101 $monthNumbers, $wpLocale->month_genitive
102 );
103
104 // Rebuild month_abbrev with english month names as keys
105 $userLocale->month_abbrev = array_combine(
106 static::$monthNames, $wpLocale->month_abbrev
107 );
108
109 // Meridiem
110 $userLocale->meridiem = [
111 'am' => $wpLocale->meridiem['am'],
112 'pm' => $wpLocale->meridiem['pm'],
113 'AM' => $wpLocale->meridiem['AM'],
114 'PM' => $wpLocale->meridiem['PM'],
115 ];
116
117 // Text Direction and other locale-specific settings
118 $userLocale->text_direction = $wpLocale->text_direction;
119 $userLocale->number_format = $wpLocale->number_format;
120 $userLocale->list_item_separator = $wpLocale->list_item_separator;
121 $userLocale->word_count_type = $wpLocale->word_count_type;
122 $userLocale->start_day_of_week = get_option('start_of_week');
123
124 $userLocale->id = $this->locale;
125
126 $userLocale->mappings = [
127 // Add the mapping of month index to number
128 'month_index_to_num' => array_combine(
129 array_keys(static::$monthNames),
130 array_map('intval', $monthNumbers)
131 ),
132 // Add the english names
133 'month' => static::$monthNames,
134 'weekday' => static::$weekdayNames
135 ];
136
137 return $userLocale;
138 } finally {
139 if ($didSwitch) {
140 restore_previous_locale();
141 }
142 }
143 }
144
145 /**
146 * Switch the locale for the user.
147 *
148 * @param string $locale
149 * @return void
150 */
151 public function switch($locale)
152 {
153 $original = get_locale();
154
155 $this->localeInfo = static::getInfo(
156 $this->locale = $locale
157 );
158
159 switch_to_locale($original);
160 }
161
162 /**
163 * Restore to the user's original locale.
164 *
165 * @return void
166 */
167 public function restore()
168 {
169 $original = get_locale();
170
171 $this->localeInfo = static::getInfo(
172 $this->locale = get_user_locale()
173 );
174
175 switch_to_locale($original);
176 }
177
178 /**
179 * Get specific weekday.
180 *
181 * @param int $day 0-6
182 * @return string
183 */
184 public function getWeekday($day)
185 {
186 if (is_string($day)) {
187 $day = Str::title($day);
188 if (strlen($day) === 3) {
189 $day = Arr::startsLike(static::$weekdayNames, $day);
190 $day = key($day);
191 } else {
192 $day = array_search($day, static::$weekdayNames);
193 }
194 }
195
196 // Validate numeric day is within valid range 0-6
197 if (!is_int($day) || $day < 0 || $day > 6) {
198 return '';
199 }
200
201 return $this->localeInfo->weekday[$day] ?? '';
202 }
203 /**
204 * Get specific weekday.
205 *
206 * @param int $day 0-6
207 * @return string
208 */
209 public function getWeekdayName($day)
210 {
211 return $this->getWeekday($day);
212 }
213
214 /**
215 * Get weekday initials (Short form).
216 *
217 * @param string $day
218 * @return string
219 */
220 public function getWeekdayInitial($day)
221 {
222 $day = $this->resolveWeekdayKey($day);
223
224 return $this->localeInfo->weekday_initial[$day] ?? '';
225 }
226
227 /**
228 * Get weekday abbr (Short form).
229 *
230 * @param string $day
231 * @return string
232 */
233 public function getWeekdayAbbrev($day)
234 {
235 $day = $this->resolveWeekdayKey($day);
236 return $this->localeInfo->weekday_abbrev[$day] ?? '';
237 }
238
239 /**
240 * Get specific month.
241 *
242 * @param int $month 0-11
243 * @return string
244 */
245 public function getMonth($month)
246 {
247 $month = $this->resolveMonthNumber($month);
248
249 if (!$month) {
250 return '';
251 }
252
253 $key = $month < 10 ? '0' . $month : (string) $month;
254
255 return $this->localeInfo->month[$key] ?? '';
256 }
257
258 /**
259 * Get specific month.
260 *
261 * @param int $month 0-11
262 * @return string
263 */
264 public function getMonthName($month)
265 {
266 return $this->getMonth($month);
267 }
268
269 /**
270 * Get specific month.
271 *
272 * @param int $month 01-12
273 * @return string
274 */
275 public function getShortMonthName($month)
276 {
277 return $this->getMonthAbbrev($month);
278 }
279
280 /**
281 * Get specific genitive month.
282 *
283 * @param int $month 01-12
284 * @return string
285 */
286 public function getMonthGenitive($month)
287 {
288 $month = $this->resolveMonthNumber($month);
289 return $this->localeInfo->month_genitive[$month] ?? '';
290 }
291
292 /**
293 * Get month abbr (Short form).
294 *
295 * @param string $month
296 * @return string
297 */
298 public function getMonthAbbrev($month)
299 {
300 $month = $this->resolveMonthName($month);
301 return $this->localeInfo->month_abbrev[$month] ?? '';
302 }
303
304 /**
305 * Get meridiem.
306 *
307 * @param string $period
308 * @return string
309 */
310 public function getMeridiem($period)
311 {
312 return $this->localeInfo->meridiem[$period] ?? '';
313 }
314
315 /**
316 * Get tetx direction.
317 *
318 * @return string
319 */
320 public function getTextDirection()
321 {
322 return $this->localeInfo->text_direction ?? 'ltr';
323 }
324
325 /**
326 * Get number format.
327 *
328 * @return string
329 */
330 public function getNumberFormat()
331 {
332 return $this->localeInfo->number_format ?? '';
333 }
334
335 /**
336 * Get list item separator.
337 *
338 * @return string
339 */
340 public function getListItemSeparator()
341 {
342 return $this->localeInfo->list_item_separator ?? '';
343 }
344
345 /**
346 * Get word count type.
347 *
348 * @return string
349 */
350 public function getWordCountType()
351 {
352 return $this->localeInfo->word_count_type ?? '';
353 }
354
355 /**
356 * Get start day number/name of the week
357 *
358 * @return int
359 */
360 public function getStartDayOfWeek($name = false)
361 {
362 $num = $this->localeInfo->startDayOfWeek ?? 1;
363
364 if ($name) {
365 return static::$weekdayNames[$num] ?? '';
366 }
367
368 return $num;
369 }
370
371 /**
372 * Get start day name of the week
373 *
374 * @return int
375 */
376 public function getStartDayNameOfWeek()
377 {
378 return $this->getStartDayOfWeek(true);
379 }
380
381 /**
382 * Checks if current locale is RTL.
383 *
384 * @since 3.0.0
385 * @return bool Whether locale is RTL.
386 */
387 public function isRtl()
388 {
389 return 'rtl' === $this->localeInfo->text_direction;
390 }
391
392 /**
393 * Resolve the month number.
394 *
395 * @param int|string $month
396 * @return int
397 */
398 public function resolveMonthNumber($month)
399 {
400 if (is_numeric($month)) {
401 $month = (int) $month;
402 $month = $this->localeInfo->mappings['month_index_to_num'][$month] ?? null;
403 } elseif (is_string($month)) {
404 if (strlen($month) === 3) {
405 $found = Arr::startsLike(
406 $this->localeInfo->mappings['month'], Str::title($month)
407 );
408 $month = $found ? key($found) + 1 : null;
409 } else {
410 $pos = array_search(Str::title($month), static::$monthNames);
411 $month = ($pos !== false) ? $pos + 1 : null;
412 }
413 }
414
415 if (!is_int($month) || $month < 1 || $month > 12) {
416 return false;
417 }
418
419 return $month;
420 }
421
422 /**
423 * Resolve the month name.
424 *
425 * @param int|string $month
426 * @return string
427 */
428 public function resolveMonthName($month)
429 {
430 if (is_numeric($month)) {
431 $month = $this->resolveMonthNumber($month);
432 $name = $this->localeInfo->mappings['month'][$month - 1];
433 } elseif (is_string($month)) {
434 $name = Str::title($month);
435 if (strlen($month) === 3) {
436 $month = Arr::startsLike(
437 $this->localeInfo->mappings['month'], Str::title($month)
438 );
439 $name = reset($month);
440 }
441 }
442
443 return $name;
444 }
445
446 /**
447 * Resolve the weekday key.
448 *
449 * @param int|string $day
450 * @return int
451 */
452 public function resolveWeekdayKey($day)
453 {
454 if (is_numeric($day)) {
455 $day = $this->localeInfo->mappings['weekday'][$day];
456 } elseif (is_string($day)) {
457 $day = Str::title($day);
458 if (strlen($day) === 3) {
459 $day = Arr::startsLike(
460 $this->localeInfo->mappings['weekday'], Str::title($day)
461 );
462 $day = reset($day);
463 }
464 }
465
466 return $day;
467 }
468
469 /**
470 * Dynamic property accessor.
471 *
472 * @param string $key
473 * @return mixed
474 */
475 public function __get($key)
476 {
477 return $this->localeInfo->{$key};
478 }
479
480 /**
481 * Get the info as array.
482 *
483 * @return array
484 */
485 public function toArray()
486 {
487 return (array) $this->localeInfo;
488 }
489
490 /**
491 * Get the locale identifier.
492 *
493 * @return string
494 */
495 public function toString()
496 {
497 return $this->locale;
498 }
499
500 /**
501 * Get the locale identifier.
502 *
503 * @return string
504 */
505 public function __toString()
506 {
507 return $this->toString();
508 }
509
510 /**
511 * Alternative constructor.
512 *
513 * @param int|null $userIdOrLocale
514 * @return $this
515 */
516 public static function init($userIdOrLocale = null)
517 {
518 return new static($userIdOrLocale);
519 }
520
521 /**
522 * Load the locale info of a user.
523 *
524 * @param int|null $userIdOrLocale
525 * @return \stdClass
526 */
527 public static function getInfo($userIdOrLocale = null)
528 {
529 return new Static($userIdOrLocale);
530 }
531 }
532