Checking Browser's Language by 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.

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.)

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';
}

Detecting the browser language of choice with PHP

first of all, you can use the User Agent Switcher extension for firefox to fake useragents and test it, though you'd have to fake the headers of the requests for HTTP_ACCEPT_LANGUAGE, for instance with the Modify Headers extension (which also lets you change the user agents as well as the header)

oh, and it doesn't work in german:

Warning: include(/home/paragrap/public_html/janette/wp-content/themes/Janette/german-home.php) [function.include]: failed to open stream: No such file or directory in /home/paragrap/public_html/janette/wp-content/themes/Janette/home.php on line 4

Warning: include() [function.include]: Failed opening '/home/paragrap/public_html/janette/wp-content/themes/Janette/german-home.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/paragrap/public_html/janette/wp-content/themes/Janette/home.php on line 4

Browser language from PHP or from Javascript?

Actually they will both capture the language set in the browser. The main difference is that the $_SERVER supports multiple languages in order of preference, whereas navigator.language will only be able to select the first language the browser prefers.

Another difference is that one is captured at client side using JavaScript, and the other at the server side using HTTP headers. It is fair to say (even though headers can be spoofed) that capturing the value at the server side is more stable.

Say in Firefox using tools->options->content->language. If I set language to say Chinese

Then when I make a request and examine the HTTP headers it will say:

Accept-Language: zh,en-us;q=0.7,en;q=0.3 //zh is the two letter Chinese representation

and $_SERVER["HTTP_ACCEPT_LANGUAGE"] will give me zh,en-us;q=0.7,en;q=0.3

navigator.language will give me only zh

So in the header you can see that you can support multiple language options in order. So if for some reason Chinese cannot be rendered, you can check for the second language of preference.

If you use navigator.language you cannot do that..



Related Topics



Leave a reply



Submit