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