Improve locale handling

This commit is contained in:
Pierre Rudloff 2017-05-30 23:30:21 +02:00
parent b4dd0aeb29
commit 0f80cbd333
7 changed files with 118 additions and 16 deletions

View File

@ -1,6 +1,7 @@
{
"name": "alltube",
"dependencies": {
"opensans-googlefont": "*"
"opensans-googlefont": "*",
"flag-icon-css": "~2.8.0"
}
}

92
classes/Locale.php Normal file
View File

@ -0,0 +1,92 @@
<?php
/**
* Locale class.
*/
namespace Alltube;
use Teto\HTTP\AcceptLanguage;
/**
* Class used to represent locales.
*/
class Locale
{
/**
* Locale language.
*
* @var string
*/
private $language;
/**
* Locale region.
*
* @var string
*/
private $region;
/**
* Locale constructor.
*
* @param string $locale ISO 15897 code
*/
public function __construct($locale)
{
$parse = AcceptLanguage::parse($locale);
$this->language = $parse[1]['language'];
$this->region = $parse[1]['region'];
}
/**
* Convert the locale to a string.
*
* @return string ISO 15897 code
*/
public function __toString()
{
return $this->getIso15897();
}
/**
* Get the full name of the locale.
*
* @param Locale $displayLocale Locale to get the name in
*
* @return string
*/
public function getFullName(Locale $displayLocale)
{
return \Locale::getDisplayName($this->getIso15897(), $displayLocale->getIso15897());
}
/**
* Get the ISO 15897 code.
*
* @return string
*/
public function getIso15897()
{
return $this->language.'_'.$this->region;
}
/**
* Get the BCP 47 code.
*
* @return string
*/
public function getBcp47()
{
return $this->language.'-'.$this->region;
}
/**
* Get the ISO 3166 code.
*
* @return string
*/
public function getIso3166()
{
return strtolower($this->region);
}
}

View File

@ -20,7 +20,7 @@ class LocaleManager
/**
* Current locale.
*
* @var string
* @var Locale
*/
private $curLocale;
@ -34,19 +34,22 @@ class LocaleManager
$session_factory = new \Aura\Session\SessionFactory();
$session = $session_factory->newInstance($cookies);
$this->sessionSegment = $session->getSegment('Alltube\LocaleManager');
$this->setLocale($this->sessionSegment->get('locale'));
$cookieLocale = $this->sessionSegment->get('locale');
if (isset($cookieLocale)) {
$this->setLocale(new Locale($this->sessionSegment->get('locale')));
}
}
/**
* Get a list of supported locales.
*
* @return array
* @return Locale[]
*/
public function getSupportedLocales()
{
$return = [];
foreach ($this->supportedLocales as $supportedLocale) {
$return[$supportedLocale] = \Locale::getDisplayName($supportedLocale, $this->curLocale);
$return[] = new Locale($supportedLocale);
}
return $return;
@ -55,7 +58,7 @@ class LocaleManager
/**
* Get the current locale.
*
* @return string
* @return Locale
*/
public function getLocale()
{
@ -65,9 +68,9 @@ class LocaleManager
/**
* Set the current locale.
*
* @param string $locale Locale code.
* @param Locale $locale Locale
*/
public function setLocale($locale)
public function setLocale(Locale $locale)
{
putenv('LANG='.$locale);
setlocale(LC_ALL, [$locale, $locale.'.utf8']);

View File

@ -34,7 +34,7 @@ class LocaleMiddleware
*/
public function testLocale(array $proposedLocale)
{
foreach ($this->locale->getSupportedLocales() as $locale => $name) {
foreach ($this->locale->getSupportedLocales() as $locale) {
$parsedLocale = AcceptLanguage::parse($locale);
if (isset($proposedLocale['language'])
&& $parsedLocale[1]['language'] == $proposedLocale['language']
@ -59,9 +59,13 @@ class LocaleMiddleware
$headers = $request->getHeader('Accept-Language');
$curLocale = $this->locale->getLocale();
if (!isset($curLocale)) {
$this->locale->setLocale(
AcceptLanguage::detect([$this, 'testLocale'], 'en_US', $headers[0])
);
if (isset($headers[0])) {
$this->locale->setLocale(
AcceptLanguage::detect([$this, 'testLocale'], 'en_US', $headers[0])
);
} else {
$this->locale->setLocale('en_US');
}
}
return $next($request, $response);

View File

@ -6,6 +6,7 @@
namespace Alltube\Controller;
use Alltube\Config;
use Alltube\Locale;
use Alltube\PasswordException;
use Alltube\VideoDownload;
use Psr\Container\ContainerInterface;
@ -126,7 +127,7 @@ class FrontController
*/
public function locale(Request $request, Response $response, array $data)
{
$this->locale->setLocale($data['locale']);
$this->locale->setLocale(new Locale($data['locale']));
return $response->withRedirect($this->container->get('router')->pathFor('index'));
}

View File

@ -1,6 +1,6 @@
{locale path="../i18n" domain="Alltube"}
<!Doctype HTML>
<html lang="{$locale|replace:'_':'-'}">
<html lang="{$locale->getBcp47()}">
<head>
<meta charset="UTF-8" />
<meta name=viewport content="width=device-width, initial-scale=1">
@ -10,6 +10,7 @@
<meta property="og:description" content="{$description|escape}" />
{/if}
<link rel="stylesheet" href="{base_url}/dist/main.css" />
<link rel="stylesheet" href="{base_url}/bower_components/flag-icon-css/css/flag-icon.min.css" />
<title>AllTube Download{if isset($title)} - {$title|escape}{/if}</title>
<link rel="canonical" href="{$canonical}" />
<link rel="icon" href="{base_url}/img/favicon.png" />

View File

@ -6,8 +6,8 @@
</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 $locales as $supportedLocale}
<li><a href="{path_for name='locale' data=['locale'=>$supportedLocale->getIso15897()]}"><span class="flag-icon flag-icon-{$supportedLocale->getIso3166()}"></span> {$supportedLocale->getFullName($locale)}</a></li>
{/foreach}
{/if}
</ul>