Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWebsite Automation

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Alternate Language Redirect

The browser provides information about the user's preferred language. (Spoken language, not programming language.)

I'll show you how to redirect visitors to your alternate language pages using the language information from the browser.

Browser language detection can be done with PHP and with JavaScript.

JavaScript is less reliable than PHP and has a response lag. It is less reliable because not all browsers have JavaScript turned on. There is a response lag because at least part of the web page has to be sent to the browser before the redirect can happen.

If PHP can not be used, JavaScript may be acceptable.

Both PHP and JavaScript language-detection redirect code are presented in this article.

Caveat: The redirect code may not be on any of the destination pages or you're likely to end up with an infinite redirect loop.

PHP Browser Language Detection and Redirect

In order to accomplish the redirect, the PHP code must be at the top of the web page source code. No characters or space may precede it.

Here is the PHP code. Customization notes follow.

<?php // Needs to be first line of page.
/* 
   Language Browser Language Detection and Redirect
   Version 1.0
   August 21, 2010

   Will Bontrager
   https://www.willmaster.com/
   Copyright 2010 Bontrager Connection, LLC

   Bontrager Connection, LLC grants you 
   a royalty free license to use or modify 
   this software provided this notice appears 
   on all copies. This software is provided 
   "AS IS," without a warranty of any kind.
*/

// Two customization steps --
//
// Step 1:
// Specify each applicable language code and its URL,
//   one line per code, in this format:
//
//   $Destination['code'] = 'https://www.willmaster.com/blog/';

$Destination['en-au'] = 'https://www.willmaster.com/blog/au.html';
$Destination['en-us'] = 'https://www.willmaster.com/blog/us.html';
$Destination['en'] = 'https://www.willmaster.com/blog/english.html';
$Destination['es'] = 'https://www.willmaster.com/blog/spanish.html';
$Destination['ru'] = 'https://www.willmaster.com/blog/russian.html';
$Destination['ar-sa'] = 'https://www.willmaster.com/blog/saudi.html';


// Step 2:
// Specify the default destination URL for when none of 
//   the above match. (May be blank for no default redirect.)

$DefaultDestination = 'https://www.willmaster.com/';


 // No other customization required. //
//////////////////////////////////////
$lang = preg_replace('/;.*$/','',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
$lang = preg_replace('/,.*$/','',strtolower($lang));
$dest = '';
if( isset($Destination[$lang]) ) { $dest = $Destination[$lang]; }
if( empty($dest) )
{
   $lang = substr($lang,0,2);
   if( isset($Destination[$lang]) ) { $dest = $Destination[$lang]; }
   else { $dest = $DefaultDestination; }
}
if( ! empty($dest) )
{
   header("Location: $dest");
   exit;
}
?>

Customization notes:

The PHP code has two places to customize. The first is a list of languages and destination URLs. the second is for the default destination URL.

  1. List of language and destination URLs.

    Use the PHP code as an example. Specify the language code and URL of each language page you have ready, one per line. Specify language codes as all lower-case characters.

    A list of language codes is at Web Browser Language Identification Codes

  2. Default destination URL.

    Specify the URL to display if none of the language codes specified above apply. This may be blank.

    If blank and none of the language codes apply, no redirect occurs. The current page will be loaded into the browser window.

JavaScript Browser Language Detection and Redirect

The higher in the web page code the JavaScript is at, the sooner the redirect happens.

Here is the JavaScript code. Customization notes follow.

<script type="text/javascript">
/* 
   Language Browser Language Detection and Redirect
   Version 1.0
   August 21, 2010

   Will Bontrager
   https://www.willmaster.com/
   Copyright 2010 Bontrager Connection, LLC

   Bontrager Connection, LLC grants you 
   a royalty free license to use or modify 
   this software provided this notice appears 
   on all copies. This software is provided 
   "AS IS," without a warranty of any kind.
*/

var Destination = new Array(); // Leave line as is.

// Two customization steps --
//
// Step 1:
// Specify each applicable language code and its URL,
//   one line per code, in this format:
//
//   Destination["code"] = "https://www.willmaster.com/blog/";

Destination["en-au"] = "https://www.willmaster.com/blog/au.html";
Destination["en-us"] = "https://www.willmaster.com/blog/us.html";
Destination["en"] = "https://www.willmaster.com/blog/english.html";
Destination["es"] = "https://www.willmaster.com/blog/spanish.html";
Destination["ru"] = "https://www.willmaster.com/blog/russian.html";
Destination["ar-sa"] = "https://www.willmaster.com/blog/saudi.html";


// Step 2:
// Specify the default destination URL for when none of 
//   the above match. (May be blank for no default redirect.)

var DefaultDestination = "https://www.willmaster.com/";


 // No other customization required. //
//////////////////////////////////////
var lang = navigator.language ? navigator.language :
           navigator.browserlanguage ? navigator.browserlanguage : 
           navigator.systemLanguage ? navigator.systemLanguage : 
           navigator.userLanguage ? navigator.userLanguage : 
           '---';
lang = lang.toLowerCase();
var dest = new String();
for( var t in Destination ) {
   if( t == lang ) {
      dest = Destination[t];
      break;
      }
   }
if( dest.length == 0 ) {
   lang = lang.substr(0,2);
   for( var t in Destination ) {
      if( t == lang ) {
         dest = Destination[t];
         break;
         }
      }
   }
if( dest.length == 0 ) { dest = DefaultDestination; }
if( dest.length > 0 ) { location.href = dest; }
</script>

Customization notes:

The JavaScript has two places to customize. The first is a list of languages and destination URLs. the second is for the default destination URL.

  1. List of language and destination URLs.

    Use the JavaScript as an example. Specify the language code and URL of each language page you have ready, one per line. Specify language codes as all lower-case characters.

    A list of language codes is at Web Browser Language Identification Codes

  2. Default destination URL.

    Specify the URL to display if none of the language codes specified above apply. This may be blank.

    If blank and none of the language codes apply, no redirect occurs. The current page will be loaded into the browser window.

The Browser Language Redirect Experience

Once the language-detection redirect software is in place and tested, it works transparently.

Whenever a browser visits with an applicable language setting, it is redirected to the relevant web page. A default URL can be specified or left blank to keep the browser on the current page when the browser language is unrecognized.

URLs of new language pages can be added to the code as appropriate.

With the JavaScript version, there may be a response lag. If JavaScript is turned off, the JavaScript version will not redirect the browser.

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC