0

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?

Edwin de Koning
  • 14,209
  • 7
  • 56
  • 74
Darien Fawkes
  • 119
  • 1
  • 5
  • 16

2 Answers2

2

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.

Hadi Eskandari
  • 25,575
  • 8
  • 51
  • 65
  • When I try to set the Cultule like this: protected void USA_Click(object sender, ImageClickEventArgs e) { Session["MyCulture"] = CultureInfo.CreateSpecificCulture("en-AU"); Server.Transfer(Request.Url.LocalPath); CultureInfo Usa = new CultureInfo("en-AU"); System.Threading.Thread.CurrentThread.CurrentCulture = Usa; } it's dos not sets. – Darien Fawkes May 24 '11 at 07:36
  • You could try to set also System.Threading.Thread.CurrentThread.CurrentUiCulture (You have to set it at the start of every request). – Peter May 24 '11 at 08:13
  • I already try that, but it dos not help, in the Thread.CurrentThread.CurrentCulture stay only one culture. – Darien Fawkes May 24 '11 at 08:27
0

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.

Michael Christensen
  • 1,768
  • 1
  • 13
  • 11