public ReadOnlyCollection <CldrDateTimeFormat> GetAvailableDateTimeFormats() { if (this.dateTimeFormats == null) { ReadOnlyCollection <CldrDateTimeFormat> datePatterns = GetAvailableDateFormats(); ReadOnlyCollection <CldrDateTimeFormat> timePatterns = Resolve(locale, "gregory").GetAvailableTimeFormats(); Dictionary <string, string> combinePatterns = Resolve(locale, "generic").GetCombinePatterns(); List <CldrDateTimeFormat> dateTimeFormats = new List <CldrDateTimeFormat>(); foreach (CldrDateTimeFormat dateFormat in datePatterns) { string combinePattern; if (dateFormat.Styles.Weekday == WeekdayStyle.Long) { combinePattern = combinePatterns["full"]; } else { switch (dateFormat.Styles.Month) { case MonthStyle.Long: combinePattern = combinePatterns["full"]; break; case MonthStyle.Short: combinePattern = combinePatterns["long"]; break; case MonthStyle.Narrow: combinePattern = combinePatterns["medium"]; break; default: combinePattern = combinePatterns["short"]; break; } } foreach (CldrDateTimeFormat timeFormat in timePatterns) { List <string> keys = new List <string>(dateFormat.PatternId); keys.AddRange(timeFormat.PatternId); DateTimePartStyles combinedStyle = new DateTimePartStyles(dateFormat.Styles, timeFormat.Styles); CldrDateTimeFormat combinedFormat = new CldrDateTimeFormat(this, combinedStyle, String.Format(combinePattern, timeFormat.Pattern, dateFormat.Pattern), keys); dateTimeFormats.Add(combinedFormat); } } this.dateTimeFormats = dateTimeFormats.AsReadOnly(); } return(this.dateTimeFormats); }
public ReadOnlyCollection <CldrDateTimeFormat> GetAvailableTimeFormats() { if (this.timeFormats == null) { XElement timeFormats = root.XPathSelectElement("timeFormats"); if (CldrUtility.IsAlias(timeFormats, out string calendar)) { this.timeFormats = Resolve(locale, calendar).GetAvailableTimeFormats(); } else { Dictionary <string, XElement> patterns = new Dictionary <string, XElement>(); foreach (XElement timeFormatLength in timeFormats.Elements("timeFormatLength")) { patterns[timeFormatLength.Attribute("type").Value] = timeFormatLength.XPathSelectElement("timeFormat/pattern"); } foreach (XElement dateFormatItem in GetAvailableFormats()) { string id = dateFormatItem.Attribute("id").Value; if (id.IndexOfAny(requiredTimeComponents) >= 0 && id.IndexOfAny(unsupportedTimeComponents) < 0) { patterns[id] = dateFormatItem; } } Dictionary <DateTimePartStyles, CldrDateTimeFormat> formats = new Dictionary <DateTimePartStyles, CldrDateTimeFormat>(); Dictionary <DateTimePartStyles, List <string> > patternKeys = new Dictionary <DateTimePartStyles, List <string> >(); foreach (KeyValuePair <string, XElement> entry in patterns) { string pattern = ParseDateTimeFormat(entry.Value.Value, out DateTimePartStyles styles); if (!formats.TryGetValue(styles, out CldrDateTimeFormat format)) { List <string> patternIds = new List <string>(); format = new CldrDateTimeFormat(this, styles, pattern, patternIds); formats.Add(styles, format); patternKeys.Add(styles, patternIds); } if (patternKeys.TryGetValue(styles, out List <string> keys)) { keys.Add(entry.Key); } } if (timeFormats.Attribute("inherits") != null) { ReadOnlyCollection <CldrDateTimeFormat> parentFormats = GetGenericOrParent().GetAvailableDateFormats(); foreach (string key in new[] { "full", "long", "medium", "short" }) { if (!patterns.ContainsKey(key)) { CldrDateTimeFormat parent = parentFormats.FirstOrDefault(v => v.PatternId.Contains(key)); if (parent != null) { formats[parent.Styles] = parent; } } } } this.timeFormats = formats.Values.ToList().AsReadOnly(); } } return(this.timeFormats); }