Detect user locale
This commit is contained in:
parent
de8c4525d9
commit
68b2cca899
5 changed files with 180 additions and 7 deletions
61
classes/LocaleMiddleware.php
Normal file
61
classes/LocaleMiddleware.php
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* LocaleMiddleware class.
|
||||||
|
*/
|
||||||
|
namespace Alltube;
|
||||||
|
|
||||||
|
use Slim\Http\Request;
|
||||||
|
use Slim\Http\Response;
|
||||||
|
use Teto\HTTP\AcceptLanguage;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Detect user locale.
|
||||||
|
*/
|
||||||
|
class LocaleMiddleware
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Supported locales.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private $locales = ['fr_FR', 'zh_CN'];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test if a locale can be used for the current user.
|
||||||
|
*
|
||||||
|
* @param array $proposedLocale Locale array created by AcceptLanguage::parse()
|
||||||
|
*
|
||||||
|
* @return string Locale name if chosen, nothing otherwise
|
||||||
|
*/
|
||||||
|
public function testLocale(array $proposedLocale)
|
||||||
|
{
|
||||||
|
foreach ($this->locales as $locale) {
|
||||||
|
$parsedLocale = AcceptLanguage::parse($locale);
|
||||||
|
if (isset($proposedLocale['language'])
|
||||||
|
&& $parsedLocale[1]['language'] == $proposedLocale['language']
|
||||||
|
&& $parsedLocale[1]['region'] == $proposedLocale['region']
|
||||||
|
) {
|
||||||
|
return $proposedLocale['language'].'_'.$proposedLocale['region'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Main middleware function
|
||||||
|
*
|
||||||
|
* @param Request $request PSR request
|
||||||
|
* @param Response $response PSR response
|
||||||
|
* @param callable $next Next middleware
|
||||||
|
*
|
||||||
|
* @return Response
|
||||||
|
*/
|
||||||
|
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']);
|
||||||
|
|
||||||
|
return $next($request, $response);
|
||||||
|
}
|
||||||
|
}
|
|
@ -14,7 +14,8 @@
|
||||||
"guzzlehttp/guzzle": "~6.2.0",
|
"guzzlehttp/guzzle": "~6.2.0",
|
||||||
"aura/session": "~2.1.0",
|
"aura/session": "~2.1.0",
|
||||||
"barracudanetworks/archivestream-php": "~1.0.5",
|
"barracudanetworks/archivestream-php": "~1.0.5",
|
||||||
"smarty-gettext/smarty-gettext": "~1.5.1"
|
"smarty-gettext/smarty-gettext": "~1.5.1",
|
||||||
|
"zonuexe/http-accept-language": "~0.4.1"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"symfony/var-dumper": "~3.2.0",
|
"symfony/var-dumper": "~3.2.0",
|
||||||
|
|
43
composer.lock
generated
43
composer.lock
generated
|
@ -4,7 +4,7 @@
|
||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"content-hash": "9793382606938199471cea3e8c7cbb6c",
|
"content-hash": "44c4a3592a8eac7111a7284b22e55868",
|
||||||
"packages": [
|
"packages": [
|
||||||
{
|
{
|
||||||
"name": "aura/session",
|
"name": "aura/session",
|
||||||
|
@ -872,6 +872,47 @@
|
||||||
"description": "Symfony Yaml Component",
|
"description": "Symfony Yaml Component",
|
||||||
"homepage": "https://symfony.com",
|
"homepage": "https://symfony.com",
|
||||||
"time": "2017-05-01T14:55:58+00:00"
|
"time": "2017-05-01T14:55:58+00:00"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "zonuexe/http-accept-language",
|
||||||
|
"version": "0.4.1",
|
||||||
|
"source": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/BaguettePHP/http-accept-language.git",
|
||||||
|
"reference": "f71422b1200737aa9d7c7fa83f07cbe4616198d5"
|
||||||
|
},
|
||||||
|
"dist": {
|
||||||
|
"type": "zip",
|
||||||
|
"url": "https://api.github.com/repos/BaguettePHP/http-accept-language/zipball/f71422b1200737aa9d7c7fa83f07cbe4616198d5",
|
||||||
|
"reference": "f71422b1200737aa9d7c7fa83f07cbe4616198d5",
|
||||||
|
"shasum": ""
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"ext-intl": "*"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"phploc/phploc": "*",
|
||||||
|
"phpunit/phpunit": "4.1.*",
|
||||||
|
"theseer/phpdox": "0.6.*"
|
||||||
|
},
|
||||||
|
"type": "library",
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Teto\\HTTP\\": "src/"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"notification-url": "https://packagist.org/downloads/",
|
||||||
|
"license": [
|
||||||
|
"MIT"
|
||||||
|
],
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "USAMI Kenta",
|
||||||
|
"email": "tadsan@zonu.me"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "HTTP Accept-Language Header parser",
|
||||||
|
"time": "2014-10-19T09:22:18+00:00"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"packages-dev": [
|
"packages-dev": [
|
||||||
|
|
|
@ -1,13 +1,9 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
$language = 'fr_FR';
|
|
||||||
putenv('LANG='.$language);
|
|
||||||
setlocale(LC_ALL, [$language, $language.'.utf8']);
|
|
||||||
|
|
||||||
require_once __DIR__.'/vendor/autoload.php';
|
require_once __DIR__.'/vendor/autoload.php';
|
||||||
use Alltube\Config;
|
use Alltube\Config;
|
||||||
use Alltube\Controller\FrontController;
|
use Alltube\Controller\FrontController;
|
||||||
|
use Alltube\LocaleMiddleware;
|
||||||
use Alltube\PlaylistArchiveStream;
|
use Alltube\PlaylistArchiveStream;
|
||||||
use Alltube\UglyRouter;
|
use Alltube\UglyRouter;
|
||||||
use Alltube\ViewFactory;
|
use Alltube\ViewFactory;
|
||||||
|
@ -21,6 +17,7 @@ if (isset($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], '/index.ph
|
||||||
stream_wrapper_register('playlist', PlaylistArchiveStream::class);
|
stream_wrapper_register('playlist', PlaylistArchiveStream::class);
|
||||||
|
|
||||||
$app = new App();
|
$app = new App();
|
||||||
|
$app->add(new LocaleMiddleware());
|
||||||
$container = $app->getContainer();
|
$container = $app->getContainer();
|
||||||
$config = Config::getInstance();
|
$config = Config::getInstance();
|
||||||
if ($config->uglyUrls) {
|
if ($config->uglyUrls) {
|
||||||
|
|
73
tests/LocaleMiddlewareTest.php
Normal file
73
tests/LocaleMiddlewareTest.php
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* LocaleMiddlewareTest class.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Alltube\Test;
|
||||||
|
|
||||||
|
use Alltube\LocaleMiddleware;
|
||||||
|
use Slim\Http\Environment;
|
||||||
|
use Slim\Http\Request;
|
||||||
|
use Slim\Http\Response;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unit tests for the FrontController class.
|
||||||
|
*/
|
||||||
|
class LocaleMiddlewareTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepare tests.
|
||||||
|
*/
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
$this->middleware = new LocaleMiddleware();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test the testLocale() function.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testTestLocale()
|
||||||
|
{
|
||||||
|
$locale = [
|
||||||
|
'language'=>'fr',
|
||||||
|
'region'=>'FR',
|
||||||
|
];
|
||||||
|
$this->assertEquals('fr_FR', $this->middleware->testLocale($locale));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test the testLocale() function with an unsupported locale.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testLocaleWithWrongLocale()
|
||||||
|
{
|
||||||
|
$locale = [
|
||||||
|
'language'=>'foo',
|
||||||
|
'region'=>'BAR'
|
||||||
|
];
|
||||||
|
$this->assertNull($this->middleware->testLocale($locale));
|
||||||
|
$this->assertNull($this->middleware->testLocale([]));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test the __invoke() function.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testInvoke()
|
||||||
|
{
|
||||||
|
$request = Request::createFromEnvironment(Environment::mock());
|
||||||
|
$this->middleware->__invoke(
|
||||||
|
$request->withHeader('Accept-Language', 'fr-FR'),
|
||||||
|
new Response(),
|
||||||
|
function () {
|
||||||
|
}
|
||||||
|
);
|
||||||
|
$this->assertEquals('fr_FR', getenv('LANG'));
|
||||||
|
$this->assertEquals('fr_FR', setlocale(LC_ALL, null));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue