Skip to content

Instantly share code, notes, and snippets.

@paulund
Last active March 27, 2019 15:04
Show Gist options
  • Save paulund/5894287 to your computer and use it in GitHub Desktop.
Save paulund/5894287 to your computer and use it in GitHub Desktop.
Automatically Detect Browser Language With PHP
<?php
$supportedLangs = array('en-GB', 'fr', 'de');
$languages = explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach($languages as $lang)
{
if(in_array($lang, $supportedLangs))
{
// Set the page locale to the first supported language found
$page->setLocale($lang);
break;
}
}
<?php
// Detect browser language
$_SERVER['HTTP_ACCEPT_LANGUAGE'];
@SimonEast
Copy link

Unfortunately this won't find a match if $_SERVER['HTTP_ACCEPT_LANGUAGE'] == 'en'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment