Skip to content

Commit

Permalink
Fix browser language detection.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Jun 1, 2017
1 parent 5aaded4 commit aac269c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 20 deletions.
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ language: php

php:
- 7.0
- 5.5
- 5.6
- 7.1

Expand Down Expand Up @@ -35,8 +34,8 @@ before_script:
- sh -c "if [ '$PHPCS' = '1' ]; then composer require cakephp/cakephp-codesniffer; fi"

script:
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.* ]]; then phpunit --coverage-clover=clover.xml; fi
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION != 7.* ]]; then phpunit; fi
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.* ]]; then vendor/bin/phpunit --coverage-clover=clover.xml; fi
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION != 7.* ]]; then vendor/bin/phpunit; fi

- if [[ $PHPCS = 1 ]]; then vendor/bin/phpcs -p --extensions=php --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests; fi

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
"issues":"https://github.com/ADmad/cakephp-i18n/issues"
},
"require": {
"cakephp/cakephp": "~3.0"
"cakephp/cakephp": "^3.4"
},
"require-dev": {
"cakephp/cakephp": "~3.3"
"cakephp/cakephp": "^3.4",
"phpunit/phpunit": "<6.0"
},
"autoload": {
"psr-4": {
Expand All @@ -43,6 +44,5 @@
"dev-master": "1.0.x-dev"
}
},
"minimum-stability": "beta",
"prefer-stable": true
}
3 changes: 1 addition & 2 deletions src/Middleware/I18nMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ public function detectLanguage(ServerRequestInterface $request, $default = null)
$lang = $default;
}

$cakeRequest = new Request();
$browserLangs = $cakeRequest->acceptLanguage();
$browserLangs = $request->acceptLanguage();
foreach ($browserLangs as $k => $langKey) {
if (strpos($langKey, '-') !== false) {
$browserLangs[$k] = substr($langKey, 0, 2);
Expand Down
22 changes: 10 additions & 12 deletions tests/TestCase/Middleware/I18nMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
namespace ADmad\I18n\Test\TestCase\Middleware;

use ADmad\I18n\Middleware\I18nMiddleware;
use Cake\Http\Response;
use Cake\Http\ServerRequestFactory;
use Cake\I18n\I18n;
use Cake\TestSuite\TestCase;
use Locale;
use Zend\Diactoros\Request;
use Zend\Diactoros\Response;

/**
* I18nMiddleware test.
Expand All @@ -23,16 +22,17 @@ public function setUp()
{
parent::setUp();

$this->server = $_SERVER;

$this->locale = Locale::getDefault();

$this->config = [
'defaultLanguage' => 'fr',
'languages' => ['fr', 'en'],
];

$this->request = ServerRequestFactory::fromGlobals([
'REQUEST_URI' => '/',
]);
$_SERVER['REQUEST_URI'] = '/';
$this->request = ServerRequestFactory::fromGlobals();
$this->request = $this->request->withAttribute('webroot', '/');
$this->response = new Response();
$this->next = function ($req, $res) {
Expand All @@ -50,7 +50,7 @@ public function tearDown()
parent::tearDown();

Locale::setDefault($this->locale);
unset($_SERVER['HTTP_ACCEPT_LANGUAGE']);
$_SERVER = $this->server;
}

/**
Expand All @@ -68,9 +68,8 @@ public function testRedirectionFromSiteRoot()
$this->assertEquals(301, $response->getStatusCode());

$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en-US,en;q=0.8,es;q=0.6,da;q=0.4';
$request = ServerRequestFactory::fromGlobals([
'REQUEST_URI' => '/',
]);
$request = ServerRequestFactory::fromGlobals();
$request->webroot = '/';
$middleware = new I18nMiddleware($this->config);
$response = $middleware($request, $this->response, $this->next);

Expand All @@ -86,9 +85,8 @@ public function testRedirectionFromSiteRoot()
*/
public function testLocaleSetting()
{
$request = ServerRequestFactory::fromGlobals([
'REQUEST_URI' => '/fr',
]);
$_SERVER['REQUEST_URI'] = '/fr';
$request = ServerRequestFactory::fromGlobals();
$request = $request->withAttribute('params', ['lang' => 'fr']);
$middleware = new I18nMiddleware($this->config);
$response = $middleware($request, $this->response, $this->next);
Expand Down

0 comments on commit aac269c

Please sign in to comment.