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')" />
<img src="Images/fr.png" onclick="langSelection('fr')"/>
<img src="Images/es.png" onclick="langSelection('es')"/>
<img src="Images/de.png" onclick="langSelection('de')"/>
<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