7

我想将日历自定义为波斯语,但在将 CultureInfo 设置为这样之后:

CultureInfo fa = new CultureInfo("fa-IR",true);
Thread.CurrentThread.CurrentCulture = fa;

日历

月份和日期的名称没有改变,因此决定覆盖默认日历。

我怎样才能做到这一点?

4

3 回答 3

7

覆盖像我这样的初学者:

public class PersianCulture : CultureInfo
{

    private readonly System.Globalization.Calendar cal;

    private readonly System.Globalization.Calendar[] optionals;

    public PersianCulture()

        : this("fa-IR", true)
    {

    }
    public PersianCulture(string cultureName, bool useUserOverride)

        : base(cultureName, useUserOverride)
    {
        cal = base.OptionalCalendars[0];
        var optionalCalendars = new List<System.Globalization.Calendar>();
        optionalCalendars.AddRange(base.OptionalCalendars);
        optionalCalendars.Insert(0, new PersianCalendar());
        Type formatType = typeof(DateTimeFormatInfo);
        Type calendarType = typeof(System.Globalization.Calendar);
        PropertyInfo idProperty = calendarType.GetProperty("ID", BindingFlags.Instance | BindingFlags.NonPublic);
        FieldInfo optionalCalendarfield = formatType.GetField("optionalCalendars",BindingFlags.Instance | BindingFlags.NonPublic);
        var newOptionalCalendarIDs = new Int32[optionalCalendars.Count];

        for (int i = 0; i < newOptionalCalendarIDs.Length; i++)

            newOptionalCalendarIDs[i] = (Int32)idProperty.GetValue(optionalCalendars[i], null);
        optionalCalendarfield.SetValue(DateTimeFormat, newOptionalCalendarIDs);

        optionals = optionalCalendars.ToArray();

        cal = optionals[0];

        DateTimeFormat.Calendar = optionals[0];
        DateTimeFormat.MonthNames = new[] { "فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", "" };

        DateTimeFormat.MonthGenitiveNames = new[] { "فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", "" };

        DateTimeFormat.AbbreviatedMonthNames = new[] { "فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", "" };

        DateTimeFormat.AbbreviatedMonthGenitiveNames = new[] { "فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", "" };

        DateTimeFormat.AbbreviatedDayNames = new string[] { "ی", "د", "س", "چ", "پ", "ج", "ش" };

        DateTimeFormat.ShortestDayNames = new string[] { "ی", "د", "س", "چ", "پ", "ج", "ش" };

        DateTimeFormat.DayNames = new string[] { "یکشنبه", "دوشنبه", "ﺳﻪشنبه", "چهارشنبه", "پنجشنبه", "جمعه", "شنبه" };
        DateTimeFormat.AMDesignator = "ق.ظ";
        DateTimeFormat.PMDesignator = "ب.ظ";

    }
    public override System.Globalization.Calendar Calendar
    {

        get { return cal; }

    }
    public override System.Globalization.Calendar[] OptionalCalendars
    {

        get { return optionals; }

    }
}

然后在Page_Load

PersianCulture fa = new PersianCulture();

Thread.CurrentThread.CurrentCulture = fa;

在这里形成

于 2013-04-05T05:52:58.827 回答
4

您可以创建CultureInfo. 它的属性不是只读的,您可以分配给它..

于 2013-04-04T17:06:32.513 回答
2

您可以在呈现控件之前设置线程的 CultureInfo,并在呈现控件后恢复。

输出 :

在此处输入图像描述

下面的示例展示了如何使用不同的 CultureInfo 呈现两个不同的日历。

默认.aspx

<%@Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Import Namespace="System.Globalization" %>
<%@ Import Namespace="System.Threading" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <% Thread.CurrentThread.CurrentCulture = new CultureInfo("fa-IR");%>

        <asp:Calendar ID="Calendar1" runat="server">
        </asp:Calendar>
        <%
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
        %>
        <asp:Calendar ID="Calendar2" runat="server"></asp:Calendar>

    </div>
    </form>
</body>
</html>

默认.aspx.cs

using System;
using System.Threading;
using System.Globalization;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}
于 2013-04-04T17:36:00.673 回答