2017-05-30 21:30:21 +00:00
|
|
|
<?php
|
2019-10-03 19:24:12 +00:00
|
|
|
|
2017-05-30 21:30:21 +00:00
|
|
|
/**
|
|
|
|
* Locale class.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Alltube;
|
|
|
|
|
2018-02-05 15:48:58 +00:00
|
|
|
use Locale as PHPLocale;
|
|
|
|
use Rinvex\Country\Country;
|
2017-05-30 21:30:21 +00:00
|
|
|
use Teto\HTTP\AcceptLanguage;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class used to represent locales.
|
|
|
|
*/
|
|
|
|
class Locale
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Locale language.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $language;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Locale region.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $region;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Locale constructor.
|
|
|
|
*
|
|
|
|
* @param string $locale ISO 15897 code
|
|
|
|
*/
|
2020-09-27 13:53:53 +00:00
|
|
|
public function __construct(string $locale)
|
2017-05-30 21:30:21 +00:00
|
|
|
{
|
|
|
|
$parse = AcceptLanguage::parse($locale);
|
|
|
|
$this->language = $parse[1]['language'];
|
2019-11-27 22:15:49 +00:00
|
|
|
if (!empty($parse[1]['region'])) {
|
|
|
|
$this->region = $parse[1]['region'];
|
|
|
|
}
|
2017-05-30 21:30:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the locale to a string.
|
|
|
|
*
|
|
|
|
* @return string ISO 15897 code
|
|
|
|
*/
|
2020-12-17 21:43:05 +00:00
|
|
|
public function __toString(): string
|
2017-05-30 21:30:21 +00:00
|
|
|
{
|
|
|
|
return $this->getIso15897();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the full name of the locale.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2020-12-17 21:43:05 +00:00
|
|
|
public function getFullName(): string
|
2017-05-30 21:30:21 +00:00
|
|
|
{
|
2023-03-21 19:25:47 +00:00
|
|
|
return mb_convert_case(PHPLocale::getDisplayName($this->getIso15897(), $this->getIso15897()), MB_CASE_TITLE);
|
2017-05-30 21:30:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the ISO 15897 code.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2020-12-17 21:43:05 +00:00
|
|
|
public function getIso15897(): string
|
2017-05-30 21:30:21 +00:00
|
|
|
{
|
2019-11-27 22:15:49 +00:00
|
|
|
if (isset($this->region)) {
|
|
|
|
return $this->language . '_' . $this->region;
|
|
|
|
} else {
|
|
|
|
return $this->language;
|
|
|
|
}
|
2017-05-30 21:30:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the BCP 47 code.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2020-12-17 21:43:05 +00:00
|
|
|
public function getBcp47(): string
|
2017-05-30 21:30:21 +00:00
|
|
|
{
|
2019-11-27 22:15:49 +00:00
|
|
|
if (isset($this->region)) {
|
|
|
|
return $this->language . '-' . $this->region;
|
|
|
|
} else {
|
|
|
|
return $this->language;
|
|
|
|
}
|
2017-05-30 21:30:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the ISO 3166 code.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2020-12-17 21:43:05 +00:00
|
|
|
public function getIso3166(): string
|
2017-05-30 21:30:21 +00:00
|
|
|
{
|
|
|
|
return strtolower($this->region);
|
|
|
|
}
|
2017-05-31 14:26:00 +00:00
|
|
|
|
2017-10-02 18:29:09 +00:00
|
|
|
/**
|
|
|
|
* Get country information from locale.
|
|
|
|
*
|
2020-05-13 20:28:05 +00:00
|
|
|
* @return Country|Country[]|null
|
2017-10-02 18:29:09 +00:00
|
|
|
*/
|
2017-05-31 14:26:00 +00:00
|
|
|
public function getCountry()
|
|
|
|
{
|
2019-11-27 22:15:49 +00:00
|
|
|
if (isset($this->region)) {
|
|
|
|
return country($this->getIso3166());
|
|
|
|
}
|
2020-05-13 19:18:32 +00:00
|
|
|
|
|
|
|
return null;
|
2017-05-31 14:26:00 +00:00
|
|
|
}
|
2017-05-30 21:30:21 +00:00
|
|
|
}
|