18

I'd like to configure the Magento site to be able to recognize the default language and location of the user and display the site in that language and with appropriate localization.

For example:

A user from Russia with the Russian language as default will go directly to the Russian Language website with Rubles as the default currency and prices.

Is this possible? If so how?

0

5 Answers 5

14

GeoIP and Store Views will help you solve this issue.

GeoIP You can use a provider like MaxMind for your GeoIP database (or load it as a web server module that adds the country in the header).

Based on the IP address you can redirect the customer to the proper Store View (check the link generated by the store view switcher.

Store Views

Russian should be a Store View. Check the store view switcher to see what is the link you need to use for your customers.

After redirection you can set a cookie to associate the customer with the store view.

2
  • We have also used the GeoIP to switch a few blocks on the site to change things like our Free Shipping message Jan 22, 2013 at 22:19
  • 1
    3 things to watch out for on this topic: (1) If the visitor uses a proxy in another country or the IP is mismatched, he may want to switch to a different website himself. Make that possible. (2) Differentiate between localisation and translation. E.g. use GeoIP for localisation and language acceptance headers for translations (Russian website with Russian/English language, Swiss Website with German/Italian/French language). See answer below. (3) If you have different prices (or a different base currency) for Russia, create a dedicated website for it. Jan 23, 2013 at 5:11
9

There is a Magento GeoIP extension (written by me) which downloads latest MaxMind data file and periodically updates it.

I suggest hooking into controller_front_init_before event. You can use the following code to check get the ISO 3166-1 code of visitor's country and then redirect him to corespondent store view if you have one:

$geoIP = Mage::getSingleton('geoip/country');
$code = $geoIP->getCountry();

/*
 * Check if store view for this country/language exists and it is not a current store view. 
 * If so do a redirect.
 * You can also take a language from user agent into consideration.
 */

The detailed article about using this extension can be found here.

5

take a look at http://www.thefutureoftheweb.com/blog/use-accept-language-header if you can program. You could make a switch in the index.php loading the storeview appropriate for the language.

10
  • Language yes, but the issue is not just the language but also the geographical location.
    – Karlson
    Jan 22, 2013 at 21:20
  • You could use the Maxmind geo API or any other free geo service to make the switch. I use maxmind because of their high accuracy (maxmind.com/en/geolocation_landing) Jan 22, 2013 at 21:24
  • What do you want to do if the user is currently in Russia, but his language is en-US? Location is often less informative (and accurate) than Accept-Language.
    – kojiro
    Jan 22, 2013 at 21:27
  • @kojiro i'd go for the http accept language first. you're right, location says little about some ones language Jan 22, 2013 at 21:29
  • 1
    Be careful, lone link is considered a poor answer since it is meaningless by itself and target resource is not guaranteed to be alive in the future. It would be preferable to include the essential parts of the answer here, and provide the link for reference.
    – j0k
    Jan 23, 2013 at 12:24
5

Another thing to look at is GEO IP.

Couple of lines of code will give you the users country code, which you can switch based on that.

But note that you will have issues using any redirection based code if you are using the same store with Enterprise Edition and Full Page Cache as the first page hit is cached.

4
  • Good point about caching. You'd have to figure out how to Vary: Accept-Language in the header for both the browser and the FPC.
    – kojiro
    Jan 22, 2013 at 21:29
  • Care to add some details about your last note regarding FPC? I'm intrigued, to say the least.
    – nevvermind
    Jan 22, 2013 at 22:11
  • I had an issue with using a module which uses GEO IP and redirects user to the correct store based on country code. This failed and after debugging, the observer was never called when the page was cached using FPC, infact only one observer was called, as FPC cached the full script running no magento code. Jan 22, 2013 at 22:14
  • I know another developer was building a script within the index.php as a work around, but not sure if this was successful or not, so can't comment YET!! Jan 22, 2013 at 22:17
4

Another option is to use PHP's auto_prepend configuration. This will cause all scripts handled by PHP to run the script before the actual requested script runs, but after .htaccess (if you're using that).

This approach combined with some of the other suggestions (e.g. using MaxMind for geo location data and store views for specific regions) can result in a rather clean approach.

If you're running a version of Magento >= 1.4, the default store code can be configured by setting the $_SERVER['MAGE_RUN_CODE'] value.

In either the .htaccess file or apache's configuration add the following:

php_value auto_prepend_file /var/www/mystore/path/to/script.php

In your script.php perform any necessary business logic and set the appropriate store code:

<?php
if (get_ip_location($_SERVER['REMOTE_ADDR']) == 'uk') {
    $_SERVER['MAGE_RUN_CODE'] = 'store_code_uk';
} else {
    $_SERVER['MAGE_RUN_CODE'] = 'store_code_us';
}

One thing to note is that this will apply for all PHP files requested, so you'll need to keep that in mind.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.