Sunday, August 05, 2012

Culture in .NET. All in one

By inspiration from article Hidden Gems inside .Net Classes I created static class that include short methods for quick retrieval object or properties of Cultures and Time Zones. All data based on build-in functionality of Framework 4.0


 public static class Culture
    {
        #region Consts
        private static readonly StringDictionary cultureDetails;
        private static readonly ReadOnlyCollection<TimeZoneInfo> timeZones = TimeZoneInfo.GetSystemTimeZones(); 
        #endregion
        
        /// <summary>
        /// Ctor
        /// </summary>
        static  Culture()
        {
            #region Init culture datails
            cultureDetails = new StringDictionary();
            foreach (CultureInfo cultureInfo in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
            {
                RegionInfo regionInfo = new RegionInfo(cultureInfo.Name);

                if (!cultureDetails.ContainsKey(regionInfo.EnglishName))
                {
                    cultureDetails.Add(regionInfo.EnglishName, regionInfo.Name);
                }
            } 
            #endregion
        }
        /// <summary>
        /// Get culture name by Country
        /// </summary>
        /// <param name="countryName"></param>
        /// <returns></returns>
        public static string GetCulture(string countryName)
        {
            return cultureDetails.ContainsKey(countryName) ? cultureDetails[countryName] : string.Empty;
        }
        /// <summary>
        /// Get Culture info by culture name
        /// </summary>
        /// <param name="cultureName"></param>
        /// <returns></returns>
        public static CultureInfo GetCultureInfo(string cultureName)
        {
            return CultureInfo.GetCultures(CultureTypes.SpecificCultures).FirstOrDefault(item => item.Name == cultureName);
        }
        /// <summary>
        /// Get month names by culture
        /// </summary>
        /// <param name="cultureName"></param>
        /// <returns></returns>
        public static string[] GetMonths(string cultureName)
        {
            var region = GetCultureInfo(cultureName);
            return region == null ? null : region.DateTimeFormat.MonthNames;
        }
        /// <summary>
        /// Get day names by culture
        /// </summary>
        /// <param name="cultureName"></param>
        /// <returns></returns>
        public static string[] GetDays(string cultureName)
        {
            var region = GetCultureInfo(cultureName);
            return region == null ? null : region.DateTimeFormat.DayNames;
        }
        /// <summary>
        /// Get first day of week by culture
        /// </summary>
        /// <param name="cultureName"></param>
        /// <returns></returns>
        public static DayOfWeek GetFirstDayOfWeek(string cultureName)
        {
            var region = GetCultureInfo(cultureName);
            return region == null ? default(DayOfWeek) : region.DateTimeFormat.FirstDayOfWeek;
        }
        /// <summary>
        /// Get datetime format by culture
        /// </summary>
        /// <param name="cultureName"></param>
        /// <returns></returns>
        public static string GetDateTimeFormat(string cultureName)
        {
            var region = GetCultureInfo(cultureName);
            return region == null ? string.Empty : region.DateTimeFormat.FullDateTimePattern;
        }
        /// <summary>
        /// Get TimeZone info by DisplayName
        /// </summary>
        /// <param name="displayName"></param>
        /// <returns></returns>
        public static TimeZoneInfo GetTimeZoneByDisplayName(string name)
        {
           return timeZones.FirstOrDefault(item => item.DisplayName == name);
        }
        /// <summary>
        /// Get TimeZone info by Standart Name
        /// </summary>
        /// <param name="displayName"></param>
        /// <returns></returns>
        public static TimeZoneInfo GetTimeZoneByStandartName(string name)
        {
            return timeZones.FirstOrDefault(item => item.StandardName == name);
        }
    }

No comments: