Papermashup

Subscribe


Tweets


"RT @bethgordon: .@Tesco Please rethink archaic 'Approved by Mums'. Pretty sure it's not just mothers who make childcare decisions. https://…"

@ashleyford 21 hours ago

"Is there ever a time that @DFS doesn't have a sale on?"

@ashleyford 5 days ago

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

PHP Language Detection

AshleyAshley

I was looking for a simple way to automatically detect a users language using PHP and came across this interesting snippet which does just that. It uses the PHP superglobal ‘HTTP_ACCEPT_LANGUAGE’ which returns something like this for me “en-US,en;q=0.8”. This is the contents language code of the header from the current request, if there is one.

The PHP Code

This code simply detects what language header is set (if there is one) and redirects the user to the relevant subdomain. English speaking users get re-directed to en.mysite.com etc. In the variable $site we’re building an array of all the languages and subdomains supported. This means that down the line if you want to add more support for different languages for instance German ‘de’ you could just append a value and key to the $sites array.

the $lang variable takes our PHP superglobal ‘HTTP_ACCEPT_LANGUAGE’ and you’ll notice that this is wrapped in the PHP function substr(); which takes 3 parameters. The string, which in our case is $_SERVER[‘HTTP_ACCEPT_LANGUAGE’]; we then specify the start and the length values. So as we want to return and match just the first two characters of the string we specify 0,2.

The next section of code is an if statement that basically says if the language code doesn’t exist in the array then we either don’t support that language or a language code wasn’t set so it defaults to ‘en’.

We then redirect the page to the correct subdomain.



<?php  // List of available localised versions as 'lang code' =--> 'url' map
$sites = array(
    "en" => "http://en.mysite.com/",
    "es" => "http://es.mysite.com/",
	"fr" => "http://fr.mysite.com/",
);

// Get 2 char lang code
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);

// Set default language if a '$lang' version of site is not available
if (!in_array($lang, array_keys($sites))){
    $lang = 'en';
}
// Finally redirect to desired location
header('Location: ' . $sites[$lang]);

?>

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

Comments 7
  • Enamul Hoq
    Posted on

    Enamul Hoq Enamul Hoq

    Reply Author

    really nice and helpful post …………thanks for sharing


  • Gomy
    Posted on

    Gomy Gomy

    Reply Author

    Great tut!
    Is there any chance to keep the primary language on the index page?
    Main page: example.com
    English: en.example.com
    French: fr.example.com
    When access example.com and I’m from other country than EN or FR, then to be redirect to example.com


  • Carlo
    Posted on

    Carlo Carlo

    Reply Author

    Hi, thanks for your article.

    Do you know where I need to place this php script ? Can it be on the home page ?

    thanks.


  • GxE
    Posted on

    GxE GxE

    Reply Author

    Thank you! Really a big thank you!
    The easiest and simplest way to solve it, really what I was looking for! Before I got 4-5 PHP files sitting there to do all this, now I need only 1 file.

    Thank you again!


  • Eric
    Posted on

    Eric Eric

    Reply Author

    Great post. You also can you this code:

    if (!isset($sites[$lang])){
    $lang = ‘en’;
    }

    “isset” is faster to execute than in_array. See http://blog.straylightrun.net/2008/12/03/tip-of-the-day-codeissetcode-vs-codein_arraycode/


  • Referencement
    Posted on

    Referencement Referencement

    Reply Author

    Let me try it and tell you guys if it works for subfolders


  • Brecht
    Posted on

    Brecht Brecht

    Reply Author

    Great script! Just what I needed!