mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Adding a method to CakeRequest to parse out the accept-language header. This will help remove features from l10n.
This commit is contained in:
parent
af303f01c7
commit
1424e02488
2 changed files with 42 additions and 1 deletions
|
@ -550,7 +550,7 @@ class CakeRequest implements ArrayAccess {
|
|||
* @param string $name Name of the header you want.
|
||||
* @return mixed Either false on no header being set or the value of the header.
|
||||
*/
|
||||
public function header($name) {
|
||||
public static function header($name) {
|
||||
$name = 'HTTP_' . strtoupper(str_replace('-', '_', $name));
|
||||
if (!empty($_SERVER[$name])) {
|
||||
return $_SERVER[$name];
|
||||
|
@ -629,6 +629,26 @@ class CakeRequest implements ArrayAccess {
|
|||
return in_array($type, $acceptTypes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the lanaguages accepted by the client, or check if a specific language is accepted.
|
||||
*
|
||||
* @param string $language The language to test.
|
||||
* @return If a $language is provided, a boolean. Otherwise the array of accepted languages.
|
||||
*/
|
||||
public static function acceptLanguage($language = null) {
|
||||
$accepts = preg_split('/[;,]/', self::header('Accept-Language'));
|
||||
foreach ($accepts as &$accept) {
|
||||
$accept = strtolower($accept);
|
||||
if (strpos($accept, '_') !== false) {
|
||||
$accept = str_replace('_', '-', $accept);
|
||||
}
|
||||
}
|
||||
if ($language === null) {
|
||||
return $accepts;
|
||||
}
|
||||
return in_array($language, $accepts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides a read/write accessor for `$this->data`. Allows you
|
||||
* to use a syntax similar to `CakeSession` for reading post data.
|
||||
|
|
|
@ -1338,6 +1338,27 @@ class CakeRequestTestCase extends CakeTestCase {
|
|||
$this->assertSame('', $request->data['Post']['empty']);
|
||||
}
|
||||
|
||||
/**
|
||||
* test accept language
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function testAcceptLanguage() {
|
||||
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'inexistent,en-ca';
|
||||
$result = CakeRequest::acceptLanguage();
|
||||
$this->assertEquals(array('inexistent', 'en-ca'), $result, 'Languages do not match');
|
||||
|
||||
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx;en_ca';
|
||||
$result = CakeRequest::acceptLanguage();
|
||||
$this->assertEquals(array('es-mx', 'en-ca'), $result, 'Languages do not match');
|
||||
|
||||
$result = CakeRequest::acceptLanguage('en-ca');
|
||||
$this->assertTrue($result);
|
||||
|
||||
$result = CakeRequest::acceptLanguage('en-us');
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* backupEnvironment method
|
||||
*
|
||||
|
|
Loading…
Add table
Reference in a new issue