文化信息全球化DateTimeFormatInfo:我们如何将数字格式更改为我的语言?
我使用 System.Globalization
如下:
CultureInfo calture = new CultureInfo(MyCultureInfoGoesHere);
DateTimeFormatInfo info = calture.DateTimeFormat;
info.AbbreviatedDayNames = new string[] { "bla", "bla", "bla", "bla", "bla", "bla", "bla" };
info.ShortestDayNames = new string[] { "bla", "bla", "bla", "bla", "bla", "bla", "bla" };
info.DayNames = new string[] { "bla", "bla", "bla", "bla", "bla", "bla", "bla" };
info.AbbreviatedMonthNames = new string[] { "bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla", "" };
info.MonthNames = new string[] { "bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla", "" };
info.AMDesignator = "bla";
info.PMDesignator = "bla";
info.ShortDatePattern = "yyyy/MM/dd";
//info.ShortDatePattern = "dd/MM/yyyy";
info.LongDatePattern = "dddd , dd MMMM yyyy";
info.FirstDayOfWeek = DayOfWeek.bla;
MyCalendar cal = new MyCalendar();
typeof(DateTimeFormatInfo).GetField("calendar", BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic).SetValue(info, cal);
//object obj = typeof(DateTimeFormatInfo).GetField("m_cultureTableRecord", BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic).GetValue(info);
//obj.GetType().GetMethod("UseCurrentCalendar", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(obj, new object[] { cal.GetType().GetProperty("ID", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(cal, null) });
typeof(System.Globalization.CultureInfo).GetField("calendar", BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic).SetValue(calture, cal);
Thread.CurrentThread.CurrentCulture = calture;
Thread.CurrentThread.CurrentUICulture = calture;
CultureInfo.CurrentCulture.DateTimeFormat = info;
CultureInfo.CurrentUICulture.DateTimeFormat = info;
我的问题是:
为什么注释行在 VS2010 (.NET 4.0) 中出现错误,但在 VS2008 (.NET 3.5) 中正常?
当我使用
DateTime.Now
时,如何本地化我的数字,我的意思是我想要我的语言的数字格式(仅适用于DateTime)!这可能吗?
预先致谢并致以最诚挚的问候。
I use System.Globalization
like below:
CultureInfo calture = new CultureInfo(MyCultureInfoGoesHere);
DateTimeFormatInfo info = calture.DateTimeFormat;
info.AbbreviatedDayNames = new string[] { "bla", "bla", "bla", "bla", "bla", "bla", "bla" };
info.ShortestDayNames = new string[] { "bla", "bla", "bla", "bla", "bla", "bla", "bla" };
info.DayNames = new string[] { "bla", "bla", "bla", "bla", "bla", "bla", "bla" };
info.AbbreviatedMonthNames = new string[] { "bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla", "" };
info.MonthNames = new string[] { "bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla", "" };
info.AMDesignator = "bla";
info.PMDesignator = "bla";
info.ShortDatePattern = "yyyy/MM/dd";
//info.ShortDatePattern = "dd/MM/yyyy";
info.LongDatePattern = "dddd , dd MMMM yyyy";
info.FirstDayOfWeek = DayOfWeek.bla;
MyCalendar cal = new MyCalendar();
typeof(DateTimeFormatInfo).GetField("calendar", BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic).SetValue(info, cal);
//object obj = typeof(DateTimeFormatInfo).GetField("m_cultureTableRecord", BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic).GetValue(info);
//obj.GetType().GetMethod("UseCurrentCalendar", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(obj, new object[] { cal.GetType().GetProperty("ID", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(cal, null) });
typeof(System.Globalization.CultureInfo).GetField("calendar", BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic).SetValue(calture, cal);
Thread.CurrentThread.CurrentCulture = calture;
Thread.CurrentThread.CurrentUICulture = calture;
CultureInfo.CurrentCulture.DateTimeFormat = info;
CultureInfo.CurrentUICulture.DateTimeFormat = info;
My questions are:
Why do the commented lines have errors in VS2010 (.NET 4.0), but they're ok in VS2008 (.NET 3.5)?
How can I localize my digits when I use
DateTime.Now
, I mean I want digits format in my language(Just For DateTime)! is it possible?
Thanks in advance and best regards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请看一下这个回答。另外那个可能是帮助。基本思想是创建一种扩展方法,用东部阿拉伯语对应的数字替换数字。顺便提一句。似乎数字和一些月份/日期名称都已经为 ps-AF 文化定义了:
我只是不知道它是否适合您。
Please take a look at this answer. Also that one could be of help. The basic idea is to create an extension method that will replace numbers with its Eastern Arabic counterparts. BTW. It seems that both numbers and some month / day names are already defined for the ps-AF culture:
I just don't know if it is appropriate for you.
您正在使用反射,它依赖于实现细节。这些细节很可能在 .NET 3.5 和 .NET 4 之间发生了变化。不要这样做。如果 API 设计者希望你搞乱这些东西,他们就会把它们公开。每当您依赖这样的实现细节时,您都应该预料到在更改实现时可能会出现损坏。
您是否尝试过克隆整个
CultureInfo
,并修改NumberFormatInfo
以及DateTimeFormatInfo
?我不能说我自己曾经这样做过,但值得一试。You're using reflection, which is relying on implementation details. Those details may well have changed between .NET 3.5 and .NET 4. Don't do that. If the API designers wanted you to mess with those things, they'd have made them public. Any time you rely on implementation details like this, you should expect potential breakage when changing implementation.
Have you tried cloning the whole
CultureInfo
, and modifying theNumberFormatInfo
as well as theDateTimeFormatInfo
? I can't say I've ever done that myself, but it's worth a try.事情发生了变化。事情发生了。生活还要继续。 ~ 伊丽莎白·斯科特
这是有关如何设置日期时间信息的示例。
现在,如果您打印出
DateTime.Now
您会得到:
我想,这就是您想要的 - 只需通过更改文化即可获得不同的时间格式。
Things change. Stuff happens. Life goes on. ~ Elizabeth Scott
Here is an example on how to set datetime info.
Now if you would print out
DateTime.Now
You would get:
I assume, this is what you wanted - get different time format, just by changing culture.