Get Free Audit

Redirect Customers to the Other Store with the Correct Language in Magento

Apr 23, 2014

2763 Pavel Novitsky

Redirect Customers to the Other Store with the Correct Language in Magento

Our clients often ask us to make their multilanguage stores automatically redirect customers to the store that has the language of user’s browser. If it’s not possible to define the language or locale is not supported by the store, they want their customers to see the default store.

Model of realization is pretty simple – we check if browser delivers HTTP_ACCEPT_LANGUAGE header, find out what languages are preferable for user, compare browser language and languages supported by the store, and redirect customer to the relevant store.

Explanation of Accept Language header.

If you output $_SERVER[‘HTTP_ACCEPT_LANGUAGE’], we will get the line that looks like that:

ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.2,fr;q=0.2

or

ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4

It’s clear that we’ve got the list of regional locales and languages: «ru-RU» is russian locale, «ru» — «russian», «en-US» is American english, «en» — «any type of english» and «fr» — french. But what’s the difference between «ru-RU» and «ru»? What is the parameter «q=0.8»?

The root of the problem as always in RFC (Hypertext Transfer Protocol — HTTP/1.1).


Then we get «ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.2,fr;q=0.2» that means «I prefer Russian language or any other dialect on the base of Russian. If not, I prefer English» and so on with the other languages in line. Every block contains regional and general code of the locale. Points divide blocks for different languages.

Every language group can have its own parameter «q» («qvalue»). Value of qvalue means approximate coefficient of user’s language preferences, maximum value can be «q=1» (it is also default).

According to this preferences are placed like this: Russian (100%), Russian dialects (80%), American English (60%), any variant of English (20%), and French (20%). Of course, Accept Language can not be the only factor while forming the page. But don’t neglect this method.

Let’s get back to Magento. The subject of redirecting to the different stores is not that new. There’s an example of realization of that switch in wiki of the official website. But anyway all articles propose to edit index.php and content just the search of available language. The system can end up not that flexible and absolutely not protected from rewriting files at upgrade.

We suggest to do this our way – let’s create a module. Fundamentally we have just 2 global steps: the first one is to define preferable language and the second is to redirect user to the store that has that language. We will check and redirect in controller_action_predispatch event. So we define it in config.xml:


We also create data array for forthcoming analysis when the event will be called.

Vlad Yunusov
Partner With Us Let's discuss how to grow your business. Get a Free Quote.
Talk to Vlad


And observer itself and the main logics:

Vlad Yunusov
Partner With Us Looking for a partner that will help you to grow your business? We are the right company to develop your webstore. Feel free to get in touch with us. We will be happy to discuss your business opportunities and provide you with a Free Quote. Talk to Vlad
Tags:

8 Comments

  1. I wasn’t able to come up with a fix for this, I can get the locale rewrites for a product, or a category but when product and category are already in the url there is no rewrite available. I think we would need to change the observer event so that the redirect occurs after Magento has already set the correct rewritten locale url.

    I also found that google is now using locale aware crawlers which resulted in a redirect when I did a fetch as googlebot which is something I want to avoid right now for SEO reasons so I think I will stop redirecting and configure the alternative language hrefs in the header instead.

  2. Thanks for the module code. If you have translated category and product urls the module does not check for a rewrite to the correct language. For example if the english store url is mystore/en/english/product and the german store url is mystore/de/deutsche/produkt and a german language browser visits mystore/en/english/product the redirect is to mystore/de/english/product instead of mystore/de/deutsche/produkt. The category and product url rewrite is not checked.

  3. Maybe problem is generated because I use redirect rule based on SetEnvIf on my .htaccess file:

    SetEnvIf Host www\.mysite\.com MAGE_RUN_CODE=en
    SetEnvIf Host ^mysite\.com MAGE_RUN_CODE=en

    SetEnvIf Host www\.mysite\.it MAGE_RUN_CODE=it
    SetEnvIf Host ^mysite\.it MAGE_RUN_CODE=it

    Do you know if is it possible access them through php?

  4. I noticed another problem: when I try to access to url “www.mysite.com/cars” it transform url in “www.mysite.it/cars?SID=25gaca4vn5h1uq6deksgvjn9m0&___store=it” while I expect “www.mysite.it/auto?SID=25gaca4vn5h1uq6deksgvjn9m0&___store=it” and this generates an 404 error.

  5. @kamzata
    this approach works good if you have one domain for all languages. In your case I’d propose to add additional check to the Belvg_Locale_Model_Observer::checkLocale(): something like «if customer opened non-english website — do not redirect him to another language». I assume if customer googled your website on the specific language he should see the same language on the page.

  6. Works great but now google-bot and others search engine bot can enter only in the “en” store view (I have different domains for each store view). How can I fix this?

Post a new comment

BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *