0

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.

pietrosld
  • 21
  • 2
  • 6

2 Answers2

2

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, '', '&') : '');
}

}
Alex
  • 32,506
  • 16
  • 106
  • 171
-1

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.

Jevgeni Smirnov
  • 3,787
  • 5
  • 33
  • 50
  • 1
    already done, the url are working fine. The problem is that the language switcher put the wrong "category url key" in the switch URL the function that gives me worng urls is **$_lang->getCurrentUrl(false)** in add/design/frontend/base/default/template/page/switch/languages.phtml thankyou anyway :) – pietrosld Feb 21 '12 at 08:32