Magento:浏览器检测加载正确的语言

问题描述:

如何在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

再次谢谢你!

浏览器发送的请求有一个名为"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

只需粘贴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文件中的任何代码,你应该没问题!