开发者

How get current CultureInfo in wrapper class?

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);  
}
开发者_JAVA技巧

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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