Magento 语言切换器:类别名称未在 url 中翻译

发布于 2025-01-07 10:54:09 字数 313 浏览 0 评论 0原文

我有一家商店,有两种语言的 2 个商店视图:意大利语和英语。

对于某些类别,我对意大利语和英语有不同的名称,例如 EN 的 Apparel 和 IT 的 Abbigliamento。

问题是,当我在 mystore.com/it/abbigliamento 时,如果我将语言切换为英语,语言切换器会将我带到 mystore.com/en/abbigliamento 而不是 mystore.com/en/apparel,并给出 404 错误。

语言切换器会更改商店 ID,但不会翻译类别名称

谢谢,Pietro。

I have a store with 2 store views for two languages, italian and english.

For some categories i have different names for italian and english, like Apparel for EN and Abbigliamento for IT.

The problem is that when i am in mystore.com/it/abbigliamento if i switch language to english the language switcher brings me to mystore.com/en/abbigliamento instead of mystore.com/en/apparel, and gives me a 404 error.

the language switcher changes the store id but don't translate the category name

thanks, Pietro.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝情姑娘 2025-01-14 10:54:09

您可以对 Mage_Core_Model_Store 使用重写,如下所示

class Example_StoreUrls_Model_Core_Store extends Mage_Core_Model_Store {


/**
 * Looks up a given request path in the current store (app) and translates it to the
 * value in $this store using the rewrite index
 *
 * You might want to throw exceptions in case of just returning the input URLs during errors.
 *
 * @param $requestPath
 */
public function lookupLocalizedPath($requestPath) {
    $urlRewriteCollectionSource = Mage::getModel('core/url_rewrite')->getCollection();
    $urlRewriteCollectionSource
        ->addFieldToFilter('request_path', $requestPath)
        ->addStoreFilter(Mage::app()->getStore());
    if(count($urlRewriteCollectionSource) == 0) {
        return $requestPath;
    }

    $idPath = $urlRewriteCollectionSource->getFirstItem()->getIdPath();

    $urlRewriteCollectionTarget = Mage::getModel('core/url_rewrite')->getCollection();
    $urlRewriteCollectionTarget
        ->addFieldToFilter('id_path', $idPath)
        ->addStoreFilter($this);

    if(count($urlRewriteCollectionTarget) == 0) {
        return $requestPath;
    }

    return $urlRewriteCollectionTarget->getFirstItem()->getRequestPath();
}

/**
 * Copied from parent + change:
 * Watch out for the inserted line

 * @param bool $fromStore
 * @return string
 */
public function getCurrentUrl($fromStore = true)
{
    $sidQueryParam = $this->_getSession()->getSessionIdQueryParam();
    $requestString = Mage::getSingleton('core/url')->escape(
        ltrim(Mage::app()->getRequest()->getRequestString(), '/'));

    $storeUrl = Mage::app()->getStore()->isCurrentlySecure()
        ? $this->getUrl('', array('_secure' => true))
        : $this->getUrl('');
    $storeParsedUrl = parse_url($storeUrl);

    $storeParsedQuery = array();
    if (isset($storeParsedUrl['query'])) {
        parse_str($storeParsedUrl['query'], $storeParsedQuery);
    }

    $currQuery = Mage::app()->getRequest()->getQuery();
    if (isset($currQuery[$sidQueryParam]) && !empty($currQuery[$sidQueryParam])
        && $this->_getSession()->getSessionIdForHost($storeUrl) != $currQuery[$sidQueryParam]
    ) {
        unset($currQuery[$sidQueryParam]);
    }

    foreach ($currQuery as $k => $v) {
        $storeParsedQuery[$k] = $v;
    }

    // inserted the following line - rest is from core
    $requestString = $this->lookupLocalizedPath($requestString);

    if (!Mage::getStoreConfigFlag(Mage_Core_Model_Store::XML_PATH_STORE_IN_URL, $this->getCode())) {
        $storeParsedQuery['___store'] = $this->getCode();
    }
    if ($fromStore !== false) {
        $storeParsedQuery['___from_store'] = $fromStore === true ? Mage::app()->getStore()->getCode() : $fromStore;
    }

    return $storeParsedUrl['scheme'] . '://' . $storeParsedUrl['host']
    . (isset($storeParsedUrl['port']) ? ':' . $storeParsedUrl['port'] : '')
    . $storeParsedUrl['path'] . $requestString
    . ($storeParsedQuery ? '?'.http_build_query($storeParsedQuery, '', '&') : '');
}

}

You could use a rewrite for Mage_Core_Model_Store as follows

class Example_StoreUrls_Model_Core_Store extends Mage_Core_Model_Store {


/**
 * Looks up a given request path in the current store (app) and translates it to the
 * value in $this store using the rewrite index
 *
 * You might want to throw exceptions in case of just returning the input URLs during errors.
 *
 * @param $requestPath
 */
public function lookupLocalizedPath($requestPath) {
    $urlRewriteCollectionSource = Mage::getModel('core/url_rewrite')->getCollection();
    $urlRewriteCollectionSource
        ->addFieldToFilter('request_path', $requestPath)
        ->addStoreFilter(Mage::app()->getStore());
    if(count($urlRewriteCollectionSource) == 0) {
        return $requestPath;
    }

    $idPath = $urlRewriteCollectionSource->getFirstItem()->getIdPath();

    $urlRewriteCollectionTarget = Mage::getModel('core/url_rewrite')->getCollection();
    $urlRewriteCollectionTarget
        ->addFieldToFilter('id_path', $idPath)
        ->addStoreFilter($this);

    if(count($urlRewriteCollectionTarget) == 0) {
        return $requestPath;
    }

    return $urlRewriteCollectionTarget->getFirstItem()->getRequestPath();
}

/**
 * Copied from parent + change:
 * Watch out for the inserted line

 * @param bool $fromStore
 * @return string
 */
public function getCurrentUrl($fromStore = true)
{
    $sidQueryParam = $this->_getSession()->getSessionIdQueryParam();
    $requestString = Mage::getSingleton('core/url')->escape(
        ltrim(Mage::app()->getRequest()->getRequestString(), '/'));

    $storeUrl = Mage::app()->getStore()->isCurrentlySecure()
        ? $this->getUrl('', array('_secure' => true))
        : $this->getUrl('');
    $storeParsedUrl = parse_url($storeUrl);

    $storeParsedQuery = array();
    if (isset($storeParsedUrl['query'])) {
        parse_str($storeParsedUrl['query'], $storeParsedQuery);
    }

    $currQuery = Mage::app()->getRequest()->getQuery();
    if (isset($currQuery[$sidQueryParam]) && !empty($currQuery[$sidQueryParam])
        && $this->_getSession()->getSessionIdForHost($storeUrl) != $currQuery[$sidQueryParam]
    ) {
        unset($currQuery[$sidQueryParam]);
    }

    foreach ($currQuery as $k => $v) {
        $storeParsedQuery[$k] = $v;
    }

    // inserted the following line - rest is from core
    $requestString = $this->lookupLocalizedPath($requestString);

    if (!Mage::getStoreConfigFlag(Mage_Core_Model_Store::XML_PATH_STORE_IN_URL, $this->getCode())) {
        $storeParsedQuery['___store'] = $this->getCode();
    }
    if ($fromStore !== false) {
        $storeParsedQuery['___from_store'] = $fromStore === true ? Mage::app()->getStore()->getCode() : $fromStore;
    }

    return $storeParsedUrl['scheme'] . '://' . $storeParsedUrl['host']
    . (isset($storeParsedUrl['port']) ? ':' . $storeParsedUrl['port'] : '')
    . $storeParsedUrl['path'] . $requestString
    . ($storeParsedQuery ? '?'.http_build_query($storeParsedQuery, '', '&') : '');
}

}
谁许谁一生繁华 2025-01-14 10:54:09

在 magento admin 中

Catalog->Manage categories

选择类别并选择首选商店视图。您应该在那里编辑并保存“URL key”参数。

如果它仍然显示旧的 url - 清理缓存并进行 url 重写重新索引。

In magento admin in

Catalog->Manage categories

Select category and choose preffered store view. There you should edit and save "URL key" parameter.

In case it still shows old url - clean cache and make url rewrite reindex.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文