Globalization of CultureInfo & DateTimeFormatInfo:

2019-08-12 12:32发布

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.

3条回答
在下西门庆
2楼-- · 2019-08-12 13:08

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.

查看更多
\"骚年 ilove
3楼-- · 2019-08-12 13:10

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.

查看更多
可以哭但决不认输i
4楼-- · 2019-08-12 13:32
  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.

查看更多
登录 后发表回答