如何从 .NET CultureInfo 类派生自定义文化?
我想将我的应用程序文化设置为我想要的任何内容,无论操作系统文化是什么。为了获得此信息,我使用了 CultureInfo 类,其中“fa-IR”作为区域性,但它使用“GregorianCalendar”作为默认日历,而不是 .NET PersianCalendar 类。因此,我尝试从 CultureInfo 派生一个新类来实现我的客户文化:
/// <summary>
/// Represents culture specific information, Persian calendar and number format info for Iran.
/// </summary>
public class PersianCultureInfo : CultureInfo
{
private Calendar _calendar = new PersianCalendar();
public PersianCultureInfo()
: base("fa-IR", true)
{
}
/// <summary>
/// Persian calendar with solar system algorithm for Iran.
/// </summary>
public override Calendar Calendar
{
get
{
return this._calendar;
}
}
public static PersianCultureInfo Create()
{
PersianCultureInfo culture = new PersianCultureInfo();
culture.PerpareDateTimeFormatInfo();
return culture;
}
private void PerpareDateTimeFormatInfo()
{
this.DateTimeFormat.Calendar = this.Calendar;
this.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Saturday;
}
}
问题是 DateTimeFormat 属性引发以下异常:
Not a valid calendar for the given culture.
Parameter name: value
所以我尝试重写OptionalCalendars 属性以将 PersianCalendar 添加到其中,因为默认情况下列表仅包含GregorianCalendar 和 HijriCalendar:
public override Calendar[] OptionalCalendars
{
get
{
return base.OptionalCalendars.Concat<Calendar>(new Calendar[1] { new PersianCalendar() }).ToArray<Calendar>();
}
}
但它并没有解决问题。怎么了?如何以正确的方式将 PersianCalendar 设置为 CultureInfo 和 DateTimeFormat 的默认日历?
I want to set my application culture to whatever I want, regardless of what the OS culture is. To obtain this I used CultureInfo class with "fa-IR" as culture, but it used the "GregorianCalendar" as default calendar and not the .NET PersianCalendar class. So I tried to derive a new class from CultureInfo to implement my customer culture:
/// <summary>
/// Represents culture specific information, Persian calendar and number format info for Iran.
/// </summary>
public class PersianCultureInfo : CultureInfo
{
private Calendar _calendar = new PersianCalendar();
public PersianCultureInfo()
: base("fa-IR", true)
{
}
/// <summary>
/// Persian calendar with solar system algorithm for Iran.
/// </summary>
public override Calendar Calendar
{
get
{
return this._calendar;
}
}
public static PersianCultureInfo Create()
{
PersianCultureInfo culture = new PersianCultureInfo();
culture.PerpareDateTimeFormatInfo();
return culture;
}
private void PerpareDateTimeFormatInfo()
{
this.DateTimeFormat.Calendar = this.Calendar;
this.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Saturday;
}
}
The problem is that the DateTimeFormat property throws the following exception:
Not a valid calendar for the given culture.
Parameter name: value
So I tried to override the OptionalCalendars property to add the PersianCalendar to them, because by default the list only contains the GregorianCalendar and HijriCalendar:
public override Calendar[] OptionalCalendars
{
get
{
return base.OptionalCalendars.Concat<Calendar>(new Calendar[1] { new PersianCalendar() }).ToArray<Calendar>();
}
}
But it didnt solved the problem. What is wrong? How can I set PersianCalendar as default calendar for CultureInfo and DateTimeFormat in correct way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用 CultureAndRegionInfoBuilder 。根据 MSDN,这是 创建自定义的首选方式文化。
编辑:
如果失败,我猜使用反射的肮脏解决方法是创建的唯一方法这样的风俗文化。
Try using a CultureAndRegionInfoBuilder. According to the MSDN this is the preferred way of creating custom cultures.
EDIT:
If this fails I guess that a dirty workaround using reflection is the only way to create such a custom culture.
五年过去了,事情发生了变化。 Microsoft 有开源 .NET,现在,它支持开箱即用的
PersianCalendar
。我已经在 .NET Core 1.0 中尝试了以下代码片段,它工作得很好:
Five years have passed and things have changed since then. Microsoft has open sourced .NET, and now, it supports
PersianCalendar
out of the box.I have tried the following snippet with .NET Core 1.0 and it works just fine:
不幸的是这是不可能的。它已作为错误提交,因此 Microsoft 已意识到此问题。请参阅 http://connect.microsoft.com/VisualStudio/feedback/details/ 507262/波斯文化信息
Unfortunately it is not possible. It has been filed as a bug so Microsoft is aware of this issue. See http://connect.microsoft.com/VisualStudio/feedback/details/507262/persian-cultureinfo