2017-05-30 20:20:16 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* LocaleManager class.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Alltube;
|
|
|
|
|
2017-06-01 21:40:36 +00:00
|
|
|
use Symfony\Component\Process\ProcessBuilder;
|
|
|
|
|
2017-05-30 20:20:16 +00:00
|
|
|
/**
|
|
|
|
* Class used to manage locales.
|
|
|
|
*/
|
|
|
|
class LocaleManager
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Supported locales.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $supportedLocales = ['en_US', 'fr_FR', 'zh_CN'];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Current locale.
|
|
|
|
*
|
2017-05-30 21:30:21 +00:00
|
|
|
* @var Locale
|
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.
|
|
|
|
*
|
|
|
|
* @var \Aura\Session\Segment
|
|
|
|
*/
|
|
|
|
private $sessionSegment;
|
|
|
|
|
2017-05-30 20:20:16 +00:00
|
|
|
/**
|
|
|
|
* LocaleManager constructor.
|
|
|
|
*
|
|
|
|
* @param array $cookies Cookie array
|
|
|
|
*/
|
|
|
|
public function __construct(array $cookies = [])
|
|
|
|
{
|
|
|
|
$session_factory = new \Aura\Session\SessionFactory();
|
|
|
|
$session = $session_factory->newInstance($cookies);
|
|
|
|
$this->sessionSegment = $session->getSegment('Alltube\LocaleManager');
|
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
|
|
|
}
|
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-06-01 21:40:36 +00:00
|
|
|
$builder = new ProcessBuilder(['locale', '-a']);
|
|
|
|
$process = $builder->getProcess();
|
|
|
|
$process->run();
|
|
|
|
$installedLocales = explode(PHP_EOL, trim($process->getOutput()));
|
2017-05-30 20:20:16 +00:00
|
|
|
foreach ($this->supportedLocales as $supportedLocale) {
|
2017-06-01 21:40:36 +00:00
|
|
|
if (in_array($supportedLocale, $installedLocales)) {
|
|
|
|
$return[] = new Locale($supportedLocale);
|
|
|
|
}
|
2017-05-30 20:20:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current locale.
|
|
|
|
*
|
2017-05-30 21:30:21 +00:00
|
|
|
* @return Locale
|
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);
|
|
|
|
setlocale(LC_ALL, [$locale, $locale.'.utf8']);
|
|
|
|
$this->curLocale = $locale;
|
|
|
|
$this->sessionSegment->set('locale', $locale);
|
|
|
|
}
|
|
|
|
}
|