Reputation: 119
I have Main.Master page with buttons that set CultureInfo and store it in session:
protected void RU_Click(object sender, ImageClickEventArgs e)
{
Session["MyCulture"] = CultureInfo.CreateSpecificCulture("ru-Ru");
Server.Transfer(Request.Url.LocalPath);
}
protected void USA_Click(object sender, ImageClickEventArgs e)
{
Session["MyCulture"] = CultureInfo.CreateSpecificCulture("en-AU");
Server.Transfer(Request.Url.LocalPath);
}
I write I non page behind wrapper class and in this class I need to get this Culture from session. How can I get it?
Upvotes: 0
Views: 1538
Reputation: 1786
To follow your current approach if you just want to access a session variable in a class that doesnt inherit from System.Web.UI.Page you can simply access it like this.
return (CultureInfo)HttpContext.Current.Session["MyCulture"];
I'm making the assumption that you added a seperate class.
If you have a web project you could try to integrate this in your Global.asax and have your wrapper class wrap the Global.asax properties too.
Upvotes: 0
Reputation: 26354
The best thing to do when working with Culture and localization information is to set it to where they belong: on the current thread. Try this:
System.Threading.Thread.CurrentThread.CurrentCulture = yourCulture;
This way you can always get the current culture from the thread your request is running on. You may also consider setting CurrentUICulture as well as some of the localization mechanism use that value.
Upvotes: 2