2013-10-10 44 views
0

如何在Magento中實現瀏覽器檢測以加載正確的語言。Magento:瀏覽器檢測加載正確的語言

示例: 如果US用戶是衝浪到我的Magento店,Magento的應該加載路徑:..myshop ../ USA/ USA =商店代碼 如果日語用戶是衝浪到我的Magento店,Magento的應加載路徑:..myshop ../ jp/ jp =商店代碼 依此類推

我想我必須改寫.htaccess重寫Url,但我從來沒有這樣做過。我該怎麼做呢?

瀏覽器檢測代碼是怎麼樣的,我必須把它放在哪裏?在header.phtml?

非常感謝你提前!

編輯: 的index.php在CE 1.7.0.2看起來像這樣

/** 
* 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); 

但這Link介紹follwing代碼,你不能簡單地取代:

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()->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]) && $stores[$lang]->getIsActive()) return $stores[$lang]; 
     } 
    } 
    return Mage::app()->getStore(); 
} 

/* Auto redirect to language store view if request is for root */ 
if ($_SERVER['REQUEST_URI'] === '/') { 
    header('Location: '.getStoreForLanguage()->getBaseUrl()); 
    exit; 
} 

#Varien_Profiler::enable(); 

#Mage::setIsDeveloperMode(true); 

#ini_set('display_errors', 1); 

umask(0); 
Mage::run(); 

任何人可以幫助我找出在哪裏放置或在哪裏適應index.php

再次謝謝你!

回答

0

瀏覽器發送的請求有一個名爲"Accept-Language" header的字段。它的格式不太直觀,如果你想正確地做,超出了htaccess文件和mod_rewrite正確解析的能力。下面是一個典型的「接受語言」請求頭:

Accept-Language: da, en-gb;q=0.8, en;q=0.7 

這意味着:「我更喜歡丹麥,但會接受英式英語和其他類型的英語」

所以你不能簡單地看爲前兩個字母的字段。如果您沒有丹麥語,那麼您必須繼續解析才能找到正確的語言。 Magento的可能有與此處理,例如一些方法:http://www.magentocommerce.com/wiki/multi-store_set_up/how_to_automatically_redirect_to_a_store_view_based_on_the_browser_language

+0

在那個magento鏈接中,它沒有解釋代碼的放置位置。我擔心我會覆蓋一些我仍然需要的東西。我正在使用CE 1.7.0.2,因此我編輯了我最初的問題。將很高興得到另一個提示,因爲我已經嘗試過,併發生錯誤。顯然是因爲我在index.php中使用了有用的smthg – furba

0

只需粘貼require_once $mageFilename;後,下面的代碼在你的CE 1.7.0.2的index.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()->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]) && $stores[$lang]->getIsActive()) return $stores[$lang]; 
     } 
    } 
    return Mage::app()->getStore(); 
} 

/* Auto redirect to language store view if request is for root */ 
if ($_SERVER['REQUEST_URI'] === '/') { 
    header('Location: '.getStoreForLanguage()->getBaseUrl()); 
    exit; 
} 

確保你不刪除或覆蓋你的index.php文件中的任何代碼,你應該沒問題!