title: Magento: Browser detection to load right language
body: <p>How do I implement a browser detection into Magento to load the right language.</p>
  
  <p>Example:
  If a US User is surfing to my Magento shop, Magento should load the path: ..myshop../usa/
  usa=storecode
  If a japanese User is surfing to my Magento shop, Magento should load the path: ..myshop../jp/
  jp=storecode
  and so on</p>
  
  <p>I guess I have to adapt the .htaccess with rewrite Urls, but I never did that before. How do I have to do it?</p>
  
  <p>How does the code of browser detection look like and where do I have to put it? In the header.phtml?</p>
  
  <p>thank you very very much in advance!</p>
  
  <p>Edit:
  index.php in CE 1.7.0.2 looks like this</p>
  
  <pre><code>    /**
   * Error reporting
   */
  error_reporting(E_ALL | E_STRICT);
  
  /**
   * Compilation includes configuration file
   */
  define('MAGENTO_ROOT', getcwd());
  
  $compilerConfig = MAGENTO_ROOT . '/includes/config.php';
  if (file_exists($compilerConfig)) {
      include $compilerConfig;
  }
  
  $mageFilename = MAGENTO_ROOT . '/app/Mage.php';
  $maintenanceFile = 'maintenance.flag';
  
  if (!file_exists($mageFilename)) {
      if (is_dir('downloader')) {
          header("Location: downloader");
      } else {
          echo $mageFilename." was not found";
      }
      exit;
  }
  
  if (file_exists($maintenanceFile)) {
      include_once dirname(__FILE__) . '/errors/503.php';
      exit;
  }
  
  require_once $mageFilename;
  
  #Varien_Profiler::enable();
  
  if (isset($_SERVER['MAGE_IS_DEVELOPER_MODE'])) {
      Mage::setIsDeveloperMode(true);
  }
  
  #ini_set('display_errors', 1);
  
  umask(0);
  
  /* Store or website code */
  $mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';
  
  /* Run store or run website */
  $mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';
  
  Mage::run($mageRunCode, $mageRunType);
  </code></pre>
  
  <p>But this <a href="http://www.magentocommerce.com/wiki/multi-store_set_up/how_to_automatically_redirect_to_a_store_view_based_on_the_browser_language" rel="nofollow">Link</a> describes the follwing code which you cannot simply replace:</p>
  
  <pre><code>require_once 'app/Mage.php';
  
  /* Determine correct language store based on browser */
  function getStoreForLanguage()
  {
      if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
          foreach (explode(",", strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE'])) as $accept) {
              if (preg_match("!([a-z-]+)(;q=([0-9.]+))?!", trim($accept), $found)) {
                  $langs[] = $found[1];
                  $quality[] = (isset($found[3]) ? (float) $found[3] : 1.0);
              }
          }
          // Order the codes by quality
          array_multisort($quality, SORT_NUMERIC, SORT_DESC, $langs);
          // get list of stores and use the store code for the key
          $stores = Mage::app()-&gt;getStores(false, true);
          // iterate through languages found in the accept-language header
          foreach ($langs as $lang) {
              $lang = substr($lang,0,2);
              if (isset($stores[$lang]) &amp;&amp; $stores[$lang]-&gt;getIsActive()) return $stores[$lang];
          }
      }
      return Mage::app()-&gt;getStore();
  }
  
  /* Auto redirect to language store view if request is for root */
  if ($_SERVER['REQUEST_URI'] === '/') {
      header('Location: '.getStoreForLanguage()-&gt;getBaseUrl());
      exit;
  }
  
  #Varien_Profiler::enable();
  
  #Mage::setIsDeveloperMode(true);
  
  #ini_set('display_errors', 1);
  
  umask(0);
  Mage::run();
  </code></pre>
  
  <p>Can anybody help me to find out where to put it or where to adapt the index.php</p>
  
  <p>Thank you again!</p>