Actions

API15

JLanguageHelper/detectLanguage

From Joomla! Documentation

< API15: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

detectLanguage()


Returns

string locale

Defined in

libraries/joomla/language/helper.php

Importing

jimport( 'joomla.language.helper' );

Source Body

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)
                        {
                                if (strtolower($browserLang) == strtolower(substr($metadata['tag'], 0, strlen($browserLang)))) {
                                        return $systemLang;
                                } elseif ($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 />