2017-05-30 20:20:16 +00:00
|
|
|
<?php
|
2019-10-03 19:24:12 +00:00
|
|
|
|
2017-05-30 20:20:16 +00:00
|
|
|
/**
|
|
|
|
* LocaleManager class.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Alltube;
|
|
|
|
|
2018-02-05 15:48:58 +00:00
|
|
|
use Aura\Session\Segment;
|
2020-05-27 22:22:30 +00:00
|
|
|
use Symfony\Component\Finder\Finder;
|
2019-11-27 22:15:49 +00:00
|
|
|
use Symfony\Component\Translation\Translator;
|
2019-11-30 15:33:39 +00:00
|
|
|
use Symfony\Component\Translation\Loader\PoFileLoader;
|
2017-06-01 21:40:36 +00:00
|
|
|
|
2017-05-30 20:20:16 +00:00
|
|
|
/**
|
|
|
|
* Class used to manage locales.
|
|
|
|
*/
|
|
|
|
class LocaleManager
|
|
|
|
{
|
2020-05-27 22:33:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Path to locales.
|
|
|
|
*/
|
2020-05-27 22:22:30 +00:00
|
|
|
private const PATH = __DIR__ . '/../i18n/';
|
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;
|
|
|
|
|
2019-11-27 22:15:49 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2019-11-27 22:15:49 +00:00
|
|
|
private 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');
|
2019-11-27 22:15:49 +00:00
|
|
|
|
|
|
|
$this->translator = new Translator(self::DEFAULT_LOCALE);
|
2017-05-30 21:30:21 +00:00
|
|
|
if (isset($cookieLocale)) {
|
2017-05-30 22:51:11 +00:00
|
|
|
$this->setLocale(new Locale($cookieLocale));
|
2017-05-30 21:30:21 +00:00
|
|
|
}
|
2019-11-27 22:15:49 +00:00
|
|
|
|
2019-11-30 15:33:39 +00:00
|
|
|
$this->translator->addLoader('gettext', new PoFileLoader());
|
2019-11-27 22:15:49 +00:00
|
|
|
foreach ($this->getSupportedLocales() as $locale) {
|
|
|
|
$this->translator->addResource(
|
|
|
|
'gettext',
|
2020-05-27 22:22:30 +00:00
|
|
|
self::PATH . $locale->getIso15897() . '/LC_MESSAGES/Alltube.po',
|
2019-11-27 22:15:49 +00:00
|
|
|
$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()
|
|
|
|
{
|
2020-05-27 22:33:07 +00:00
|
|
|
$return = [
|
|
|
|
new Locale('en_US')
|
|
|
|
];
|
2019-11-27 22:15:49 +00:00
|
|
|
|
2020-05-27 22:22:30 +00:00
|
|
|
$finder = new Finder();
|
|
|
|
$finder->depth(0)
|
|
|
|
->directories()
|
|
|
|
->in(self::PATH);
|
|
|
|
|
|
|
|
foreach ($finder as $file) {
|
|
|
|
$return[] = new Locale($file->getFilename());
|
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
|
2020-05-13 20:28:05 +00:00
|
|
|
* @return void
|
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
|
|
|
{
|
2019-11-27 22:15:49 +00:00
|
|
|
$this->translator->setLocale($locale->getIso15897());
|
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.
|
2020-05-13 20:28:05 +00:00
|
|
|
* @return void
|
2017-11-10 11:18:20 +00:00
|
|
|
*/
|
|
|
|
public function unsetLocale()
|
|
|
|
{
|
2019-11-27 22:15:49 +00:00
|
|
|
$this->translator->setLocale(self::DEFAULT_LOCALE);
|
2017-11-10 11:18:20 +00:00
|
|
|
$this->curLocale = null;
|
2017-11-12 15:34:14 +00:00
|
|
|
$this->sessionSegment->clear();
|
2017-11-10 11:18:20 +00:00
|
|
|
}
|
2019-11-27 22:15:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Smarty "t" block.
|
|
|
|
*
|
2020-05-13 20:28:05 +00:00
|
|
|
* @param mixed[] $params Block parameters
|
2020-09-27 13:53:53 +00:00
|
|
|
* @param string|null $text Block content
|
2019-11-27 22:15:49 +00:00
|
|
|
*
|
|
|
|
* @return string Translated string
|
|
|
|
*/
|
2020-09-27 13:53:53 +00:00
|
|
|
public function smartyTranslate(array $params, string $text = null)
|
2019-11-27 22:15:49 +00:00
|
|
|
{
|
2019-11-29 20:33:49 +00:00
|
|
|
if (isset($params['params'])) {
|
|
|
|
return $this->t($text, $params['params']);
|
|
|
|
} else {
|
|
|
|
return $this->t($text);
|
|
|
|
}
|
2019-11-27 22:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Translate a string.
|
|
|
|
*
|
2020-09-27 13:53:53 +00:00
|
|
|
* @param string|null $string $string String to translate
|
2019-11-27 22:15:49 +00:00
|
|
|
*
|
2020-05-13 20:28:05 +00:00
|
|
|
* @param mixed[] $params
|
2019-11-27 22:15:49 +00:00
|
|
|
* @return string Translated string
|
|
|
|
*/
|
2020-09-27 13:53:53 +00:00
|
|
|
public function t(string $string = null, array $params = [])
|
2019-11-27 22:15:49 +00:00
|
|
|
{
|
2020-09-27 13:53:53 +00:00
|
|
|
if (isset($string)) {
|
|
|
|
return $this->translator->trans($string, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
2019-11-27 22:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get LocaleManager singleton instance.
|
|
|
|
*
|
|
|
|
* @return LocaleManager
|
2020-07-15 21:05:41 +00:00
|
|
|
* @todo Stop using a singleton.
|
2019-11-27 22:15:49 +00:00
|
|
|
*/
|
|
|
|
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
|
|
|
}
|