Bug Report #1118
parse http_accept_language header
| Status: | Closed | Start date: | ||
|---|---|---|---|---|
| Priority: | Normal | Due date: | ||
| Assignee: | % Done: | 100% |
||
| Category: | Core | |||
| Target version: | 2.4 | |||
| Resolution: | fixed | Points: |
Description
Hello.
I have interest in parse the HTTP_ACCEPT_LANGUAGE header sent by client. Kohana offers me a method located at Kohana class and called user_agent. If I use Kohana::user_agent('languages') in my code, it return me an array of ordered values sent by the client to server using the following code:
system/core/Kohana.php
$return = array();
if ( ! empty($_SERVER['HTTP_ACCEPT_LANGUAGE']))
{
if (preg_match_all('/[-a-z]{2,}/', strtolower(trim($_SERVER['HTTP_ACCEPT_LANGUAGE'])), $matches))
{
// Found a result
$return = $matchesr0;
}
}
This code simply take the languages codes, even someone specify the q=0 directive which is specified in RFC 2616 (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html).
I think that the code could be similar to:
$return = array();
if ( ! empty($_SERVER['HTTP_ACCEPT_LANGUAGE']))
{
//¿contiene varias especificaciones (separadas por coma)?
if(strpos($_SERVER['HTTP_ACCEPT_LANGUAGE'], ',') === false)
{
//sólo contiene 1 especificación
$spec = array($_SERVER['HTTP_ACCEPT_LANGUAGE']);
}
else
{
//contiene 2 o mas
$spec = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
}
//para cada especificacion...
foreach($spec as $item)
{
//¿tiene qvalue?
if(strpos($item, ';') !== false)
{
//si, tiene
$a = explode(';', $item);
$coding = trim($ar0);
//parseamos para obtener el qvalue
preg_match('/^q=([0-9.]+)$/', trim($ar1), $result);
$qvalue = intval(floatval($resultr1) * 100);
}
else
{
//no, no tiene, se presupone que es 1
$qvalue = 100;
$coding = trim($item);
}
//añadimos los resultados a la lista final
$return[$coding] = $qvalue;
}
//ordenamos la matriz en orden inverso
arsort($return);
}
An alternative would be modify the Request_Core::parse_accept_header to allow the hability to specify the header to parse and use this to obtain a real array of accepted languages in user browser.
Associated revisions
Generalize Accept-* header parsing and consider q values for Accept-Language header. Fixes #1118
History
Updated by Parnell Springmeyer almost 3 years ago
This is looking more like a feature request to me, I can't entirely understand you though...
Updated by Javier Aranda almost 3 years ago
I think that if the original code does not accomplish the RFC2616 specification, is a bug and not a new feature.
Updated by John Heathco almost 3 years ago
As I understand it the q-values signify an order of preference for accepted languages. This is definitely a feature request rather than a bug fix, but a useful feature request nonetheless.
Updated by Javier Aranda almost 3 years ago
Ok, you are the boss ;)
The important subject is that new versions of Kohana includes this new feature.
Updated by Jeremy Bush almost 3 years ago
Do you have a patch?
Updated by Javier Aranda over 2 years ago
- File request.patch added
- 11 set to 2.3.4
I write a patch for the SVN HEAD of 2.4 branch to parse and process the language header. Also, may be interesting write functions to parse al process the encoding and charset headers.
Updated by Jeremy Bush over 2 years ago
- Status changed from New to Assigned
- Assignee set to John Heathco
- Priority changed from High to Normal
Updated by Chris Bandy over 2 years ago
- Assignee changed from John Heathco to Chris Bandy
I'll take this one since I have a related set of changes to make to request.
Updated by Chris Bandy over 2 years ago
- Status changed from Assigned to Closed
- % Done changed from 0 to 100
Applied in changeset r4470.
Updated by Chris Bandy over 2 years ago
- Resolution set to fixed