Detect user locale

This commit is contained in:
Pierre Rudloff 2017-05-29 21:11:59 +02:00
parent de8c4525d9
commit 68b2cca899
5 changed files with 180 additions and 7 deletions

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

View file

@ -14,7 +14,8 @@
"guzzlehttp/guzzle": "~6.2.0",
"aura/session": "~2.1.0",
"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": {
"symfony/var-dumper": "~3.2.0",

43
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"content-hash": "9793382606938199471cea3e8c7cbb6c",
"content-hash": "44c4a3592a8eac7111a7284b22e55868",
"packages": [
{
"name": "aura/session",
@ -872,6 +872,47 @@
"description": "Symfony Yaml Component",
"homepage": "https://symfony.com",
"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": [

View file

@ -1,13 +1,9 @@
<?php
$language = 'fr_FR';
putenv('LANG='.$language);
setlocale(LC_ALL, [$language, $language.'.utf8']);
require_once __DIR__.'/vendor/autoload.php';
use Alltube\Config;
use Alltube\Controller\FrontController;
use Alltube\LocaleMiddleware;
use Alltube\PlaylistArchiveStream;
use Alltube\UglyRouter;
use Alltube\ViewFactory;
@ -21,6 +17,7 @@ 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) {

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