Skip to content

Instantly share code, notes, and snippets.

@mondalaci
Last active December 16, 2015 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mondalaci/5363944 to your computer and use it in GitHub Desktop.
Save mondalaci/5363944 to your computer and use it in GitHub Desktop.
Detect primary language by HTTP Accept-Language.
<?php
$accepted_languages = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
$primary_language = $accepted_languages[0];
if ($pos=strpos($primary_language, ';') !== false) {
$primary_language = substr($primary_language, 0, $pos+1); // Throw away quality value.
}
if ($pos=strpos($primary_language, '-') !== false) {
$primary_language = substr($primary_language, 0, $pos+1); // Throw away dialect.
}
header("Location: http://mysite.com/$primary_language"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment