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

时间:2013-10-10 18:31:24

标签: .htaccess magento url rewrite

如何在Magento中实施浏览器检测以加载正确的语言。

实施例: 如果美国用户正在冲我的Magento商店,Magento应加载路径:..myshop ../ usa / 美国=商店代码 如果日本用户正在冲我的Magento商店,Magento应该加载路径:..myshop ../ jp / JP =商店代码 等等

我想我必须使用重写Urls来修改.htaccess,但我之前从未这样做过。我该怎么做?

浏览器检测代码如何显示,我需要在哪里放置?在header.phtml?

提前非常感谢你!

编辑: CE 1.7.0.2中的index.php看起来像这样

    /**
 * 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描述了你不能简单替换的以下代码:

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

再次感谢你!

2 个答案:

答案 0 :(得分:0)

浏览器发送的请求有一个名为"Accept-Language" header的字段。它的格式化并不是那么直观,如果你想正确地做,那就超出了htaccess文件和mod_rewrite正确解析的能力。这是典型的“Accept-Language”请求标题:

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

答案 1 :(得分:0)

只需将以下代码粘贴到CE 1.7.0.2 index.php中的require_once $mageFilename;之后:

/* 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文件中的任何代码,您应该没问题!