简体   繁体   English

Magento商店语言切换

[英]Magento shop language switch

I have a magento shop with 2 languages. 我有一家2种语言的magento商店。 The language switcher works with set a different store view in the url. 语言切换器可与在URL中设置不同的商店视图一起使用。

  • /shop/?___store=myshop_view for mainlanguage /shop/?___store=myshop_view主语言
  • /shop/?___store=myshop_view_fr for french /shop/?___store=myshop_view_fr (法语)

That works fine and how it should work. 那很好,应该如何工作。 Now first problem comes when I am in french store and switch to mainlanguage with /shop/?___store=myshop_view , now I see the shop in the mainlanguage, but when I call /shop/ without any storeview paramenters it switches back to french. 现在,当我在法国商店中并使用/shop/?___store=myshop_view切换到主要语言时,出现了第一个问题,现在我看到了主要语言中的商店,但是当我致电/shop/而没有任何/shop/视图参数时,它将切换回法语。 Why is that and can I change that? 为什么会这样,我可以更改吗?

The second problem comes with externall add to card links. 第二个问题来自外部添加到卡链接。 A add to card link would look like: /shop/checkout/cart/add/uenc/aHR0cDovL3d3dy5teS13aW5lLmNoLnJ1dGlzaGF1c2VyLm5pbmUuY2gv/product/2169/?___SID=U&___store=myshop_view So this link is for the main language. 添加到卡的链接类似于: /shop/checkout/cart/add/uenc/aHR0cDovL3d3dy5teS13aW5lLmNoLnJ1dGlzaGF1c2VyLm5pbmUuY2gv/product/2169/?___SID=U&___store=myshop_view因此该链接适用于。 If the shop was in french before and I click this link, i get the success message in the main language, but the rest of the shop is french again. 如果商店以前是法语的,并且我单击此链接,那么我会收到以主要语言显示的成功消息,但商店的其余部分还是法语。

I think the first and second problem are nearly the same. 我认为第一个和第二个问题几乎相同。 Then the add to card link will redirect to the shop overview without any store view paramenter in the url. 然后,添加到卡链接将重定向到商店概览,而网址中没有任何商店视图参数。

Any idea how I can get a add to card link that can switch the language of the shop? 知道如何获得可以切换商店语言的“添加到卡”链接吗?

As @RohitS said. 正如@RohitS所说​​。 Magento saves the storeview in the cookie, and with the url you can change the storeview temporary, but the value in the cookie is still the same. Magento将storeview保存在cookie中,并且可以使用url临时更改storeview,但是cookie中的值仍然相同。

Good thing is, that the external add to card link is on a subdomain, so I was able to set the cookie for the hole domain (by adding a dot before the domain name: .domain.com). 好东西是,外部添加到卡的链接位于子域上,因此我能够为空域设置cookie(通过在域名:.domain.com之前添加一个点)。 In the cookie I just had to set document.cookie = "store=myshop_view_fr;path=/;domain=.my-shop.com"; 在cookie中,我只需要设置document.cookie = "store=myshop_view_fr;path=/;domain=.my-shop.com"; The path attribute was important for me, cause the shop is in /shop/. 路径属性对我来说很重要,因为商店位于/ shop /中。

You can switch visitors to a store view of their preferred language based upon their location. 您可以根据访问者的位置将访问者切换到其首选语言的商店视图。

First of all, make sure the following configuration settings are met. 首先,请确保满足以下配置设置。

· Your web server must have mod_rewrited enabled. ·您的Web服务器必须启用mod_rewrited。

· Enable URL rewriting (Admin Panel → System → Configuration → Web → Url Options → Use Web Server Rewrites → Yes). ·启用URL重写(“管理面板”→“系统”→“配置”→“ Web”→“ URL选项”→“使用Web服务器重写”→“是”)。

· Enable store code in the urls (Admin Panel → System → Configuration → Web → Url Options → Add store code to Urls → Yes). ·在URL中启用商店代码(管理面板→系统→配置→Web→网址选项→将商店代码添加到Urls→是)。

Next, you have to create store views. 接下来,您必须创建商店视图。 Here we create 2 store views. 在这里,我们创建2个商店视图。 English and Spanish. 英语和西班牙语。

English Store View: 英文商店视图:

· Go to Manage Stores (Admin Panel → System → Manage Stores). ·转到管理商店(管理面板→系统→管理商店)。

· Create a new store view ·创建一个新的商店视图

· Set the store and name to appropriate values (eg Default Store and English) ·将商店和名称设置为适当的值(例如,默认商店和英语)

· Set the code to the country code, in this case en. ·将代码设置为国家代码,在这种情况下为en。 This is the value that will be matched to the browser locale settings. 这是将与浏览器区域设置匹配的值。

· Sort order should remain zero (0) as this will be our default language. ·排序顺序应保持零(0),因为这是我们的默认语言。

Do the same for Spanish store view but set the order to 1 in this case. 对西班牙商店视图执行相同的操作,但在这种情况下将顺序设置为1。

Lastly, we have to modify index.php to enable redirection when a visitor visits the main store url as follows. 最后,我们必须修改index.php以在访客访问主商店URL时启用重定向,如下所示。

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM