RegionExtensions is a set of methods that provide easy access to NCLDR data for regions
Exemplo n.º 1
0
        /// <summary>
        /// GetRegionInformation gets the RegionInformation for the CultureInfo
        /// </summary>
        /// <param name="cultureInfo">The CultureInfo to get the RegionInformation for</param>
        /// <returns>The RegionInformation for the CultureInfo</returns>
        public static RegionInformation GetRegionInformation(this CultureInfo cultureInfo)
        {
            AssertHasRegionInfo(cultureInfo);
            RegionInfo regionInfo = new RegionInfo(cultureInfo.Name);

            return(RegionExtensions.GetRegionInformation(regionInfo.TwoLetterISORegionName));
        }
Exemplo n.º 2
0
        /// <summary>
        /// GetPostcodeRegex gets the postal code regular expression for the CultureInfo
        /// </summary>
        /// <param name="cultureInfo">The CultureInfo to get the postal code regular expression for</param>
        /// <returns>The postal code regular expression for the CultureInfo</returns>
        public static string GetPostcodeRegex(this CultureInfo cultureInfo)
        {
            AssertHasRegionInfo(cultureInfo);
            RegionInfo regionInfo = new RegionInfo(cultureInfo.Name);

            return(RegionExtensions.GetPostcodeRegex(regionInfo.TwoLetterISORegionName));
        }
Exemplo n.º 3
0
        /// <summary>
        /// GetAvailableCalendars gets an array of .NET calendars available to the Culture
        /// </summary>
        /// <param name="culture">The Culture to get the Calendars for</param>
        /// <returns>An array of .NET calendars available to the Culture</returns>
        /// <remarks>The list is filtered to include only those .NET Calendars that can be used
        /// with custom cultures</remarks>
        private static System.Globalization.Calendar[] GetAvailableCalendars(Culture culture)
        {
            if (culture.Dates == null || culture.Dates.Calendars == null)
            {
                return(null);
            }

            List <System.Globalization.Calendar> availableCalendars = new List <System.Globalization.Calendar>();

            Types.Calendar[] allCalendars = null;
            if (culture.Identity.Region != null)
            {
                string[] calendarPreferenceIds = RegionExtensions.GetCalendarPreferenceIds(culture.Identity.Region.Id);
                if (calendarPreferenceIds != null)
                {
                    allCalendars = (from cpi in calendarPreferenceIds
                                    select(from c in culture.Dates.Calendars
                                           where c.Id == cpi
                                           select c).FirstOrDefault()).ToArray();
                }
            }

            if (allCalendars == null)
            {
                allCalendars = culture.Dates.Calendars;
            }

            foreach (Types.Calendar calendar in allCalendars)
            {
                System.Globalization.Calendar dotNetCalendar = GetCalendar(calendar);

                // The CultureAndRegionInfoBuilder.AvailableCalendars property does not allow
                // certain types of calendars to be included in the array assigned to it
                if (dotNetCalendar != null &&
                    dotNetCalendar.GetType() != typeof(PersianCalendar) &&
                    dotNetCalendar.GetType() != typeof(TaiwanLunisolarCalendar) &&
                    dotNetCalendar.GetType() != typeof(KoreanLunisolarCalendar) &&
                    dotNetCalendar.GetType() != typeof(JapaneseLunisolarCalendar) &&
                    dotNetCalendar.GetType() != typeof(ChineseLunisolarCalendar) &&
                    dotNetCalendar.GetType() != typeof(JulianCalendar))
                {
                    availableCalendars.Add(dotNetCalendar);
                }
            }

            return(availableCalendars.ToArray());
        }
Exemplo n.º 4
0
 /// <summary>
 /// GetDayOfWeek gets the first DayOfWeek for the RegionInfo
 /// </summary>
 /// <param name="regionInfo">The RegionInfo to get the first DayOfWeek for</param>
 /// <returns>The first DayOfWeek for the RegionInfo</returns>
 public static DayOfWeek GetDayOfWeek(this RegionInfo regionInfo)
 {
     return(RegionExtensions.GetFirstDayOfWeek(regionInfo.TwoLetterISORegionName));
 }
Exemplo n.º 5
0
 /// <summary>
 /// GetPaperSize gets the RegionPaperSize for the RegionInfo
 /// </summary>
 /// <param name="regionInfo">The RegionInfo to get the RegionPaperSize for</param>
 /// <returns>The RegionPaperSize for the RegionInfo</returns>
 public static RegionPaperSize GetPaperSize(this RegionInfo regionInfo)
 {
     return(RegionExtensions.GetPaperSize(regionInfo.TwoLetterISORegionName));
 }
Exemplo n.º 6
0
 /// <summary>
 /// GetMeasurementSystem gets the MeasurementSystem for the RegionInfo
 /// </summary>
 /// <param name="regionInfo">The RegionInfo to get the MeasurementSystem for</param>
 /// <returns>The MeasurementSystem for the RegionInfo</returns>
 public static RegionMeasurementSystem GetMeasurementSystem(this RegionInfo regionInfo)
 {
     return(RegionExtensions.GetMeasurementSystem(regionInfo.TwoLetterISORegionName));
 }
Exemplo n.º 7
0
 /// <summary>
 /// GetRegionInformation gets the RegionInformation for the RegionInfo
 /// </summary>
 /// <param name="regionInfo">The RegionInfo to get the RegionInformation for</param>
 /// <returns>The RegionInformation for the RegionInfo</returns>
 public static RegionInformation GetRegionInformation(this RegionInfo regionInfo)
 {
     return(RegionExtensions.GetRegionInformation(regionInfo.TwoLetterISORegionName));
 }
Exemplo n.º 8
0
 /// <summary>
 /// GetTelephoneCode gets the telephone code used by the RegionInfo
 /// </summary>
 /// <param name="regionInfo">The RegionInfo to get the telephone code for</param>
 /// <returns>The telephone code used by the RegionInfo</returns>
 /// <remarks>GetTelephoneCode gets only the first telephone code used by the region.
 /// There is only one telephone code when a region is a country/region. Only when a region
 /// is larger than a country/region (e.g. The World) will it have more than one telephone code.</remarks>
 public static string GetTelephoneCode(this RegionInfo regionInfo)
 {
     return(RegionExtensions.GetTelephoneCode(regionInfo.TwoLetterISORegionName));
 }
Exemplo n.º 9
0
 /// <summary>
 /// GetPostcodeRegex gets the postal code regular expression for the RegionInfo
 /// </summary>
 /// <param name="regionInfo">The RegionInfo to get the postal code regular expression for</param>
 /// <returns>The postal code regular expression for the RegionInfo</returns>
 public static string GetPostcodeRegex(this RegionInfo regionInfo)
 {
     return(RegionExtensions.GetPostcodeRegex(regionInfo.TwoLetterISORegionName));
 }
Exemplo n.º 10
0
 /// <summary>
 /// GetCalendarPreferences gets the calendar preference ids for the RegionInfo
 /// </summary>
 /// <param name="regionInfo">The RegionInfo to get the calendar preference ids for</param>
 /// <returns>The calendar preference ids for the RegionInfo</returns>
 public static string[] GetCalendarPreferenceIds(this RegionInfo regionInfo)
 {
     return(RegionExtensions.GetCalendarPreferenceIds(regionInfo.TwoLetterISORegionName));
 }
Exemplo n.º 11
0
 /// <summary>
 /// GetHour gets the RegionHour for the RegionInfo
 /// </summary>
 /// <param name="regionInfo">The RegionInfo to get the RegionHour for</param>
 /// <returns>The RegionHour for the RegionInfo</returns>
 public static RegionHour GetHour(this RegionInfo regionInfo)
 {
     return(RegionExtensions.GetHour(regionInfo.TwoLetterISORegionName));
 }
