Navigation Menu

Skip to content

Commit

Permalink
LocaleSelectorFilter now sets the current locale
Browse files Browse the repository at this point in the history
    
LocaleSelectorFilter now sets the current locale based on the HTTP
Accept header, not the default one that is stablished application wide.

fixes #7208
  • Loading branch information
pperejon committed Aug 11, 2015
1 parent fd37e91 commit 48d0c2c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/Routing/Filter/LocaleSelectorFilter.php
Expand Up @@ -17,6 +17,7 @@
use Cake\Event\Event;
use Cake\Routing\DispatcherFilter;
use Locale;
use Cake\I18n\I18n;

/**
* Sets the runtime default locale for the request based on the
Expand Down Expand Up @@ -48,7 +49,7 @@ public function __construct($config = [])
}

/**
* Inspects the request for the Accept-Language header and sets the default
* Inspects the request for the Accept-Language header and sets the
* Locale for the current runtime if it matches the list of valid locales
* as passed in the configuration.
*
Expand All @@ -64,6 +65,6 @@ public function beforeDispatch(Event $event)
return;
}

Locale::setDefault($locale);
I18n::locale($locale);
}
}
11 changes: 6 additions & 5 deletions tests/TestCase/Routing/Filter/LocaleSelectorFilterTest.php
Expand Up @@ -19,6 +19,7 @@
use Cake\Routing\Filter\LocaleSelectorFilter;
use Cake\TestSuite\TestCase;
use Locale;
use Cake\I18n\I18n;

/**
* Locale selector filter test.
Expand Down Expand Up @@ -63,19 +64,19 @@ public function testSimpleSelection()
'environment' => ['HTTP_ACCEPT_LANGUAGE' => 'en-GB,en;q=0.8,es;q=0.6,da;q=0.4']
]);
$filter->beforeDispatch(new Event('name', null, ['request' => $request]));
$this->assertEquals('en_GB', Locale::getDefault());
$this->assertEquals('en_GB', I18n::locale());

$request = new Request([
'environment' => ['HTTP_ACCEPT_LANGUAGE' => 'es_VE,en;q=0.8,es;q=0.6,da;q=0.4']
]);
$filter->beforeDispatch(new Event('name', null, ['request' => $request]));
$this->assertEquals('es_VE', Locale::getDefault());
$this->assertEquals('es_VE', I18n::locale());

$request = new Request([
'environment' => ['HTTP_ACCEPT_LANGUAGE' => 'en;q=0.4,es;q=0.6,da;q=0.8']
]);
$filter->beforeDispatch(new Event('name', null, ['request' => $request]));
$this->assertEquals('da', Locale::getDefault());
$this->assertEquals('da', I18n::locale());
}

/**
Expand All @@ -97,7 +98,7 @@ public function testWithWhitelist()
]
]);
$filter->beforeDispatch(new Event('name', null, ['request' => $request]));
$this->assertEquals('es_VE', Locale::getDefault());
$this->assertEquals('es_VE', I18n::locale());

Locale::setDefault('en_US');
$request = new Request([
Expand All @@ -106,6 +107,6 @@ public function testWithWhitelist()
]
]);
$filter->beforeDispatch(new Event('name', null, ['request' => $request]));
$this->assertEquals('en_US', Locale::getDefault());
$this->assertEquals('en_US', I18n::locale());
}
}

0 comments on commit 48d0c2c

Please sign in to comment.