I've put together a small script in PHP that checks for the browser's language settings and redirect them to a language version of the site (WP multisite),
function redirect() {
$language = substr( $_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2 );
switch( $language ) {
case 'sv':
header( 'Location: http://www.example.com/sv/' );
break;
case 'no':
header( 'Location: http://www.example.com/no/' );
break;
case 'da':
header( 'Location: http://www.example.com/da/' );
break;
default:
header( 'Location: http://www.example.com/' );
break;
}
}
if ( strlen($url) < 4 ) {
session_start();
if ( empty($_SESSION[ 'language' ]) ) {
$_SESSION[ 'language' ] = true;
redirect();
}
}
When testing with Mobile Safari or Mobile Chrome the redirect doesn't appear to work. Is there any special output for the accept language for mobile browsers that I need to consider?
Update: After some more debugging I found out this:
- Mobile Safari displays the correct language when echoing HTTP_ACCEPT_LANGUAGE but does not redirect.
- Mobile Chrome (iOS only, works on Android) doesn't display the correct language (defaults to "en").
- iOS parses the http header data in a different order, compare iOS Chrome (en-US,en;q=0.8,sv;q=0.6) and OSX Chrome (sv,en-US;q=0.8,en;q=0.6).