ASP.NET 全球化——显示日期

发布于 2024-07-09 07:06:30 字数 674 浏览 6 评论 0原文

早上好,

对于新手问题表示歉意。 我刚刚开始使用 ASP.NET 国际化设置。

背景信息:

我有一个显示 HTML 对象的网站。 在该

HTML 对象中,我有一列显示日期。 我的服务器位于美国,这些日期显示为 MM/DD/YYYY。 我的许多用户通过 Excel、数据 --> 插入此网页。 导入外部数据 --> 导入Web查询接口。 我的用户大部分都在美国,因此这些日期会正确显示在他们的 Excel 屏幕中。

现在我需要使该网页适用于英国用户。 事实上,他们下载的日期为 MM/DD/YYYY,这使得他们的电子表格无法使用,因为他们的区域设置被设置为 DD/MM/YYYY

我的问题是:

如何让 Web 服务器意识到传入请求具有 en-GB 区域性设置? 我可以设计自己的小自定义解决方法,但我确信我不是第一个遇到这种情况的程序员。 专业人士如何处理这个问题? 我正在寻找一个相对简单且快速安装的解决方案,但我不想只是将我自己的逻辑中的一些蹩脚的错误片段放在一起,从现在起我将害怕 6 个月。

预先非常感谢, -艾伦。

Good morning,

Apologies for the newbie question. I'm just getting started with ASP.NET internationalization settings.

Background info:

I have a website which displays a <table> HTML object. In that <table> HTML object, I have a column which displays dates. My server being in the US, those dates show up as MM/DD/YYYY. Many of my users plug into this webpage through Excel, via the Data --> Import External Data --> Import Web Query interface. My users, for the most part, are in the US, so those dates show up correctly in their Excel screens.

Now I need to make the webpage work for UK users. As is, they are downloading the dates as MM/DD/YYYY, which makes their spreadsheets unusable since their regional settings are set to DD/MM/YYYY.

My question is:

How do I make it so the web server realizes that the incoming request has a en-GB culture setting? I could engineer my own little custom workaround, but I'm sure I'm not the first programmer to come across this. How do the pro's handle this? I'm looking for a solution that would be relatively simple and quick to put up, but I don't want to just put some crappy buggy piece of my own logic togethe that I'm going to dread 6 months from now.

Thanks a lot in advance,
-Alan.

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

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

发布评论

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

评论(3

烟柳画桥 2024-07-16 07:06:30

几点:

  • <全球化> 元素还需要属性culture="auto"。 uiCulture 属性影响用于检索资源的语言。 区域性属性影响用于格式化数字和日期的区域性。

  • 正如这篇 MSDN 文章中所述,它不是最佳实践是完全依赖浏览器设置来确定页面的 UI 区域性。 用户经常使用不符合其偏好的浏览器(例如,在网吧中)。 您应该为用户提供一种方法来显式选择页面的语言或语言和文化(CultureInfo 名称)。

A couple of points:

  • The <globalization> element also needs the attribute culture="auto". The uiCulture attribute affects the language used to retrieve resources. The culture attribute affects the culture used for formatting numbers an dates.

  • As noted in this MSDN article, it is not a best practice to rely exclusively on browser settings to determine the UI culture for a page. Users frequently use browsers that are not set to their preferences (for example, in an Internet cafe). You should provide a method for users to explicitly choose a language or language and culture (CultureInfo name) for the page.

神经暖 2024-07-16 07:06:30

如果您愿意,您可以允许浏览器自动设置您的 UI 区域性,只需打开 web.config,如下所示:

<configuration>
   <system.web>    
       <globalization uiCulture="auto" />
       ...

然后浏览器设置的区域性将自动在您的应用程序中设置。 这意味着当您让框架显示日期/时间值时,它们将根据当前线程的 UI 文化进行格式化。

如果您使用货币和/或本地化文本,这也会有所帮助(但是您必须为您支持的每种文化提供本地化资源)。

You can allow the browser to set your UI culture automatically if you wish, by opening up the web.config, like this:

<configuration>
   <system.web>    
       <globalization uiCulture="auto" />
       ...

And then the culture set by the browser will be automatically set in your app. This means that when you have the framework display date/time values, they will be formatted according to the current thread's UI Culture.

This will also help if you are using currency and/or localized text (however you have to provide the localized resources for each culture you support).

帅气尐潴 2024-07-16 07:06:30

您还可以接受查询字符串参数来覆盖区域性设置。

文化初始化应该在 Page.InitializeCulture 方法中进行。

protected override void InitializeCulture ( )
{
  Thread.CurrentThread.CurrentCulture
    = Thread.CurrentThread.CurrentUICulture
    = Request.QueryString [ "culture" ] != null ? new CultureInfo ( Request.QueryString [ "culture" ] ) : CultureInfo.InvariantCulture;
  //base.InitializeCulture ( );
}

用法: http://tempuri.org/page.aspx?culture=en-GB< /a>

You could also accept a query string parameter for overriding the culture settings.

Culture initialization should go in the Page.InitializeCulture method.

protected override void InitializeCulture ( )
{
  Thread.CurrentThread.CurrentCulture
    = Thread.CurrentThread.CurrentUICulture
    = Request.QueryString [ "culture" ] != null ? new CultureInfo ( Request.QueryString [ "culture" ] ) : CultureInfo.InvariantCulture;
  //base.InitializeCulture ( );
}

Usage: http://tempuri.org/page.aspx?culture=en-GB

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