From b4dd0aeb2932cc254ab55d1bb6b1d8508df63658 Mon Sep 17 00:00:00 2001 From: Pierre Rudloff Date: Tue, 30 May 2017 22:20:16 +0200 Subject: [PATCH] Basic locale switcher --- classes/LocaleManager.php | 77 +++++++++++++++++++++++++++++++++ classes/LocaleMiddleware.php | 21 ++++++--- controllers/FrontController.php | 23 ++++++++++ css/style.css | 7 ++- index.php | 8 +++- templates/inc/head.tpl | 2 +- templates/inc/header.tpl | 7 +++ 7 files changed, 135 insertions(+), 10 deletions(-) create mode 100644 classes/LocaleManager.php diff --git a/classes/LocaleManager.php b/classes/LocaleManager.php new file mode 100644 index 0000000..f581bc5 --- /dev/null +++ b/classes/LocaleManager.php @@ -0,0 +1,77 @@ +newInstance($cookies); + $this->sessionSegment = $session->getSegment('Alltube\LocaleManager'); + $this->setLocale($this->sessionSegment->get('locale')); + } + + /** + * Get a list of supported locales. + * + * @return array + */ + public function getSupportedLocales() + { + $return = []; + foreach ($this->supportedLocales as $supportedLocale) { + $return[$supportedLocale] = \Locale::getDisplayName($supportedLocale, $this->curLocale); + } + + return $return; + } + + /** + * Get the current locale. + * + * @return string + */ + public function getLocale() + { + return $this->curLocale; + } + + /** + * Set the current locale. + * + * @param string $locale Locale code. + */ + public function setLocale($locale) + { + putenv('LANG='.$locale); + setlocale(LC_ALL, [$locale, $locale.'.utf8']); + $this->curLocale = $locale; + $this->sessionSegment->set('locale', $locale); + } +} diff --git a/classes/LocaleMiddleware.php b/classes/LocaleMiddleware.php index d885acf..19593c5 100644 --- a/classes/LocaleMiddleware.php +++ b/classes/LocaleMiddleware.php @@ -5,6 +5,7 @@ namespace Alltube; +use Psr\Container\ContainerInterface; use Slim\Http\Request; use Slim\Http\Response; use Teto\HTTP\AcceptLanguage; @@ -15,11 +16,14 @@ use Teto\HTTP\AcceptLanguage; class LocaleMiddleware { /** - * Supported locales. + * LocaleMiddleware constructor. * - * @var array + * @param ContainerInterface $container Slim dependency container */ - private $locales = ['fr_FR', 'zh_CN']; + public function __construct(ContainerInterface $container) + { + $this->locale = $container->get('locale'); + } /** * Test if a locale can be used for the current user. @@ -30,7 +34,7 @@ class LocaleMiddleware */ public function testLocale(array $proposedLocale) { - foreach ($this->locales as $locale) { + foreach ($this->locale->getSupportedLocales() as $locale => $name) { $parsedLocale = AcceptLanguage::parse($locale); if (isset($proposedLocale['language']) && $parsedLocale[1]['language'] == $proposedLocale['language'] @@ -53,9 +57,12 @@ class LocaleMiddleware public function __invoke(Request $request, Response $response, callable $next) { $headers = $request->getHeader('Accept-Language'); - $locale = AcceptLanguage::detect([$this, 'testLocale'], 'en_US', $headers[0]); - putenv('LANG='.$locale); - setlocale(LC_ALL, [$locale, $locale.'.utf8']); + $curLocale = $this->locale->getLocale(); + if (!isset($curLocale)) { + $this->locale->setLocale( + AcceptLanguage::detect([$this, 'testLocale'], 'en_US', $headers[0]) + ); + } return $next($request, $response); } diff --git a/controllers/FrontController.php b/controllers/FrontController.php index bbcc358..5a2aa16 100644 --- a/controllers/FrontController.php +++ b/controllers/FrontController.php @@ -78,6 +78,7 @@ class FrontController $this->download = new VideoDownload(); $this->container = $container; $this->view = $this->container->get('view'); + $this->locale = $this->container->get('locale'); $session_factory = new \Aura\Session\SessionFactory(); $session = $session_factory->newInstance($cookies); $this->sessionSegment = $session->getSegment('Alltube\Controller\FrontController'); @@ -106,12 +107,30 @@ class FrontController 'description' => 'Easily download videos from Youtube, Dailymotion, Vimeo and other websites.', 'domain' => $uri->getScheme().'://'.$uri->getAuthority(), 'canonical' => $this->getCanonicalUrl($request), + 'locales' => $this->locale->getSupportedLocales(), + 'locale' => $this->locale->getLocale(), ] ); return $response; } + /** + * Switch locale. + * + * @param Request $request PSR-7 request + * @param Response $response PSR-7 response + * @param array $data Query parameters + * + * @return Response + */ + public function locale(Request $request, Response $response, array $data) + { + $this->locale->setLocale($data['locale']); + + return $response->withRedirect($this->container->get('router')->pathFor('index')); + } + /** * Display a list of extractors. * @@ -132,6 +151,7 @@ class FrontController 'description' => 'List of all supported websites from which Alltube Download '. 'can extract video or audio files', 'canonical' => $this->getCanonicalUrl($request), + 'locale' => $this->locale->getLocale(), ] ); @@ -156,6 +176,7 @@ class FrontController 'title' => 'Password prompt', 'description' => 'You need a password in order to download this video with Alltube Download', 'canonical' => $this->getCanonicalUrl($request), + 'locale' => $this->locale->getLocale(), ] ); @@ -246,6 +267,7 @@ class FrontController 'protocol' => $protocol, 'config' => $this->config, 'canonical' => $this->getCanonicalUrl($request), + 'locale' => $this->locale->getLocale(), ] ); @@ -297,6 +319,7 @@ class FrontController 'class' => 'video', 'title' => 'Error', 'canonical' => $this->getCanonicalUrl($request), + 'locale' => $this->locale->getLocale(), ] ); diff --git a/css/style.css b/css/style.css index a055a97..42b1bc9 100644 --- a/css/style.css +++ b/css/style.css @@ -23,7 +23,7 @@ header { {padding-right:21px;} -header a +header .social a { overflow:hidden; height:38px; @@ -595,6 +595,11 @@ h1 { font-family:monospace; } +.locales { + float: left; + text-align: left; +} + @media (max-width: 640px) { .formats, .thumb { diff --git a/index.php b/index.php index 0be9a33..89d76e9 100644 --- a/index.php +++ b/index.php @@ -3,6 +3,7 @@ require_once __DIR__.'/vendor/autoload.php'; use Alltube\Config; use Alltube\Controller\FrontController; +use Alltube\LocaleManager; use Alltube\LocaleMiddleware; use Alltube\PlaylistArchiveStream; use Alltube\UglyRouter; @@ -17,13 +18,14 @@ if (isset($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], '/index.ph stream_wrapper_register('playlist', PlaylistArchiveStream::class); $app = new App(); -$app->add(new LocaleMiddleware()); $container = $app->getContainer(); $config = Config::getInstance(); if ($config->uglyUrls) { $container['router'] = new UglyRouter(); } $container['view'] = ViewFactory::create($container); +$container['locale'] = new LocaleManager($_COOKIE); +$app->add(new LocaleMiddleware($container)); $controller = new FrontController($container, null, $_COOKIE); @@ -45,4 +47,8 @@ $app->get( '/redirect', [$controller, 'redirect'] )->setName('redirect'); +$app->get( + '/locale/{locale}', + [$controller, 'locale'] +)->setName('locale'); $app->run(); diff --git a/templates/inc/head.tpl b/templates/inc/head.tpl index 3c8c116..3fb8052 100644 --- a/templates/inc/head.tpl +++ b/templates/inc/head.tpl @@ -1,6 +1,6 @@ {locale path="../i18n" domain="Alltube"} - + diff --git a/templates/inc/header.tpl b/templates/inc/header.tpl index 628b0f7..a978372 100644 --- a/templates/inc/header.tpl +++ b/templates/inc/header.tpl @@ -4,5 +4,12 @@ {t}Share on Twitter{/t}
{t}Share on Facebook{/t}
+