Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to derive custom culture from .NET CultureInfo class?
    primarykey
    data
    text
    <p>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:</p> <pre><code>/// &lt;summary&gt; /// Represents culture specific information, Persian calendar and number format info for Iran. /// &lt;/summary&gt; public class PersianCultureInfo : CultureInfo { private Calendar _calendar = new PersianCalendar(); public PersianCultureInfo() : base("fa-IR", true) { } /// &lt;summary&gt; /// Persian calendar with solar system algorithm for Iran. /// &lt;/summary&gt; 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; } } </code></pre> <p>The problem is that the DateTimeFormat property throws the following exception:</p> <pre><code>Not a valid calendar for the given culture. Parameter name: value </code></pre> <p>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:</p> <pre><code> public override Calendar[] OptionalCalendars { get { return base.OptionalCalendars.Concat&lt;Calendar&gt;(new Calendar[1] { new PersianCalendar() }).ToArray&lt;Calendar&gt;(); } } </code></pre> <p>But it didnt solved the problem. What is wrong? How can I set PersianCalendar as default calendar for CultureInfo and DateTimeFormat in correct way?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload