Switch to symfony/translation for translations (#250)

This commit is contained in:
Pierre Rudloff 2019-11-27 23:15:49 +01:00
parent 0b1ce90f47
commit a5bd827d21
16 changed files with 399 additions and 212 deletions

View file

@ -135,6 +135,11 @@ class Config
*/
public $genericFormats = [];
/**
* Enable debug mode.
*
* @var bool
*/
public $debug = false;
/**

View file

@ -38,7 +38,9 @@ class Locale
{
$parse = AcceptLanguage::parse($locale);
$this->language = $parse[1]['language'];
$this->region = $parse[1]['region'];
if (!empty($parse[1]['region'])) {
$this->region = $parse[1]['region'];
}
}
/**
@ -68,7 +70,11 @@ class Locale
*/
public function getIso15897()
{
return $this->language . '_' . $this->region;
if (isset($this->region)) {
return $this->language . '_' . $this->region;
} else {
return $this->language;
}
}
/**
@ -78,7 +84,11 @@ class Locale
*/
public function getBcp47()
{
return $this->language . '-' . $this->region;
if (isset($this->region)) {
return $this->language . '-' . $this->region;
} else {
return $this->language;
}
}
/**
@ -98,6 +108,8 @@ class Locale
*/
public function getCountry()
{
return country($this->getIso3166());
if (isset($this->region)) {
return country($this->getIso3166());
}
}
}

View file

@ -8,6 +8,8 @@ namespace Alltube;
use Aura\Session\Segment;
use Symfony\Component\Process\Process;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\Loader\MoFileLoader;
/**
* Class used to manage locales.
@ -19,7 +21,7 @@ class LocaleManager
*
* @var array
*/
private $supportedLocales = ['en_US', 'fr_FR', 'zh_CN', 'es_ES', 'pt_BR', 'de_DE', 'ar_001'];
private $supportedLocales = ['en_US', 'fr_FR', 'zh_CN', 'es_ES', 'pt_BR', 'de_DE', 'ar'];
/**
* Current locale.
@ -35,19 +37,49 @@ class LocaleManager
*/
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;
/**
* LocaleManager constructor.
*/
public function __construct()
private function __construct()
{
$session = SessionManager::getSession();
$this->sessionSegment = $session->getSegment(self::class);
$cookieLocale = $this->sessionSegment->get('locale');
$this->translator = new Translator(self::DEFAULT_LOCALE);
if (isset($cookieLocale)) {
$this->setLocale(new Locale($cookieLocale));
}
bindtextdomain('Alltube', __DIR__ . '/../i18n/');
textdomain('Alltube');
$this->translator->addLoader('gettext', new MoFileLoader());
foreach ($this->getSupportedLocales() as $locale) {
$this->translator->addResource(
'gettext',
__DIR__ . '/../i18n/' . $locale->getIso15897() . '/LC_MESSAGES/Alltube.mo',
$locale->getIso15897()
);
}
}
/**
@ -58,16 +90,9 @@ class LocaleManager
public function getSupportedLocales()
{
$return = [];
$process = new Process(['locale', '-a']);
$process->run();
$installedLocales = explode(PHP_EOL, trim($process->getOutput()));
foreach ($this->supportedLocales as $supportedLocale) {
if (
in_array($supportedLocale, $installedLocales)
|| in_array($supportedLocale . '.utf8', $installedLocales)
) {
$return[] = new Locale($supportedLocale);
}
$return[] = new Locale($supportedLocale);
}
return $return;
@ -90,8 +115,7 @@ class LocaleManager
*/
public function setLocale(Locale $locale)
{
putenv('LANG=' . $locale);
setlocale(LC_ALL, [$locale . '.utf8', $locale]);
$this->translator->setLocale($locale->getIso15897());
$this->curLocale = $locale;
$this->sessionSegment->set('locale', $locale);
}
@ -101,7 +125,47 @@ class LocaleManager
*/
public function unsetLocale()
{
$this->translator->setLocale(self::DEFAULT_LOCALE);
$this->curLocale = null;
$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)
{
return $this->t($text);
}
/**
* Translate a string.
*
* @param string $string String to translate
*
* @return string Translated string
*/
public function t($string)
{
return $this->translator->trans($string);
}
/**
* Get LocaleManager singleton instance.
*
* @return LocaleManager
*/
public static function getInstance()
{
if (!isset(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
}

View file

@ -74,6 +74,13 @@ class Video
*/
private $urls;
/**
* LocaleManager instance.
*
* @var LocaleManager
*/
protected $localeManager;
/**
* VideoDownload constructor.
*
@ -87,6 +94,8 @@ class Video
$this->requestedFormat = $requestedFormat;
$this->password = $password;
$this->config = Config::getInstance();
$this->localeManager = LocaleManager::getInstance();
}
/**
@ -116,7 +125,9 @@ class Video
* */
public static function getExtractors()
{
return explode("\n", trim(self::callYoutubedl(['--list-extractors'])));
$video = new self('');
return explode("\n", trim($video->callYoutubedl(['--list-extractors'])));
}
/**
@ -130,7 +141,7 @@ class Video
*
* @return string Result
*/
private static function callYoutubedl(array $arguments)
private function callYoutubedl(array $arguments)
{
$config = Config::getInstance();
@ -145,7 +156,7 @@ class Video
if ($errorOutput == 'ERROR: This video is protected by a password, use the --video-password option') {
throw new PasswordException($errorOutput, $exitCode);
} elseif (substr($errorOutput, 0, 21) == 'ERROR: Wrong password') {
throw new Exception(_('Wrong password'), $exitCode);
throw new Exception($this->localeManager->t('Wrong password'), $exitCode);
} else {
throw new Exception($errorOutput, $exitCode);
}
@ -177,7 +188,7 @@ class Video
$arguments[] = $this->password;
}
return $this::callYoutubedl($arguments);
return $this->callYoutubedl($arguments);
}
/**
@ -236,7 +247,7 @@ class Video
$this->urls = explode("\n", $this->getProp('get-url'));
if (empty($this->urls[0])) {
throw new EmptyUrlException(_('youtube-dl returned an empty URL.'));
throw new EmptyUrlException($this->localeManager->t('youtube-dl returned an empty URL.'));
}
}
@ -345,7 +356,11 @@ class Video
$to = null
) {
if (!$this->checkCommand([$this->config->avconv, '-version'])) {
throw new Exception(_('Can\'t find avconv or ffmpeg at ') . $this->config->avconv . '.');
throw new Exception(
$this->localeManager->t(
'Can\'t find avconv or ffmpeg at '
) . $this->config->avconv . '.'
);
}
$durationRegex = '/(\d+:)?(\d+:)?(\d+)/';
@ -358,14 +373,14 @@ class Video
if (!empty($from)) {
if (!preg_match($durationRegex, $from)) {
throw new Exception(_('Invalid start time: ') . $from . '.');
throw new Exception($this->localeManager->t('Invalid start time: ') . $from . '.');
}
$afterArguments[] = '-ss';
$afterArguments[] = $from;
}
if (!empty($to)) {
if (!preg_match($durationRegex, $to)) {
throw new Exception(_('Invalid end time: ') . $to . '.');
throw new Exception($this->localeManager->t('Invalid end time: ') . $to . '.');
}
$afterArguments[] = '-to';
$afterArguments[] = $to;
@ -411,14 +426,14 @@ class Video
public function getAudioStream($from = null, $to = null)
{
if (isset($this->_type) && $this->_type == 'playlist') {
throw new Exception(_('Conversion of playlists is not supported.'));
throw new Exception($this->localeManager->t('Conversion of playlists is not supported.'));
}
if (isset($this->protocol)) {
if (in_array($this->protocol, ['m3u8', 'm3u8_native'])) {
throw new Exception(_('Conversion of M3U8 files is not supported.'));
throw new Exception($this->localeManager->t('Conversion of M3U8 files is not supported.'));
} elseif ($this->protocol == 'http_dash_segments') {
throw new Exception(_('Conversion of DASH segments is not supported.'));
throw new Exception($this->localeManager->t('Conversion of DASH segments is not supported.'));
}
}
@ -427,7 +442,7 @@ class Video
$stream = popen($avconvProc->getCommandLine(), 'r');
if (!is_resource($stream)) {
throw new Exception(_('Could not open popen stream.'));
throw new Exception($this->localeManager->t('Could not open popen stream.'));
}
return $stream;
@ -444,7 +459,11 @@ class Video
public function getM3uStream()
{
if (!$this->checkCommand([$this->config->avconv, '-version'])) {
throw new Exception(_('Can\'t find avconv or ffmpeg at ') . $this->config->avconv . '.');
throw new Exception(
$this->localeManager->t(
'Can\'t find avconv or ffmpeg at '
) . $this->config->avconv . '.'
);
}
$urls = $this->getUrl();
@ -464,7 +483,7 @@ class Video
$stream = popen($process->getCommandLine(), 'r');
if (!is_resource($stream)) {
throw new Exception(_('Could not open popen stream.'));
throw new Exception($this->localeManager->t('Could not open popen stream.'));
}
return $stream;
@ -482,7 +501,7 @@ class Video
$urls = $this->getUrl();
if (!isset($urls[0]) || !isset($urls[1])) {
throw new Exception(_('This video does not have two URLs.'));
throw new Exception($this->localeManager->t('This video does not have two URLs.'));
}
$process = new Process(
@ -501,7 +520,7 @@ class Video
$stream = popen($process->getCommandLine(), 'r');
if (!is_resource($stream)) {
throw new Exception(_('Could not open popen stream.'));
throw new Exception($this->localeManager->t('Could not open popen stream.'));
}
return $stream;
@ -534,7 +553,7 @@ class Video
);
$stream = popen($process->getCommandLine(), 'r');
if (!is_resource($stream)) {
throw new Exception(_('Could not open popen stream.'));
throw new Exception($this->localeManager->t('Could not open popen stream.'));
}
return $stream;
@ -554,7 +573,7 @@ class Video
public function getConvertedStream($audioBitrate, $filetype)
{
if (in_array($this->protocol, ['m3u8', 'm3u8_native'])) {
throw new Exception(_('Conversion of M3U8 files is not supported.'));
throw new Exception($this->localeManager->t('Conversion of M3U8 files is not supported.'));
}
$avconvProc = $this->getAvconvProcess($audioBitrate, $filetype, false);
@ -562,7 +581,7 @@ class Video
$stream = popen($avconvProc->getCommandLine(), 'r');
if (!is_resource($stream)) {
throw new Exception(_('Could not open popen stream.'));
throw new Exception($this->localeManager->t('Could not open popen stream.'));
}
return $stream;

View file

@ -35,9 +35,12 @@ class ViewFactory
$request = $request->withUri($request->getUri()->withScheme('https')->withPort(443));
}
$localeManager = $container['locale'];
$smartyPlugins = new SmartyPlugins($container['router'], $request->getUri()->withUserInfo(null));
$view->registerPlugin('function', 'path_for', [$smartyPlugins, 'pathFor']);
$view->registerPlugin('function', 'base_url', [$smartyPlugins, 'baseUrl']);
$view->registerPlugin('block', 't', [$localeManager, 'smartyTranslate']);
return $view;
}