Exemplo n.º 12
0
        /// <summary>
        /// CreateDateTimeFormatInfo creates a .NET DateTimeFormatInfo for the CLDR Culture
        /// </summary>
        /// <param name="culture">The Culture to get the DateTimeFormatInfo for</param>
        /// <returns>A .NET DateTimeFormatInfo for the CLDR Culture</returns>
        private static DateTimeFormatInfo CreateDateTimeFormatInfo(Culture culture)
        {
            System.Globalization.Calendar calendar           = new GregorianCalendar(GregorianCalendarTypes.Localized);
            DateTimeFormatInfo            dateTimeFormatInfo = new DateTimeFormatInfo();

            dateTimeFormatInfo.Calendar = calendar;

            if (culture.Dates != null && culture.Dates.Calendars != null)
            {
                Types.Calendar gregorianCalendar = (from c in culture.Dates.Calendars
                                                    where string.Compare(c.Id, "gregorian", StringComparison.InvariantCulture) == 0
                                                    select c).FirstOrDefault();
                if (gregorianCalendar != null)
                {
                    if (gregorianCalendar.AbbreviatedDayNames != null && gregorianCalendar.AbbreviatedDayNames.GetLength(0) == 7)
                    {
                        dateTimeFormatInfo.AbbreviatedDayNames = gregorianCalendar.AbbreviatedDayNames;
                    }

                    if (gregorianCalendar.DayNames != null && gregorianCalendar.DayNames.GetLength(0) == 7)
                    {
                        dateTimeFormatInfo.DayNames = gregorianCalendar.DayNames;
                    }

                    if (gregorianCalendar.ShortestDayNames != null && gregorianCalendar.ShortestDayNames.GetLength(0) == 7)
                    {
                        dateTimeFormatInfo.ShortestDayNames = gregorianCalendar.ShortestDayNames;
                    }

                    if (gregorianCalendar.AbbreviatedMonthNames != null)
                    {
                        dateTimeFormatInfo.AbbreviatedMonthNames = gregorianCalendar.AbbreviatedMonthNames;
                    }

                    if (gregorianCalendar.AbbreviatedMonthGenitiveNames != null)
                    {
                        dateTimeFormatInfo.AbbreviatedMonthGenitiveNames = gregorianCalendar.AbbreviatedMonthGenitiveNames;
                    }

                    if (gregorianCalendar.MonthNames != null)
                    {
                        dateTimeFormatInfo.MonthNames = gregorianCalendar.MonthNames;
                    }

                    if (gregorianCalendar.MonthGenitiveNames != null)
                    {
                        dateTimeFormatInfo.MonthGenitiveNames = gregorianCalendar.MonthGenitiveNames;
                    }

                    if (!string.IsNullOrEmpty(gregorianCalendar.AMDesignator))
                    {
                        // AMDesignator must be 14 characters or less
                        string amDesignator = gregorianCalendar.AMDesignator;
                        dateTimeFormatInfo.AMDesignator = amDesignator.Substring(0, Math.Min(amDesignator.Length, 14));
                    }

                    if (!string.IsNullOrEmpty(gregorianCalendar.PMDesignator))
                    {
                        // PMDesignator must be 14 characters or less
                        string pmDesignator = gregorianCalendar.PMDesignator;
                        dateTimeFormatInfo.PMDesignator = pmDesignator.Substring(0, Math.Min(pmDesignator.Length, 14));
                    }

                    dateTimeFormatInfo.CalendarWeekRule = CalendarWeekRule.FirstDay;

                    if (culture.Identity.Region != null)
                    {
                        dateTimeFormatInfo.FirstDayOfWeek = RegionExtensions.GetFirstDayOfWeek(culture.Identity.Region.Id);
                    }
                    else
                    {
                        dateTimeFormatInfo.FirstDayOfWeek = RegionExtensions.GetFirstDayOfWeek(NCldr.RegionIdForTheWorld);
                    }

                    if (!string.IsNullOrEmpty(gregorianCalendar.FullDotNetDatePattern))
                    {
                        // dateTimeFormatInfo.FullDateTimePattern = "dd MMMM yyyy HH:mm:ss";
                        dateTimeFormatInfo.FullDateTimePattern = gregorianCalendar.FullDotNetDatePattern;
                    }

                    if (!string.IsNullOrEmpty(gregorianCalendar.LongDotNetDatePattern))
                    {
                        // dateTimeFormatInfo.LongDatePattern = "dd MMMM yyyy";
                        // dateTimeFormatInfo.MonthDayPattern = "dd MMMM";
                        // dateTimeFormatInfo.YearMonthPattern = "MMMM, yyyy";
                        dateTimeFormatInfo.LongDatePattern = gregorianCalendar.LongDotNetDatePattern;

                        // CLDR does not have a month day pattern so make one by stripping out the year from the long date format
                        dateTimeFormatInfo.MonthDayPattern =
                            dateTimeFormatInfo.LongDatePattern.Replace("y", string.Empty);

                        // CLDR does not have a year month pattern so make one by stripping out the day from the long date format
                        dateTimeFormatInfo.YearMonthPattern =
                            dateTimeFormatInfo.LongDatePattern.Replace("d", string.Empty);
                    }

                    if (!string.IsNullOrEmpty(gregorianCalendar.ShortDotNetDatePattern))
                    {
                        // dateTimeFormatInfo.ShortDatePattern = "dd-MM-yyyy";
                        dateTimeFormatInfo.ShortDatePattern = gregorianCalendar.ShortDotNetDatePattern;
                    }

                    if (!string.IsNullOrEmpty(gregorianCalendar.LongDotNetTimePattern))
                    {
                        // dateTimeFormatInfo.LongTimePattern = "HH:mm:ss";
                        dateTimeFormatInfo.LongTimePattern = gregorianCalendar.LongDotNetTimePattern;
                    }

                    if (!string.IsNullOrEmpty(gregorianCalendar.ShortDotNetTimePattern))
                    {
                        // dateTimeFormatInfo.ShortTimePattern = "HH:mm";
                        dateTimeFormatInfo.ShortTimePattern = gregorianCalendar.ShortDotNetTimePattern;
                    }

                    dateTimeFormatInfo.DateSeparator = gregorianCalendar.DateSeparator;
                    dateTimeFormatInfo.TimeSeparator = gregorianCalendar.TimeSeparator;
                }
            }

            return(dateTimeFormatInfo);
        }
