title: Detect Browser Language in PHP and set $locale accordingly
body: <p>I'm trying to achieve, that when someone visits my wordpress page the .po (language-) packages of his pefered language are loaded. 
  At the moment it is possible to change the language by adding a ?lang= parameter to the URL. But i want the right language to be selected based on the browser language.</p>
  
  <p>My code:</p>
  
  <pre><code>&lt;?php
  // start the session 
  session_start();
  $browserlang = " ".$_SERVER['HTTP_ACCEPT_LANGUAGE'];
  
  // if there's a "lang" parameter in the URL...  
  if( isset( $_GET[ 'lang' ] ) ) { 
  
      // ...set a session variable named WPLANG based on the URL parameter...     
      $_SESSION[ 'WPLANG' ] = $_GET[ 'lang' ]; 
  
      // ...and define the WPLANG constant with the WPLANG session variable 
      $locale = $_SESSION[ 'WPLANG' ];
      echo 'Based on URL parameter';
  
  // if there isn't a "lang" parameter in the URL...  
  } else {
  
      // if the WPLANG session variable is already set...
      if( isset( $_SESSION[ 'WPLANG' ] ) ) {
  
          // ...define the WPLANG constant with the WPLANG session variable 
          $locale = $_SESSION[ 'WPLANG' ];
          echo 'Based on session variable';
  
     // if the WPLANG session variable isn't set...
     } else { 
  
          // set the WPLANG constant to your default language code is (or empty, if you don't need it)        
          $locale = $browserlang;
          echo 'Should be based on browser language. It is:' . $browserlang;
  
      } 
  };
  ?&gt;
  </code></pre>
  
  <p>$locale is used to set the language and select the right .po files.</p>
  
  <p>Now i want $locale to be </p>
  
  <blockquote>
    <p>$locale = 'en_US'</p>
  </blockquote>
  
  <p>by default, but when someone enters the page that has default language "de", "de_DE", "de_CH" or "de_AT" it should be.</p>
  
  <blockquote>
    <p>$locale = 'de_DE'</p>
  </blockquote>
  
  <p>The Code im currently using isnt working.</p>
  
  <pre><code>$browserlang = " ".$_SERVER['HTTP_ACCEPT_LANGUAGE'];
  echo $browserlang;
  </code></pre>
  
  <p>Shows me the right language, which is "de_DE", but <code>$locale = $browserlang</code> doesnt do anything. On the other hand when i set <code>$locale = 'de_DE'</code> it works...</p>
  
  <p>Thank you guys in advance.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>When i use <code>echo $locale</code> is says de-DE. Thats very stange, because it doesnt work...</p>
  
  <p><strong>Edit 2:</strong></p>
  
  <p>Thats because it must be de_DE (underline) not de-DE (minus)... how to fix that?</p>
  
  <p><strong>Edit 3:</strong></p>
  
  <p>Finally it works.</p>