0

I am building a Multi-Lingual site in webforms aspx project, and I am thinking of the best solution to do this.

I have some images on the MasterPage, and once clicked, I am calling a Jquery method, that should redirect to a web method.

In turn I have a base page that is initializing the Culture, and all the pages, except for the MasterPage are inheriting from it.

So far I have the following:-

HTML:-

<div class="LocalizationFlags">
<img src="Images/gb.png" onclick="langSelection('gb')" /> &nbsp; &nbsp; 
<img src="Images/fr.png" onclick="langSelection('fr')"/> &nbsp; &nbsp; 
<img src="Images/es.png" onclick="langSelection('es')"/> &nbsp; &nbsp; 
<img src="Images/de.png" onclick="langSelection('de')"/> &nbsp; &nbsp; 
<img src="Images/it.png" onclick="langSelection('it')"/>
</div>

JQuery :-

    function langSelection(lang) {
    setSession(lang);
};

function setSession(Lang) {
    var args = {
        lang: Lang
    };
    $.ajax({
        type: "POST",
        url: "Site.Master.aspx/SetUserCulture",
        data: JSON.stringify(args),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function () {
            alert('Success.');
        },
        error: function () {
            alert("Fail");
        }
    });
};

Site.Master.cs

    [WebMethod(EnableSession = true)]
    private void SetUserCulture(string lang)
    {
        HttpContext.Current.Session["CurrentUI"] = lang;
    }

BasePage.cs

    protected override void InitializeCulture()
    {
        if (Session["CurrentUI"] != null)
        {
            String selectedLanguage = (string)Session["CurrentUI"];
            UICulture = selectedLanguage;
            Culture = selectedLanguage;

            Thread.CurrentThread.CurrentCulture =
                CultureInfo.CreateSpecificCulture(selectedLanguage);
            Thread.CurrentThread.CurrentUICulture = new
                CultureInfo(selectedLanguage);
        }

        base.InitializeCulture();
    }

Now there are several problems with what I have at the moment. Jquery does not work, throws a "Fail", and also I know that I cannot use the Site.Master.cs to put the webmethod in.

Is it just a case of creating a WCF service for this method, and then calling it from the Jquery code?

Am I on the right track here?

Thanks for your help and time

JMon
  • 3,387
  • 16
  • 63
  • 102

1 Answers1

0

Your ajax request most likely fails because it does not reach the destination - as you said you cannot have WebMethod in the master's codebehind. Also there is no need to serialize your parameters with JSON, plain object should work just as well. Besides, dataType: json is not needed here - you do not return any data for this request.

There is no need for a web service here - a simple HTTP handler will suffice. Just do not forget to implement marker interface IRequiresSessionState to have an access to the session inside the handler.

In general cookies might be a better place to store user's culture, since you might want to retain this information across different visits of the same user. Another option is also user profile in the DB - in case you are dealing with registered users. However your current implementation that uses session should work.

Andrei
  • 55,890
  • 9
  • 87
  • 108
  • Hi Andrei, thanks for the explanation. Could you give me an example of the HTTP handler you were referring to? – JMon Mar 27 '13 at 14:12
  • @Johann, here is the link to [MSDN page](http://msdn.microsoft.com/en-us/library/system.web.ihttphandler.aspx). In two words it is very straightforward - you implement an interface with one method, register in in web.config, and then call it from other parts of the app. – Andrei Mar 27 '13 at 14:19
  • Hi Andrei, so I created a class CultureHandler that implements IHTTPHandler and IRequiresSessionState, with one method SetUserCulture(string lang). How do you register it in the web.config and then use it in code? – JMon Mar 27 '13 at 14:39
  • @Johann, here is the [walkthough](http://msdn.microsoft.com/en-us/library/46c5ddfy%28v=vs.71%29.aspx) for registration routine. Make sure that `SetUserCulture` is call ed inside `ProcessRequest` method - this is the one that will be called by ASP.NET. After registration the handler will be available for you at the URL you have specified - use it in your ajax call. – Andrei Mar 27 '13 at 15:14