Improve locale handling
This commit is contained in:
parent
b4dd0aeb29
commit
0f80cbd333
7 changed files with 118 additions and 16 deletions
92
classes/Locale.php
Normal file
92
classes/Locale.php
Normal file
|
@ -0,0 +1,92 @@
|
|||
<?php
|
||||
/**
|
||||
* Locale class.
|
||||
*/
|
||||
|
||||
namespace Alltube;
|
||||
|
||||
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
|
||||
*/
|
||||
public function __construct($locale)
|
||||
{
|
||||
$parse = AcceptLanguage::parse($locale);
|
||||
$this->language = $parse[1]['language'];
|
||||
$this->region = $parse[1]['region'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the locale to a string.
|
||||
*
|
||||
* @return string ISO 15897 code
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return $this->getIso15897();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the full name of the locale.
|
||||
*
|
||||
* @param Locale $displayLocale Locale to get the name in
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFullName(Locale $displayLocale)
|
||||
{
|
||||
return \Locale::getDisplayName($this->getIso15897(), $displayLocale->getIso15897());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the ISO 15897 code.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getIso15897()
|
||||
{
|
||||
return $this->language.'_'.$this->region;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the BCP 47 code.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getBcp47()
|
||||
{
|
||||
return $this->language.'-'.$this->region;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the ISO 3166 code.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getIso3166()
|
||||
{
|
||||
return strtolower($this->region);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue