alltube/classes/LocaleManager.php

186 lines
3.9 KiB
PHP
Raw Normal View History

2017-05-30 20:20:16 +00:00
<?php
2017-05-30 20:20:16 +00:00
/**
* LocaleManager class.
*/
namespace Alltube;
use Aura\Session\Segment;
use Symfony\Component\Process\Process;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\Loader\PoFileLoader;
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', 'es_ES', 'pt_BR', 'de_DE', 'ar', 'pl_PL', 'tr_TR'];
2017-05-30 20:20:16 +00:00
/**
* Current locale.
*
* @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.
*
* @var Segment
2017-05-30 21:49:38 +00:00
*/
private $sessionSegment;
/**
* Default locale.
*
* @var string
*/
private const DEFAULT_LOCALE = 'en';
/**
* Symfony Translator instance.
*
* @var Translator
*/
private $translator;
/**
* Singleton instance.
*
* @var LocaleManager|null
*/
private static $instance;
2017-05-30 20:20:16 +00:00
/**
* LocaleManager constructor.
*/
private function __construct()
2017-05-30 20:20:16 +00:00
{
$session = SessionManager::getSession();
$this->sessionSegment = $session->getSegment(self::class);
2017-05-30 21:30:21 +00:00
$cookieLocale = $this->sessionSegment->get('locale');
$this->translator = new Translator(self::DEFAULT_LOCALE);
2017-05-30 21:30:21 +00:00
if (isset($cookieLocale)) {
$this->setLocale(new Locale($cookieLocale));
2017-05-30 21:30:21 +00:00
}
$this->translator->addLoader('gettext', new PoFileLoader());
foreach ($this->getSupportedLocales() as $locale) {
$this->translator->addResource(
'gettext',
__DIR__ . '/../i18n/' . $locale->getIso15897() . '/LC_MESSAGES/Alltube.po',
$locale->getIso15897()
);
}
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-05-30 20:20:16 +00:00
foreach ($this->supportedLocales as $supportedLocale) {
$return[] = new Locale($supportedLocale);
2017-05-30 20:20:16 +00:00
}
return $return;
}
/**
* Get the current locale.
*
* @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
{
$this->translator->setLocale($locale->getIso15897());
2017-05-30 20:20:16 +00:00
$this->curLocale = $locale;
$this->sessionSegment->set('locale', $locale);
}
/**
* Unset the current locale.
*/
public function unsetLocale()
{
$this->translator->setLocale(self::DEFAULT_LOCALE);
$this->curLocale = null;
2017-11-12 15:34:14 +00:00
$this->sessionSegment->clear();
}
/**
* Smarty "t" block.
*
* @param array $params Block parameters
* @param string $text Block content
*
* @return string Translated string
*/
public function smartyTranslate(array $params, $text)
{
if (isset($params['params'])) {
return $this->t($text, $params['params']);
} else {
return $this->t($text);
}
}
/**
* Translate a string.
*
* @param string $string String to translate
*
* @return string Translated string
*/
public function t($string, array $params = [])
{
return $this->translator->trans($string, $params);
}
/**
* Get LocaleManager singleton instance.
*
* @return LocaleManager
*/
public static function getInstance()
{
if (!isset(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
2019-11-27 23:04:05 +00:00
/**
* Destroy singleton instance.
*
* @return void
*/
public static function destroyInstance()
{
self::$instance = null;
}
2017-05-30 20:20:16 +00:00
}