文化信息全球化DateTimeFormatInfo:我们如何将数字格式更改为我的语言?

发布于 2024-10-31 13:29:28 字数 2061 浏览 0 评论 0原文

我使用 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;

我的问题是:

  1. 为什么注释行在 VS2010 (.NET 4.0) 中出现错误,但在 VS2008 (.NET 3.5) 中正常?

  2. 当我使用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:

  1. Why do the commented lines have errors in VS2010 (.NET 4.0), but they're ok in VS2008 (.NET 3.5)?

  2. 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 技术交流群。

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

发布评论

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

评论(3

ヤ经典坏疍 2024-11-07 13:29:28

请看一下这个回答。另外那个可能是帮助。基本思想是创建一种扩展方法,用东部阿拉伯语对应的数字替换数字。顺便提一句。似乎数字和一些月份/日期名称都已经为 ps-AF 文化定义了:

        // gets Eastern Arabic digits array
        string[] numerals = CultureInfo.CreateSpecificCulture("ps-AF").NumberFormat.NativeDigits;

我只是不知道它是否适合您。

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:

        // gets Eastern Arabic digits array
        string[] numerals = CultureInfo.CreateSpecificCulture("ps-AF").NumberFormat.NativeDigits;

I just don't know if it is appropriate for you.

方圜几里 2024-11-07 13:29:28
  1. 您正在使用反射,它依赖于实现细节。这些细节很可能在 .NET 3.5 和 .NET 4 之间发生了变化。不要这样做。如果 API 设计者希望你搞乱这些东西,他们就会把它们公开。每当您依赖这样的实现细节时,您都应该预料到在更改实现时可能会出现损坏。

  2. 您是否尝试过克隆整个CultureInfo,并修改NumberFormatInfo以及DateTimeFormatInfo?我不能说我自己曾经这样做过,但值得一试。

  1. 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.

  2. Have you tried cloning the whole CultureInfo, and modifying the NumberFormatInfo as well as the DateTimeFormatInfo? I can't say I've ever done that myself, but it's worth a try.

甜心 2024-11-07 13:29:28

为什么注释行在 VS2010 (.NET 4.0) 中出错,但在 VS2008 (.NET 3.5) 中却正常?

事情发生了变化。事情发生了。生活还要继续。 ~ 伊丽莎白·斯科特

当我使用 DateTime 时,如何本地化我的数字。现在,我的意思是我想要我的语言的数字格式(仅适用于 DateTime)!是否可以?
尽管 C# 已经包含了大量的默认值,但您可以根据自己的喜好对它们进行微调。

这是有关如何设置日期时间信息的示例。

现在,如果您打印出 DateTime.Now

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", false);
Console.WriteLine(DateTime.Now.ToString("D", CultureInfo.CurrentCulture));
Thread.CurrentThread.CurrentCulture = new CultureInfo("et")
{
    DateTimeFormat = { LongDatePattern = "d. MMMM yyyy'. a.'" }
}; 
Console.WriteLine(DateTime.Now.ToString("D", CultureInfo.CurrentCulture));

您会得到:

Sunday, April 10, 2011
10. aprill 2011. a.

我想,这就是您想要的 - 只需通过更改文化即可获得不同的时间格式。

why the commented lines have error in VS2010 (.NET 4.0), but they're ok in VS2008 (.NET 3.5)?

Things change. Stuff happens. Life goes on. ~ Elizabeth Scott

How can i localization my digits when i use DateTime.Now, I mean I want digits format in my language(Just For DateTime)! is it possible?
Although C# already contains nice amount of default values, you can fine tune them for your liking.

Here is an example on how to set datetime info.

Now if you would print out DateTime.Now

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", false);
Console.WriteLine(DateTime.Now.ToString("D", CultureInfo.CurrentCulture));
Thread.CurrentThread.CurrentCulture = new CultureInfo("et")
{
    DateTimeFormat = { LongDatePattern = "d. MMMM yyyy'. a.'" }
}; 
Console.WriteLine(DateTime.Now.ToString("D", CultureInfo.CurrentCulture));

You would get:

Sunday, April 10, 2011
10. aprill 2011. a.

I assume, this is what you wanted - get different time format, just by changing culture.

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