如何在包装类中获取当前的CultureInfo?

发布于 2024-11-09 04:42:52 字数 506 浏览 2 评论 0原文

我有 Main.Master 页面,其中包含设置 CultureInfo 并将其存储在会话中的按钮:

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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

掀纱窥君容 2024-11-16 04:42:52

使用文化和本地化信息时最好的做法是将其设置到它们所属的位置:在当前线程上。试试这个:

System.Threading.Thread.CurrentThread.CurrentCulture = yourCulture;

通过这种方式,您始终可以从运行请求的线程中获取当前区域性。您还可以考虑设置 CurrentUICulture 以及一些使用该值的本地化机制。

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.

吹泡泡o 2024-11-16 04:42:52

要遵循当前的方法,如果您只想访问不继承自 System.Web.UI.Page 的类中的会话变量,您可以像这样简单地访问它。

return (CultureInfo)HttpContext.Current.Session["MyCulture"];

我假设您添加了一个单独的类。

如果您有一个 Web 项目,您可以尝试将其集成到 Global.asax 中,并让您的包装类也包装 Global.asax 属性。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文