如何从 .NET CultureInfo 类派生自定义文化?

发布于 2024-10-17 08:17:33 字数 1703 浏览 3 评论 0原文

我想将我的应用程序文化设置为我想要的任何内容,无论操作系统文化是什么。为了获得此信息,我使用了 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 技术交流群。

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

发布评论

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

评论(3

说谎友 2024-10-24 08:17:33

尝试使用 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.

彡翼 2024-10-24 08:17:33

五年过去了,事情发生了变化。 Microsoft 有开源 .NET,现在,它支持开箱即用的 PersianCalendar

我已经在 .NET Core 1.0 中尝试了以下代码片段,它工作得很好:

using System;
using System.Globalization;

namespace ConsoleApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            System.Globalization.CultureInfo.CurrentCulture = 
                new CultureInfo("fa-IR");
            Console.WriteLine(System.DateTime.Now); 
            // Prints: 1395/4/19 A.D. 13:31:55  
        }
    }
}

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:

using System;
using System.Globalization;

namespace ConsoleApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            System.Globalization.CultureInfo.CurrentCulture = 
                new CultureInfo("fa-IR");
            Console.WriteLine(System.DateTime.Now); 
            // Prints: 1395/4/19 A.D. 13:31:55  
        }
    }
}
故乡的云 2024-10-24 08:17:33

不幸的是这是不可能的。它已作为错误提交,因此 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

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