Detect Browser Language in PHP

Detect Browser Language in PHP

why dont you keep it simple and clean

<?php
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
$acceptLang = ['fr', 'it', 'en'];
$lang = in_array($lang, $acceptLang) ? $lang : 'en';
require_once "index_{$lang}.php";

?>

Detect web browser language using PHP

Based on your example, it seems that you're not concerned with the language localisation (if it's British English or American English for example). Rather you're only concerned that the language preference is English.

That being the case, I would suggest taking only the first two characters from the locale.

$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);

This will return you en, fr, ar, etc, irrespective of the localisation. Use this to set the content language that is returned to the user, and if a language doesn't exist, server a default language, e.g. English.

Detect Browser Language in PHP and set $locale accordingly

Edit 3:

Got it working:

<?php
// start the session
session_start();

// if there's a "lang" parameter in the URL...
if( isset( $_GET[ 'lang' ] ) ) {

// ...set a session variable named WPLANG based on the URL parameter...
$_SESSION[ 'WPLANG' ] = $_GET[ 'lang' ];

// ...and define the WPLANG constant with the WPLANG session variable
$locale = $_SESSION[ 'WPLANG' ];
$languagemsg = 'based on url parameter';

// if there isn't a "lang" parameter in the URL...
} else {

// if the WPLANG session variable is already set...
if( isset( $_SESSION[ 'WPLANG' ] ) ) {

// ...define the WPLANG constant with the WPLANG session variable
$locale = $_SESSION[ 'WPLANG' ];
$languagemsg = 'based on session variable';

// if the WPLANG session variable isn't set...
} else {

// set the WPLANG constant to your default language code is (or empty, if you don't need it)
$browserlang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2);
if ($browserlang == 'de') {$browserlang = 'de_DE';}
else {$browserlang = 'en_US';}
$_SESSION[ 'WPLANG' ] = $browserlang;
$locale = $browserlang;
$languagemsg = 'based on browser language';

}
};
?>

Modern browsers always list the prefered language in front of the others. Thats why i only pick the first two letters of the sting:

$browserlang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2);

now i set $browserlang = de_DE for all language codes that begin with "de". For all other languages i set $browserlang = en_US.

'$languagemsg' is just for debugging reasons.

PHP Detect User language

I'm not going to repeat all the valid answers here, but this link shows another good approach (for multi-lingo websites) taking advantage of http_negotiate_language() method mirror

So combining that mapping and your exciting code you will have:

$map = array("en" => "english", "es" => "spanish");
$userLanguage = $map[ http_negotiate_language(array_keys($map)) ];

if ($userLanguage === 'english') {
echo "we have detected you are English. would you like to visit our site in English?";
} else {
header('location: /index.php?lang=default');
}

But if you are only interested to detect the english (en) language, you might want to shorten the script to:

if ('en' == substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2)) {
echo "we have detected you are English. would you like to visit our site in English?";
} else {
header('location: /index.php?lang=default');
}

PHP: Detect user language and able to change language

Found the way how to do this:

$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);

if ( !empty($_GET['language']) ) {
$_COOKIE['language'] = $_GET['language'] === 'en' ? 'en' : 'ru';
} elseif (empty($_COOKIE['language'])) {
$_COOKIE['language'] = $lang;
}
setcookie('language', $_COOKIE['language']);

if ( $_COOKIE['language'] == "en") {
$language = 'en';
} else {
$language = 'ru';
}

Checking browser's language by PHP?

Likely just a case sensitivity issue; eregi('en-us') or preg_match('/en-us/i') should have picked it up.

However, just looking for ‘en-us’ in the header may get it wrong sometimes, in particular when both the US and UK languages are listed. “Accept-Language” is actually quite a complicated header, which really you'd want a proper parser for.

If you have PECL the whole job is already done for you: http://www.php.net/manual/en/function.http-negotiate-language.php

I don't know why the other answers are going for the User-Agent header; this is utterly bogus. User-Agent is not mandated to hold a language value in any particular place, and for some browsers (eg. Opera, and some minor browser I've never heard of called ‘Internet Explorer’) it will not at all. Where it does contain a language, that'll be the of language the browser build was installed in, not the user's preferred language which is what you should be looking at. (This setting will default to the build language, but can be customised by the user from the preferences UI.)

Detect browser preferred language to serve html pages

You can use HTTP_ACCEPT_LANGUAGE header. Save supported languages to array and then loop the header using foreach.

$languages = array('en', 'fi', 'sv', 'no');
$header = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach($header as $lang) {
if(in_array($lang, $languages)) {
header("Location: $lang.php"); // i.e. fi.php or se.php
break;
}
}

HTTP_ACCEPT_LANGUAGE contains something like this:

Accept-Language: en-gb,en;q=0.5

As you can see, there is multiple languages, en-gb and en. That's why it's clever to loop the header. If you prefer functions, here's one:

function get_user_lang() {
$languages = array('en', 'fi', 'sv', 'no');
$header = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach($header as $lang) {
if(in_array($lang, $languages)) {
return $lang; // i.e. fi.php or se.php
break;
}
}
}
echo 'Your language is ' . get_user_lang();

Step-by-step guide:

  1. Create new files for each language. Name them like this: "fi.php" or "se.php".

  2. Place the first code part to very top of your home page. That file must include .php ending, so it must be php file. If you don't understand, that should be where to place it:

    <?php 
    $languages = array('en', 'fi', 'sv', 'no');
    $header = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
    foreach($header as $lang) {
    if(in_array($lang, $languages)) {
    header("Location: $lang.php"); // i.e. fi.php or se.php
    break;
    }
    }
    ?>
    <!DOCTYPE html>
    <html>
    <head>
    <title>title</title>
    </head>
    <body>
    contents
    </body>
    </html>
  3. Navigate to your home page in your browser. If your browser's language is english, it'll redirect to "en.php", if swedish; "se.php" and so on.

  4. You can see all language codes from this link, swedish is "sv".



Related Topics



Leave a reply



Submit