Kentico 7 zone location.

Jorge López asked on April 16, 2014 01:58

Hello, there is an issue i cant fix.

Right now when a german user displays my page, he gets by default the spanish version. I would like to do an automatically relocation and place him on the right language version of the website depending of the origin country but i don't know how to do it.

Thank you.

Recent Answers


Vilém Jeniš answered on April 17, 2014 02:50

Hi Jorge!

The language version (culture) of the site that is served to a visitor is determined by several factors. The first one is cookies. Cookie 'CMSPreferredCulture' is the one, that determines what language is served. If no cookie is available, User's prefered culture (configured in the administration UI) is served. If no user is logged in, browser settings are taken into account. FireFox has these stored in "%appdata%\Roaming\Mozilla\Firefox\Profiles<somejibberish>.default\prefs.js" and the line goes like this: user_pref("intl.accept_languages", "en-us"); The setting is also available via UI:

Image Text

So verify your geman visitor has no cookies 'CMSPreferredCulture', is not logged in (easier), has browser configured to accept german language only, your site has the german culture associated. (CMSSiteManager > Sites > Edit > Cultures) and also that the document served by default is available in german version.

0 votesVote for this answer Mark as a Correct answer

Martin Danko answered on April 17, 2014 02:54

Hello Jorge,

I've been solving a similar issue with one of our customers in the past and here is the solution that worked:

  1. You will need to use the Global events:

  2. You should be interested in: CMSSessionEvents -> Start

And code example:

string ipAddress = HttpContext.Current.Request.UserHostAddress;
string currentCountryName = IP2CountryHelper.GetCountryByIp(ipAddress);

switch (currentCountryName)
{

case "INDIA":
CMSContext.CurrentUser.PreferredCultureCode = "hi-IN";
break;
case "UNITED KINGDOM":
CMSContext.CurrentUser.PreferredCultureCode = "en-GB";
break;
default:
CMSContext.CurrentUser.PreferredCultureCode= "en-US";
break;
} 

Best regards, Martin

1 votesVote for this answer Mark as a Correct answer

Jorge López answered on April 21, 2014 04:06

Hello Martin.

This is what i need!

But i dont know where i need to put this code, i didnt find any CMSSessionEvents options in the CMS. I tried to edit the CMSSessionEvents.cs file in App_Code/Handlers, but i get a lot of errors like:

CS1519: Invalid token 'switch' in class, struct, or interface member declaration

and if i comment the switch i get this error:

CS0103: The name 'IP2CountryHelper' does not exist in the current context

For this second error i suppose i need to add a using at the top, but i don't know what i need to add

This is my CMSSessionEvents.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using CMS.SettingsProvider;

/// <summary>
/// Global CMS session events
/// </summary>
public class CMSSessionEvents
{
    /// <summary>
    /// Fires upon the application start
    /// </summary>
    public static CMSHandler Start = new CMSHandler();

    string ipAddress = HttpContext.Current.Request.UserHostAddress;
    string currentCountryName = IP2CountryHelper.GetCountryByIp(ipAddress);

        switch (currentCountryName)
        {
            case "SPAIN":
            CMSContext.CurrentUser.PreferredCultureCode = "es-es";
            break;
            case "FRANCE":
            CMSContext.CurrentUser.PreferredCultureCode = "fr-FR";
            break;
            case "BELGIUM":
            CMSContext.CurrentUser.PreferredCultureCode = "fr-FR";
            break;
            case "CANADA":
            CMSContext.CurrentUser.PreferredCultureCode = "fr-FR";
            break;
            case "GERMANY":
            CMSContext.CurrentUser.PreferredCultureCode = "de-DE";
            break;
            case "RUSSIAN FEDERATION":
            CMSContext.CurrentUser.PreferredCultureCode = "ru-RU";
            break;
            case "UNITED KINGDOM":
            CMSContext.CurrentUser.PreferredCultureCode = "en-GB";
            break;
            case "UNITED STATES":
            CMSContext.CurrentUser.PreferredCultureCode = "en-us";
            break;
            default:
            CMSContext.CurrentUser.PreferredCultureCode= "en-GB";
            break;
        }


    /// <summary>
    /// Fires upon the application end
    /// </summary>
    public static CMSHandler End = new CMSHandler();
}

Thank you so much for your help!

0 votesVote for this answer Mark as a Correct answer

Yehuda Lando answered on April 21, 2014 06:15

You can find more information on how to register event handlers here

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.