alltube/classes/LocaleManager.php

168 lines
3.7 KiB
PHP
Raw Permalink Normal View History

2017-05-30 22:20:16 +02:00
<?php
2017-05-30 22:20:16 +02:00
/**
* LocaleManager class.
*/
namespace Alltube;
use Aura\Session\Segment;
use Aura\Session\Session;
2020-05-28 00:22:30 +02:00
use Symfony\Component\Finder\Finder;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\Loader\PoFileLoader;
2017-05-30 22:20:16 +02:00
/**
* Class used to manage locales.
*/
class LocaleManager
{
2020-05-28 00:33:07 +02:00
/**
* Path to locales.
*/
2020-05-28 00:22:30 +02:00
private const PATH = __DIR__ . '/../i18n/';
2017-05-30 22:20:16 +02:00
/**
* Current locale.
*
* @var Locale|null
2017-05-30 22:20:16 +02:00
*/
private ?Locale $curLocale = null;
2017-05-30 22:20:16 +02:00
2017-05-30 23:49:38 +02:00
/**
* Session segment used to store session variables.
*
* @var Segment
2017-05-30 23:49:38 +02:00
*/
private Segment $sessionSegment;
2017-05-30 23:49:38 +02:00
/**
* Default locale.
*
* @var string
*/
private const DEFAULT_LOCALE = 'en';
/**
* Symfony Translator instance.
*
* @var Translator
*/
private Translator $translator;
2017-05-30 22:20:16 +02:00
/**
* LocaleManager constructor.
* @param Session $session
2017-05-30 22:20:16 +02:00
*/
public function __construct(Session $session)
2017-05-30 22:20:16 +02:00
{
$this->sessionSegment = $session->getSegment(self::class);
2017-05-30 23:30:21 +02:00
$cookieLocale = $this->sessionSegment->get('locale');
$this->translator = new Translator(self::DEFAULT_LOCALE);
2017-05-30 23:30:21 +02:00
if (isset($cookieLocale)) {
$this->setLocale(new Locale($cookieLocale));
2017-05-30 23:30:21 +02:00
}
$this->translator->addLoader('gettext', new PoFileLoader());
foreach ($this->getSupportedLocales() as $locale) {
$this->translator->addResource(
'gettext',
2020-05-28 00:22:30 +02:00
self::PATH . $locale->getIso15897() . '/LC_MESSAGES/Alltube.po',
$locale->getIso15897()
);
}
2017-05-30 22:20:16 +02:00
}
/**
* Get a list of supported locales.
*
2017-05-30 23:30:21 +02:00
* @return Locale[]
2017-05-30 22:20:16 +02:00
*/
2020-12-17 22:43:05 +01:00
public function getSupportedLocales(): array
2017-05-30 22:20:16 +02:00
{
2020-05-28 00:33:07 +02:00
$return = [
new Locale('en_US')
];
2020-05-28 00:22:30 +02:00
$finder = new Finder();
$finder->depth(0)
->directories()
->in(self::PATH);
foreach ($finder as $file) {
$return[] = new Locale($file->getFilename());
2017-05-30 22:20:16 +02:00
}
return $return;
}
/**
* Get the current locale.
*
* @return Locale|null
2017-05-30 22:20:16 +02:00
*/
2020-12-17 22:43:05 +01:00
public function getLocale(): ?Locale
2017-05-30 22:20:16 +02:00
{
return $this->curLocale;
}
/**
* Set the current locale.
*
2017-05-30 23:30:21 +02:00
* @param Locale $locale Locale
2020-05-13 22:28:05 +02:00
* @return void
2017-05-30 22:20:16 +02:00
*/
2022-05-28 23:43:07 +02:00
public function setLocale(Locale $locale): void
2017-05-30 22:20:16 +02:00
{
$this->translator->setLocale($locale->getIso15897());
2017-05-30 22:20:16 +02:00
$this->curLocale = $locale;
$this->sessionSegment->set('locale', $locale);
}
/**
* Unset the current locale.
2020-05-13 22:28:05 +02:00
* @return void
*/
2022-05-28 23:43:07 +02:00
public function unsetLocale(): void
{
$this->translator->setLocale(self::DEFAULT_LOCALE);
$this->curLocale = null;
2017-11-12 16:34:14 +01:00
$this->sessionSegment->clear();
}
/**
* Smarty "t" block.
*
2022-02-03 20:21:25 +01:00
* @param string[]|string[][] $params Block parameters
2020-09-27 15:53:53 +02:00
* @param string|null $text Block content
*
* @return string Translated string
*/
2020-12-17 22:43:05 +01:00
public function smartyTranslate(array $params, string $text = null): string
{
2022-02-03 20:21:25 +01:00
if (isset($params['params']) && is_array($params['params'])) {
return $this->t($text, $params['params']);
} else {
return $this->t($text);
}
}
/**
* Translate a string.
*
2020-09-27 15:53:53 +02:00
* @param string|null $string $string String to translate
*
2022-02-03 20:21:25 +01:00
* @param string[] $params
* @return string Translated string
*/
2020-12-17 22:43:05 +01:00
public function t(string $string = null, array $params = []): string
{
2020-09-27 15:53:53 +02:00
if (isset($string)) {
return $this->translator->trans($string, $params);
}
return '';
}
2017-05-30 22:20:16 +02:00
}