Collections functions
General functions
Node functions
Render functions
Theme permission functions
User functions
Resource functions

http_get_preferred_language()

Description

Use the browser settings to determine the default / preferred language

Parameters

ColumnTypeDefaultDescription
$strict_mode boolean false

Return

string The language string the user may prefer

Location

include/language_functions.php lines 553 to 602

Definition

 
function http_get_preferred_language($strict_mode false)
{
    global 
$languages;

    if (empty(
$_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
        return 
null;
    }

    
$accepted_languages preg_split('/,\s*/'$_SERVER['HTTP_ACCEPT_LANGUAGE']);
    
$current_lang false;
    
$current_quality 0;
    
$language_map = array();

    foreach (
$languages as $key => $value) {
        
$language_map[strtolower($key)] = $key;
    }

    foreach (
$accepted_languages as $accepted_language) {
        
$res preg_match('/^([a-z]{1,8}(?:-[a-z]{1,8})*)(?:;\s*q=(0(?:\.[0-9]{1,3})?|1(?:\.0{1,3})?))?$/i'$accepted_language$matches);

        if (!
$res) {
            continue;
        }

        
$lang_code explode('-'$matches[1]);

        
// Use specified quality, if any
        
if (isset($matches[2])) {
            
$lang_quality = (float)$matches[2];
        } else {
            
$lang_quality 1.0;
        }

        while (
count($lang_code)) {
            
$short strtolower(join('-'$lang_code));
            if (
array_key_exists($short$language_map) && $lang_quality $current_quality) {
                
$current_lang $language_map[$short];
                
$current_quality $lang_quality;
            }

            if (
$strict_mode) {
                break;
            }

            
array_pop($lang_code);
        }
    }

    return 
$current_lang;
}

This article was last updated 23rd July 2024 20:35 Europe/London time based on the source file dated 3rd May 2024 14:45 Europe/London time.