Detect a Users Language

Detect a Users LanguageShow something like "Language: *Your Language*"

1. Make a PHP file and insert this into it. Lets say language.php

<?php

function lixlpixel_get_env_var($Var)
{
 if(empty($GLOBALS[$Var]))
 {
  $GLOBALS[$Var]=(!empty($GLOBALS['_SERVER'][$Var]))?
  $GLOBALS['_SERVER'][$Var]:
  (!empty($GLOBALS['HTTP_SERVER_VARS'][$Var]))?
  $GLOBALS['HTTP_SERVER_VARS'][$Var]:'';
 }
}

function lixlpixel_detect_lang()
{
 // Detect HTTP_ACCEPT_LANGUAGE & HTTP_USER_AGENT.
 lixlpixel_get_env_var('HTTP_ACCEPT_LANGUAGE');
 lixlpixel_get_env_var('HTTP_USER_AGENT');
 
 $_AL=strtolower($GLOBALS['HTTP_ACCEPT_LANGUAGE']);
 $_UA=strtolower($GLOBALS['HTTP_USER_AGENT']);
 
 foreach($GLOBALS['_LANG'] as $K)
 {
  if(strpos($_AL, $K)===0)
   return $K;
 }
 
 foreach($GLOBALS['_LANG'] as $K)
 {
  if(strpos($_AL, $K)!==false)
   return $K;
 }
 foreach($GLOBALS['_LANG'] as $K)
 {
  if(preg_match("/[[( ]{$K}[;,_-)]/",$_UA))
   return $K;
 }
 
 return $GLOBALS['_DLANG'];
}

// Define default language.
$GLOBALS['_DLANG']='en';

// Define all available languages.
// WARNING: uncomment all available languages

$GLOBALS['_LANG'] = array(
'af', // afrikaans.
'ar', // arabic.
'bg', // bulgarian.
'ca', // catalan.
'cs', // czech.
'da', // danish.
'de', // german.
'el', // greek.
'en', // english.
'es', // spanish.
'et', // estonian.
'fi', // finnish.
'fr', // french.
'gl', // galician.
'he', // hebrew.
'hi', // hindi.
'hr', // croatian.
'hu', // hungarian.
'id', // indonesian.
'it', // italian.
'ja', // japanese.
'ko', // korean.
'ka', // georgian.
'lt', // lithuanian.
'lv', // latvian.
'ms', // malay.
'nl', // dutch.
'no', // norwegian.
'pl', // polish.
'pt', // portuguese.
'ro', // romanian.
'ru', // russian.
'sk', // slovak.
'sl', // slovenian.
'sq', // albanian.
'sr', // serbian.
'sv', // swedish.
'th', // thai.
'tr', // turkish.
'uk', // ukrainian.
'zh' // chinese.
);


echo 'Language: '.lixlpixel_detect_lang(); 

?>

2. Now we go to your main page and put:

<?php include('language.php'); ?>

3. That should be it.


Related Articles

  • Quality of Content Plus A Good SEO Process Equals to Search Engine Top Ranking
    Quality is a very subjective term. The primary mark of a quality web site is the content. This has less to do with the language and more to do with the subject matter under discussion.
  • What is XHTML?
    We have all heard of HTML. HTML stands for Hypertext Markup Language. What is a hypertext markup language, you ask? It is a language for specifying how certain text should appear. When you design a web page, you want certain content or text to be displayed in a specific way.
  • Page Cloaking and Some More
    Web cloaking, a technique used to deliver different web pages to users and browsers, can seem very attractive. But is it really safe? Get an experts opinion.
  • JavaScript Browser Detection
    Browser detection allows you to find out what browser your viewer is using, and then perform a script based on it - or just to send a friendly message to those with your favorite browser.
  • JavaScript Redirection
    Redirection is often used to take viewers to a page depending on their browsers name or version. To redirect a viewer instantly, you just need to add a short command in your head section:
  • Geotargeting Profits: Tools to Harness and Control Your Traffic
    You think knowing your customers
  • 4 Powerful Features of Javascript Programming Language
    In the community of web developers and surfers, Javascript is highly popular as client side scripting language for web browsers. Lets take a look at some of the features of this language.
  • Last Modified and User's Screen Resolution Detection
    An easy and efficient way to write the date a file was last modified.
    We call the function filemtime() that returns an integer (the number of seconds elapsed from Jan 1st, 1970 00:00 GMT) Anyway, to make this integer viewable by human beings, we just format it using date() function.
  • Platform Independent Client-Side Applications with Java
    That very umph comes with Java. Java was originally developed at Sun Microsystems in 1991 to provide a platform-independent programming language and operating system for consumer electronics (TV sets, toasters and VCRs)...
  • Remove Banners And Pop-Ups
    If you want to get rid of a banner on a free server and are willing to risk it, try these simple HTML tricks to remove it.

Contact Web Design Outsource and get started today

Need Website Designing, Development, Redesigning, Maintenance and SEO services or help growing your company's web presence? Request a free Quote Now.