feat: Make strings in PHP files translatable

Meaningful strings are now parsed through the getext() function and the "composer update-locales"
script now parses PHP files.

Fixes #143
This commit is contained in:
Pierre Rudloff 2018-01-26 11:37:43 +01:00
parent 9ec3194c5e
commit 986dad5100
5 changed files with 101 additions and 25 deletions

View file

@ -47,6 +47,8 @@ class LocaleManager
if (isset($cookieLocale)) { if (isset($cookieLocale)) {
$this->setLocale(new Locale($cookieLocale)); $this->setLocale(new Locale($cookieLocale));
} }
bindtextdomain('Alltube', __DIR__.'/../i18n/');
textdomain('Alltube');
} }
/** /**

View file

@ -34,6 +34,10 @@ class VideoDownload
} else { } else {
$this->config = Config::getInstance(); $this->config = Config::getInstance();
} }
/*
We don't translate these exceptions because they always occur before Slim can catch them
so they will always go to the logs.
*/
if (!is_file($this->config->youtubedl)) { if (!is_file($this->config->youtubedl)) {
throw new \Exception("Can't find youtube-dl at ".$this->config->youtubedl); throw new \Exception("Can't find youtube-dl at ".$this->config->youtubedl);
} elseif (!$this->checkCommand([$this->config->python, '--version'])) { } elseif (!$this->checkCommand([$this->config->python, '--version'])) {
@ -107,7 +111,7 @@ class VideoDownload
if ($errorOutput == 'ERROR: This video is protected by a password, use the --video-password option') { if ($errorOutput == 'ERROR: This video is protected by a password, use the --video-password option') {
throw new PasswordException($errorOutput); throw new PasswordException($errorOutput);
} elseif (substr($errorOutput, 0, 21) == 'ERROR: Wrong password') { } elseif (substr($errorOutput, 0, 21) == 'ERROR: Wrong password') {
throw new \Exception('Wrong password'); throw new \Exception(_('Wrong password'));
} else { } else {
throw new \Exception($errorOutput); throw new \Exception($errorOutput);
} }
@ -263,7 +267,7 @@ class VideoDownload
private function getAvconvProcess(\stdClass $video, $audioBitrate, $filetype = 'mp3', $audioOnly = true) private function getAvconvProcess(\stdClass $video, $audioBitrate, $filetype = 'mp3', $audioOnly = true)
{ {
if (!$this->checkCommand([$this->config->avconv, '-version'])) { if (!$this->checkCommand([$this->config->avconv, '-version'])) {
throw(new \Exception('Can\'t find avconv or ffmpeg')); throw(new \Exception(_('Can\'t find avconv or ffmpeg.')));
} }
if ($video->protocol == 'rtmp') { if ($video->protocol == 'rtmp') {
@ -319,7 +323,7 @@ class VideoDownload
{ {
$video = $this->getJSON($url, $format, $password); $video = $this->getJSON($url, $format, $password);
if (in_array($video->protocol, ['m3u8', 'm3u8_native'])) { if (in_array($video->protocol, ['m3u8', 'm3u8_native'])) {
throw(new \Exception('Conversion of M3U8 files is not supported.')); throw(new \Exception(_('Conversion of M3U8 files is not supported.')));
} }
$avconvProc = $this->getAvconvProcess($video, $this->config->audioBitrate); $avconvProc = $this->getAvconvProcess($video, $this->config->audioBitrate);
@ -327,7 +331,7 @@ class VideoDownload
$stream = popen($avconvProc->getCommandLine(), 'r'); $stream = popen($avconvProc->getCommandLine(), 'r');
if (!is_resource($stream)) { if (!is_resource($stream)) {
throw new \Exception('Could not open popen stream.'); throw new \Exception(_('Could not open popen stream.'));
} }
return $stream; return $stream;
@ -346,7 +350,7 @@ class VideoDownload
public function getM3uStream(\stdClass $video) public function getM3uStream(\stdClass $video)
{ {
if (!$this->checkCommand([$this->config->avconv, '-version'])) { if (!$this->checkCommand([$this->config->avconv, '-version'])) {
throw(new \Exception('Can\'t find avconv or ffmpeg')); throw(new \Exception(_('Can\'t find avconv or ffmpeg.')));
} }
$process = new Process( $process = new Process(
@ -364,7 +368,7 @@ class VideoDownload
$stream = popen($process->getCommandLine(), 'r'); $stream = popen($process->getCommandLine(), 'r');
if (!is_resource($stream)) { if (!is_resource($stream)) {
throw new \Exception('Could not open popen stream.'); throw new \Exception(_('Could not open popen stream.'));
} }
return $stream; return $stream;
@ -397,7 +401,7 @@ class VideoDownload
$stream = popen($process->getCommandLine(), 'r'); $stream = popen($process->getCommandLine(), 'r');
if (!is_resource($stream)) { if (!is_resource($stream)) {
throw new \Exception('Could not open popen stream.'); throw new \Exception(_('Could not open popen stream.'));
} }
return $stream; return $stream;
@ -430,7 +434,7 @@ class VideoDownload
); );
$stream = popen($process->getCommandLine(), 'r'); $stream = popen($process->getCommandLine(), 'r');
if (!is_resource($stream)) { if (!is_resource($stream)) {
throw new \Exception('Could not open popen stream.'); throw new \Exception(_('Could not open popen stream.'));
} }
return $stream; return $stream;
@ -454,7 +458,7 @@ class VideoDownload
} }
$stream = fopen('playlist://'.implode(';', $playlistItems).'/'.$format, 'r'); $stream = fopen('playlist://'.implode(';', $playlistItems).'/'.$format, 'r');
if (!is_resource($stream)) { if (!is_resource($stream)) {
throw new \Exception('Could not fopen popen stream.'); throw new \Exception(_('Could not open fopen stream.'));
} }
return $stream; return $stream;
@ -478,7 +482,7 @@ class VideoDownload
{ {
$video = $this->getJSON($url, $format, $password); $video = $this->getJSON($url, $format, $password);
if (in_array($video->protocol, ['m3u8', 'm3u8_native'])) { if (in_array($video->protocol, ['m3u8', 'm3u8_native'])) {
throw(new \Exception('Conversion of M3U8 files is not supported.')); throw(new \Exception(_('Conversion of M3U8 files is not supported.')));
} }
$avconvProc = $this->getAvconvProcess($video, $audioBitrate, $filetype, false); $avconvProc = $this->getAvconvProcess($video, $audioBitrate, $filetype, false);
@ -486,7 +490,7 @@ class VideoDownload
$stream = popen($avconvProc->getCommandLine(), 'r'); $stream = popen($avconvProc->getCommandLine(), 'r');
if (!is_resource($stream)) { if (!is_resource($stream)) {
throw new \Exception('Could not open popen stream.'); throw new \Exception(_('Could not open popen stream.'));
} }
return $stream; return $stream;

View file

@ -84,7 +84,10 @@
}, },
"scripts": { "scripts": {
"compile": "composer install --ignore-platform-reqs", "compile": "composer install --ignore-platform-reqs",
"update-locales": "tsmarty2c.php templates > i18n/template.pot", "update-locales": [
"tsmarty2c.php templates > i18n/template.pot",
"xgettext --omit-header -j -o i18n/template.pot classes/* controllers/*"
],
"youtube-dl": "vendor/rg3/youtube-dl/youtube_dl/__main__.py" "youtube-dl": "vendor/rg3/youtube-dl/youtube_dl/__main__.py"
} }
} }

View file

@ -113,7 +113,7 @@ class FrontController
[ [
'config' => $this->config, 'config' => $this->config,
'class' => 'index', 'class' => 'index',
'description' => 'Easily download videos from Youtube, Dailymotion, Vimeo and other websites.', 'description' => _('Easily download videos from Youtube, Dailymotion, Vimeo and other websites.'),
'domain' => $uri->getScheme().'://'.$uri->getAuthority(), 'domain' => $uri->getScheme().'://'.$uri->getAuthority(),
'canonical' => $this->getCanonicalUrl($request), 'canonical' => $this->getCanonicalUrl($request),
'supportedLocales' => $this->localeManager->getSupportedLocales(), 'supportedLocales' => $this->localeManager->getSupportedLocales(),
@ -156,9 +156,9 @@ class FrontController
[ [
'extractors' => $this->download->listExtractors(), 'extractors' => $this->download->listExtractors(),
'class' => 'extractors', 'class' => 'extractors',
'title' => 'Supported websites', 'title' => _('Supported websites'),
'description' => 'List of all supported websites from which Alltube Download '. 'description' => _('List of all supported websites from which Alltube Download '.
'can extract video or audio files', 'can extract video or audio files'),
'canonical' => $this->getCanonicalUrl($request), 'canonical' => $this->getCanonicalUrl($request),
'locale' => $this->localeManager->getLocale(), 'locale' => $this->localeManager->getLocale(),
] ]
@ -182,8 +182,8 @@ class FrontController
'password.tpl', 'password.tpl',
[ [
'class' => 'password', 'class' => 'password',
'title' => 'Password prompt', 'title' => _('Password prompt'),
'description' => 'You need a password in order to download this video with Alltube Download', 'description' => _('You need a password in order to download this video with Alltube Download'),
'canonical' => $this->getCanonicalUrl($request), 'canonical' => $this->getCanonicalUrl($request),
'locale' => $this->localeManager->getLocale(), 'locale' => $this->localeManager->getLocale(),
] ]
@ -258,11 +258,11 @@ class FrontController
} else { } else {
$template = 'video.tpl'; $template = 'video.tpl';
} }
$title = 'Video download'; $title = _('Video download');
$description = 'Download video from '.$video->extractor_key; $description = _('Download video from ').$video->extractor_key;
if (isset($video->title)) { if (isset($video->title)) {
$title = $video->title; $title = $video->title;
$description = 'Download "'.$video->title.'" from '.$video->extractor_key; $description = _('Download').' "'.$video->title.'" '._('from').' '.$video->extractor_key;
} }
$this->view->render( $this->view->render(
$response, $response,
@ -325,7 +325,7 @@ class FrontController
[ [
'errors' => $exception->getMessage(), 'errors' => $exception->getMessage(),
'class' => 'video', 'class' => 'video',
'title' => 'Error', 'title' => _('Error'),
'canonical' => $this->getCanonicalUrl($request), 'canonical' => $this->getCanonicalUrl($request),
'locale' => $this->localeManager->getLocale(), 'locale' => $this->localeManager->getLocale(),
] ]
@ -397,7 +397,7 @@ class FrontController
private function getRemuxStream(array $urls, $format, Response $response, Request $request) private function getRemuxStream(array $urls, $format, Response $response, Request $request)
{ {
if (!$this->config->remux) { if (!$this->config->remux) {
throw new \Exception('You need to enable remux mode to merge two formats.'); throw new \Exception(_('You need to enable remux mode to merge two formats.'));
} }
$stream = $this->download->getRemuxStream($urls); $stream = $this->download->getRemuxStream($urls);
$response = $response->withHeader('Content-Type', 'video/x-matroska'); $response = $response->withHeader('Content-Type', 'video/x-matroska');
@ -464,7 +464,7 @@ class FrontController
); );
} else { } else {
if (empty($videoUrls[0])) { if (empty($videoUrls[0])) {
throw new \Exception("Can't find URL of video"); throw new \Exception(_("Can't find URL of video."));
} }
return $response->withRedirect($videoUrls[0]); return $response->withRedirect($videoUrls[0]);

View file

@ -19,6 +19,7 @@ msgstr ""
#: templates/playlist.tpl:26 templates/password.tpl:10 templates/video.tpl:97 #: templates/playlist.tpl:26 templates/password.tpl:10 templates/video.tpl:97
#: templates/video.tpl:100 templates/index.tpl:19 #: templates/video.tpl:100 templates/index.tpl:19
#: controllers/FrontController.php:265
msgid "Download" msgid "Download"
msgstr "" msgstr ""
@ -38,7 +39,7 @@ msgstr ""
msgid "Video password" msgid "Video password"
msgstr "" msgstr ""
#: templates/extractors.tpl:4 #: templates/extractors.tpl:4 controllers/FrontController.php:159
msgid "Supported websites" msgid "Supported websites"
msgstr "" msgstr ""
@ -137,3 +138,69 @@ msgstr ""
#: templates/index.tpl:32 #: templates/index.tpl:32
msgid "Bookmarklet" msgid "Bookmarklet"
msgstr "" msgstr ""
#: classes/VideoDownload.php:114
msgid "Wrong password"
msgstr ""
#: classes/VideoDownload.php:270 classes/VideoDownload.php:353
msgid "Can't find avconv or ffmpeg."
msgstr ""
#: classes/VideoDownload.php:326 classes/VideoDownload.php:485
msgid "Conversion of M3U8 files is not supported."
msgstr ""
#: classes/VideoDownload.php:334 classes/VideoDownload.php:371
#: classes/VideoDownload.php:404 classes/VideoDownload.php:437
#: classes/VideoDownload.php:493
msgid "Could not open popen stream."
msgstr ""
#: classes/VideoDownload.php:461
msgid "Could not open fopen stream."
msgstr ""
#: controllers/FrontController.php:116
msgid ""
"Easily download videos from Youtube, Dailymotion, Vimeo and other websites."
msgstr ""
#: controllers/FrontController.php:160
msgid ""
"List of all supported websites from which Alltube Download can extract video "
"or audio files"
msgstr ""
#: controllers/FrontController.php:185
msgid "Password prompt"
msgstr ""
#: controllers/FrontController.php:186
msgid ""
"You need a password in order to download this video with Alltube Download"
msgstr ""
#: controllers/FrontController.php:261
msgid "Video download"
msgstr ""
#: controllers/FrontController.php:262
msgid "Download video from "
msgstr ""
#: controllers/FrontController.php:265
msgid "from"
msgstr ""
#: controllers/FrontController.php:328
msgid "Error"
msgstr ""
#: controllers/FrontController.php:400
msgid "You need to enable remux mode to merge two formats."
msgstr ""
#: controllers/FrontController.php:467
msgid "Can't find URL of video."
msgstr ""