Exemplo n.º 13
0
        /// <summary>
        /// Creates a CultureAndRegionInfoBuilder object for the NCLDR culture
        /// </summary>
        /// <param name="cldrCultureName">The name of the CLDR culture from which to create the .NET culture</param>
        /// <param name="dotNetCultureName">The name of the .NET custom culture to create</param>
        /// <returns>A CultureAndRegionInfoBuilder object for the NCLDR culture</returns>
        public static CultureAndRegionInfoBuilder CreateNCldrCultureAndRegionInfoBuilder(string cldrCultureName, string dotNetCultureName)
        {
            Culture culture = Culture.GetCulture(cldrCultureName);

            CultureAndRegionModifiers cultureAndRegionModifiers =
                culture.IsNeutralCulture ? CultureAndRegionModifiers.Neutral : CultureAndRegionModifiers.None;

            CultureAndRegionInfoBuilder builder = new CultureAndRegionInfoBuilder(dotNetCultureName, cultureAndRegionModifiers);

            CultureInfo parentCultureInfo = GetParentCultureInfo(culture);

            builder.Parent = parentCultureInfo;

            if (string.IsNullOrEmpty(culture.EnglishName))
            {
                builder.CultureEnglishName = culture.Identity.CultureName;
            }
            else
            {
                builder.CultureEnglishName = culture.EnglishName;
            }

            if (string.IsNullOrEmpty(culture.NativeName) && string.IsNullOrEmpty(culture.EnglishName))
            {
                builder.CultureNativeName = culture.Identity.CultureName;
            }
            else if (string.IsNullOrEmpty(culture.NativeName))
            {
                builder.CultureNativeName = culture.EnglishName;
            }
            else
            {
                builder.CultureNativeName = culture.NativeName;
            }

            builder.TwoLetterISOLanguageName = culture.Identity.Language.Id;

            // CLDR does not have data for ThreeLetterISOLanguageName but one must be assigned for the culture to be valid
            builder.ThreeLetterISOLanguageName = culture.Identity.Language.Id;

            // CLDR does not have data for ThreeLetterWindowsLanguageName but one must be assigned for the culture to be valid
            builder.ThreeLetterWindowsLanguageName = culture.Identity.Language.Id;

            // The CultureAndRegionInfoBuilder requires dummy values for these properties
            // even though there is no region or currency values
            builder.RegionEnglishName            = "xxx";
            builder.RegionNativeName             = "xxx";
            builder.ThreeLetterISORegionName     = "xxx";
            builder.ThreeLetterWindowsRegionName = "xxx";
            builder.TwoLetterISORegionName       = "xx";
            builder.ISOCurrencySymbol            = "xx";
            builder.CurrencyEnglishName          = "xxx";
            builder.CurrencyNativeName           = "xxx";

            if (culture.Identity.Region != null)
            {
                Region region = culture.Identity.Region;
                builder.RegionEnglishName = region.EnglishName;

                string regionNativeName = region.DisplayName(culture.Identity.Language.Id);
                if (string.IsNullOrEmpty(regionNativeName))
                {
                    builder.RegionNativeName = region.EnglishName;
                }
                else
                {
                    builder.RegionNativeName = regionNativeName;
                }

                RegionCode regionCode = RegionExtensions.GetRegionCode(region.Id);
                if (regionCode != null && regionCode.Alpha3 != null)
                {
                    builder.ThreeLetterISORegionName = regionCode.Alpha3;
                }
                else
                {
                    builder.ThreeLetterISORegionName = region.Id;
                }

                // CLDR does not have data for ThreeLetterWindowsRegionName but one must be assigned for the culture to be valid
                builder.ThreeLetterWindowsRegionName = region.Id;
                builder.TwoLetterISORegionName       = region.Id;

                builder.IsMetric = RegionExtensions.GetMeasurementSystem(region.Id).IsMetric;
            }
            else
            {
                builder.IsMetric = RegionExtensions.GetMeasurementSystem(NCldr.RegionIdForTheWorld).IsMetric;
            }

            // CLDR does not have data for KeyboardLayoutId or GeoId
            builder.IetfLanguageTag = cldrCultureName;

            builder.GregorianDateTimeFormat = CreateDateTimeFormatInfo(culture);
            builder.AvailableCalendars      = GetAvailableCalendars(culture);

            builder.NumberFormat = CreateNumberFormatInfo(culture);

            if (culture.Numbers != null &&
                culture.Numbers.CurrencyDisplayNameSets != null &&
                culture.Numbers.CurrentCurrencyPeriod != null)
            {
                string currencyName = culture.Numbers.CurrentCurrencyPeriod.Id;
                if (!string.IsNullOrEmpty(currencyName))
                {
                    builder.ISOCurrencySymbol = currencyName;

                    builder.CurrencyEnglishName = CultureExtensions.GetCurrencyDisplayName(currencyName, "en");

                    string currencyNativeName = CultureExtensions.GetCurrencyDisplayName(currencyName, culture.Identity.Language.Id);
                    builder.CurrencyNativeName = currencyNativeName == null ? builder.CurrencyEnglishName : currencyNativeName;
                }
            }

            builder.TextInfo    = parentCultureInfo.TextInfo;
            builder.CompareInfo = parentCultureInfo.CompareInfo;

            return(builder);
        }