2017-05-30 20:20:16 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* LocaleManager class.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Alltube;
|
|
|
|
|
2018-02-05 15:48:58 +00:00
|
|
|
use Aura\Session\Segment;
|
2017-12-23 13:17:29 +00:00
|
|
|
use Symfony\Component\Process\Process;
|
2017-06-01 21:40:36 +00:00
|
|
|
|
2017-05-30 20:20:16 +00:00
|
|
|
/**
|
|
|
|
* Class used to manage locales.
|
|
|
|
*/
|
|
|
|
class LocaleManager
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Supported locales.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2018-12-07 17:19:00 +00:00
|
|
|
private $supportedLocales = ['en_US', 'fr_FR', 'zh_CN', 'es_ES', 'pt_BR', 'de_DE', 'ar_001'];
|
2017-05-30 20:20:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Current locale.
|
|
|
|
*
|
2019-03-30 17:21:45 +00:00
|
|
|
* @var Locale|null
|
2017-05-30 20:20:16 +00:00
|
|
|
*/
|
|
|
|
private $curLocale;
|
|
|
|
|
2017-05-30 21:49:38 +00:00
|
|
|
/**
|
|
|
|
* Session segment used to store session variables.
|
|
|
|
*
|
2018-02-05 15:48:58 +00:00
|
|
|
* @var Segment
|
2017-05-30 21:49:38 +00:00
|
|
|
*/
|
|
|
|
private $sessionSegment;
|
|
|
|
|
2017-05-30 20:20:16 +00:00
|
|
|
/**
|
|
|
|
* LocaleManager constructor.
|
|
|
|
*/
|
2019-04-22 15:00:51 +00:00
|
|
|
public function __construct()
|
2017-05-30 20:20:16 +00:00
|
|
|
{
|
2019-04-22 15:00:51 +00:00
|
|
|
$session = SessionManager::getSession();
|
2018-02-05 15:48:58 +00:00
|
|
|
$this->sessionSegment = $session->getSegment(self::class);
|
2017-05-30 21:30:21 +00:00
|
|
|
$cookieLocale = $this->sessionSegment->get('locale');
|
|
|
|
if (isset($cookieLocale)) {
|
2017-05-30 22:51:11 +00:00
|
|
|
$this->setLocale(new Locale($cookieLocale));
|
2017-05-30 21:30:21 +00:00
|
|
|
}
|
2018-01-26 10:37:43 +00:00
|
|
|
bindtextdomain('Alltube', __DIR__.'/../i18n/');
|
|
|
|
textdomain('Alltube');
|
2017-05-30 20:20:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of supported locales.
|
|
|
|
*
|
2017-05-30 21:30:21 +00:00
|
|
|
* @return Locale[]
|
2017-05-30 20:20:16 +00:00
|
|
|
*/
|
|
|
|
public function getSupportedLocales()
|
|
|
|
{
|
|
|
|
$return = [];
|
2017-12-23 13:17:29 +00:00
|
|
|
$process = new Process(['locale', '-a']);
|
2017-06-01 21:40:36 +00:00
|
|
|
$process->run();
|
|
|
|
$installedLocales = explode(PHP_EOL, trim($process->getOutput()));
|
2017-05-30 20:20:16 +00:00
|
|
|
foreach ($this->supportedLocales as $supportedLocale) {
|
2017-10-12 21:28:54 +00:00
|
|
|
if (in_array($supportedLocale, $installedLocales)
|
|
|
|
|| in_array($supportedLocale.'.utf8', $installedLocales)
|
|
|
|
) {
|
2017-06-01 21:40:36 +00:00
|
|
|
$return[] = new Locale($supportedLocale);
|
|
|
|
}
|
2017-05-30 20:20:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current locale.
|
|
|
|
*
|
2019-03-30 17:21:45 +00:00
|
|
|
* @return Locale|null
|
2017-05-30 20:20:16 +00:00
|
|
|
*/
|
|
|
|
public function getLocale()
|
|
|
|
{
|
|
|
|
return $this->curLocale;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the current locale.
|
|
|
|
*
|
2017-05-30 21:30:21 +00:00
|
|
|
* @param Locale $locale Locale
|
2017-05-30 20:20:16 +00:00
|
|
|
*/
|
2017-05-30 21:30:21 +00:00
|
|
|
public function setLocale(Locale $locale)
|
2017-05-30 20:20:16 +00:00
|
|
|
{
|
|
|
|
putenv('LANG='.$locale);
|
2018-05-28 10:00:27 +00:00
|
|
|
setlocale(LC_ALL, [$locale.'.utf8', $locale]);
|
2017-05-30 20:20:16 +00:00
|
|
|
$this->curLocale = $locale;
|
|
|
|
$this->sessionSegment->set('locale', $locale);
|
|
|
|
}
|
2017-11-10 11:18:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Unset the current locale.
|
|
|
|
*/
|
|
|
|
public function unsetLocale()
|
|
|
|
{
|
|
|
|
$this->curLocale = null;
|
2017-11-12 15:34:14 +00:00
|
|
|
$this->sessionSegment->clear();
|
2017-11-10 11:18:20 +00:00
|
|
|
}
|
2017-05-30 20:20:16 +00:00
|
|
|
}
|