14

locale

locale
SYNOPSIS

locale()

DESCRIPTION

locale returns the preferred language extracted from the HTTP request header Accept-Language.

The function returns the ISO 639 language code with the highest quality value. If the header is not present, locale returns false.

locale is used by the dispatch function to determine the language when it is not specified in the URL.

EXAMPLE

If Accept-Language: fr-FR,fr;q=0.9,en;q=0.8 is found in the header, locale returns fr.

CODE
  1. function locale() {
  2.     if (empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
  3.         return false;
  4.     }
  5.  
  6.     $httplanguages = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
  7.  
  8.     $lang = false;
  9.     $quality = 0.0;
  10.  
  11.     $accepted = preg_split('/,\s*/', $httplanguages);
  12.  
  13.     foreach ($accepted as $accept) {
  14.         $match = null;
  15.         $result = preg_match('/^([a-z]{1,8}(?:[-_][a-z]{1,8})*)(?:;\s*q=(0(?:\.[0-9]{1,3})?|1(?:\.0{1,3})?))?$/i', $accept, $match);
  16.  
  17.         if ($result < 1) {
  18.             continue;
  19.         }
  20.  
  21.         $q = isset($match[2]) ? (float)$match[2] : 1.0;
  22.  
  23.         if ($q > $quality) {
  24.             $quality = $q;
  25.  
  26.             $lang = strtok($match[1], '-_');
  27.  
  28.             if ($quality == 1.0) {
  29.                 break;
  30.             }
  31.         }
  32.     }
  33.  
  34.     return $lang;
  35. }
SEE ALSO

engine

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].