Actions

API16

JLanguageHelper/detectLanguage

From Joomla! Documentation

< API16:JLanguageHelper
Replacement filing cabinet.png
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.

Contents

Description

Tries to detect the language.

[<! removed edit link to red link >]

<! removed transcluded page call, red link never existed >

Syntax

static detectLanguage()


Returns

string locale

Defined in

libraries/joomla/language/helper.php

Importing

jimport( 'joomla.language.helper' );

Source Body

public static function detectLanguage()
{
        if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
        {
                $systemLangs    = JLanguage::getKnownLanguages();
                $browserLangs   = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
 
                foreach ($browserLangs as $browserLang)
                {
                        // slice out the part before ; on first step, the part before - on second, place into array
                        $browserLang = substr($browserLang, 0, strcspn($browserLang, ';'));
                        $primary_browserLang = substr($browserLang, 0, 2);
 
                        foreach($systemLangs as $systemLang => $metadata)
                        {
                        // take off 3 letters iso code languages as they can't match browsers' languages and default them to en
                                $Jinstall_lang = $metadata['tag'];
 
                                if (strlen($Jinstall_lang) < 6)
                                {
                                        if (strtolower($browserLang) == strtolower(substr($metadata['tag'], 0, strlen($browserLang)))) {
                                                return $systemLang;
                                        }
                                        else if ($primary_browserLang == substr($metadata['tag'], 0, 2)) {
                                                $primaryDetectedLang = $systemLang;
                                        }
                                }
                        }
 
                        if (isset($primaryDetectedLang)) {
                                return $primaryDetectedLang;
                        }
                }
        }
 
        return 'en-GB';
}

[<! removed edit link to red link >] <! removed transcluded page call, red link never existed >

Examples

<CodeExamplesForm />