Basic locale switcher

This commit is contained in:
Pierre Rudloff 2017-05-30 22:20:16 +02:00
parent e64bb9b9f5
commit b4dd0aeb29
7 changed files with 135 additions and 10 deletions

77
classes/LocaleManager.php Normal file
View file

@ -0,0 +1,77 @@
<?php
/**
* LocaleManager class.
*/
namespace Alltube;
/**
* Class used to manage locales.
*/
class LocaleManager
{
/**
* Supported locales.
*
* @var array
*/
private $supportedLocales = ['en_US', 'fr_FR', 'zh_CN'];
/**
* Current locale.
*
* @var string
*/
private $curLocale;
/**
* LocaleManager constructor.
*
* @param array $cookies Cookie array
*/
public function __construct(array $cookies = [])
{
$session_factory = new \Aura\Session\SessionFactory();
$session = $session_factory->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);
}
}

View file

@ -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);
}

View file

@ -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(),
]
);

View file

@ -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 {

View file

@ -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();

View file

@ -1,6 +1,6 @@
{locale path="../i18n" domain="Alltube"}
<!Doctype HTML>
<html lang="en">
<html lang="{$locale|replace:'_':'-'}">
<head>
<meta charset="UTF-8" />
<meta name=viewport content="width=device-width, initial-scale=1">

View file

@ -4,5 +4,12 @@
{t}Share on Twitter{/t}<div class="twittermask"></div></a>
<a class="facebook" href="https://www.facebook.com/sharer/sharer.php?u={base_url|urlencode}" target="_blank">{t}Share on Facebook{/t}<div class="facebookmask"></div></a>
</div>
<ul class="locales">
{if isset($locales)}
{foreach $locales as $locale=>$name}
<li><a href="{path_for name='locale' data=['locale'=>$locale]}">{$name}</a></li>
{/foreach}
{/if}
</ul>
</header>
<div class="wrapper">