[英]How can I get the client side date and currency formats?
I want to display dates and currency amounts in my users chosen format. 我想以用户选择的格式显示日期和货币金额。
How can I retrive this from the client machine? 如何从客户端计算机中检索?
Alternatively, is there some other way to format dates/currency correctly? 另外,还有其他方法可以正确格式化日期/货币吗?
Thanks 谢谢
A browser exposes it's locale: You can retrieve it by using ** Thread.CurrentThread.CurrentUICulture**
浏览器公开其语言环境:您可以使用** Thread.CurrentThread.CurrentUICulture**
检索它。
read more about this here and here 在这里和这里阅读有关此内容的更多信息
You can overwrite their Culture with a culture of your chosing (if the select another language from a menu perhaps) 您可以使用自己选择的文化来覆盖他们的文化(如果可以从菜单中选择其他语言)
to format the date time, and string read here 格式化日期时间,并在此处读取字符串
Watch out, setting a culture happens very soon in a page cycle, if you set it to late, the resource files will be those of a previous selected Culture. 请注意,设置区域性会在页面周期中很快发生,如果将其设置为较晚,资源文件将是先前选择的区域性文件。
To set the Culture early in the cycle you can create a basepage (that inherits ViewPage){} 要在周期的早期设置文化,您可以创建一个基页(继承ViewPage){}
using System.Globalization;
using System.Threading;
using System.Web.Mvc;
namespace NerdDinner.Views
{
public class NerdDinnerViewPage<T> : ViewPage<T> where T : class
{
protected override void InitializeCulture()
{
base.InitializeCulture();
Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentCulture.Clone() as CultureInfo;
if (Thread.CurrentThread.CurrentCulture != null)
{
Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator = ".";
Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator = ".";
}
}
}
}
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="NerdDinner.Views.NerdDinnerViewPage<NerdDinner.Models.DinnerFormViewModel>" %>
here is the full example, but I copied the code for easyness: example 这里是完整的例子,但我复制了easyness代码: 例如
another approach is using the global asax which can be found here 另一种方法是使用全局asax,可以在此处找到
In order to select a useful default for the .NET tools at your disposal (such as CultureInfo), my recommendation (in a web scenario) would be to parse the HTTP user agent string, as in the examples provided here . 为了选择在您的处置为.NET工具有用的默认值(如CultureInfo的),我的建议(以网络情况)将解析HTTP用户代理字符串,如提供的例子在这里 。 But in addition, the user should be able to override the default, and you ought to save the user's choice in a cookie and, if applicable, user options. 但是此外,用户应该能够覆盖默认值,并且您应该将用户的选择保存在cookie中,如果适用,还可以保存用户选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.