private TimeTextInfo GetTimeTextInfo(IFormatProvider provider) { // Return the default if there is no provider: if (provider == null) { return(CommonLanguagesTimeTextInfo.GetTimeTextInfo(DefaultTwoLetterISOLanguageName)); } // See if the provider can give us what we want: var timeTextInfo = (TimeTextInfo)provider.GetFormat(typeof(TimeTextInfo)); if (timeTextInfo != null) { return(timeTextInfo); } // See if there is a rule for this culture: if (!(provider is CultureInfo cultureInfo)) { return(CommonLanguagesTimeTextInfo.GetTimeTextInfo(DefaultTwoLetterISOLanguageName)); } timeTextInfo = CommonLanguagesTimeTextInfo.GetTimeTextInfo(cultureInfo.TwoLetterISOLanguageName); // If cultureInfo was supplied, // we will always return, even if null: return(timeTextInfo); }
public void Add_Invalid_Custom_Language_TimeTextInfo_Should_Throw() { var language = "123xyz"; Assert.That(() => { TimeTextInfo custom = new() { PluralRule = PluralRules.GetPluralRule(language), Ptxt_week = new[] { "{0} week", "{0} weeks" }, Ptxt_day = new[] { "{0} day", "{0} days" }, Ptxt_hour = new[] { "{0} hour", "{0} hours" }, Ptxt_minute = new[] { "{0} minute", "{0} minutes" }, Ptxt_second = new[] { "{0} second", "{0} seconds" }, Ptxt_millisecond = new[] { "{0} millisecond", "{0} milliseconds" }, Ptxt_w = new[] { "{0}w" }, Ptxt_d = new[] { "{0}d" }, Ptxt_h = new[] { "{0}h" }, Ptxt_m = new[] { "{0}m" }, Ptxt_s = new[] { "{0}s" }, Ptxt_ms = new[] { "{0}ms" }, Ptxt_lessThan = "less than {0}" }; CommonLanguagesTimeTextInfo.AddLanguage(language, custom); }, Throws.ArgumentException.And.Message.Contains("IsoLangToDelegate not found")); } }
/// <summary> /// Initializes the extension with a default TimeTextInfo. /// </summary> /// <param name="defaultTwoLetterLanguageName">This will be used when no CultureInfo is supplied. Can be null.</param> public TimeFormatter(string defaultTwoLetterLanguageName) { if (CommonLanguagesTimeTextInfo.GetTimeTextInfo(defaultTwoLetterLanguageName) == null) { throw new ArgumentException($"Language '{defaultTwoLetterLanguageName}' for {nameof(defaultTwoLetterLanguageName)} is not implemented."); } DefaultTwoLetterISOLanguageName = defaultTwoLetterLanguageName; DefaultFormatOptions = TimeSpanUtility.DefaultFormatOptions; }
public void Get_TimeTextInfo_For_BuiltIn_Languages(string language, string property) { var tti = typeof(CommonLanguagesTimeTextInfo) .GetProperty(property, BindingFlags.Public | BindingFlags.Static) ?.GetValue(null, null) as TimeTextInfo; Assert.That(tti, Is.Not.Null); Assert.That(() => CommonLanguagesTimeTextInfo.GetTimeTextInfo(language) !.GetLessThanText("1"), Is.EqualTo(tti?.GetLessThanText("1"))); }
public void Add_Valid_Custom_Language_TimeTextInfo() { var language = "nl"; // dummy - it's English, not Dutch ;-) TimeTextInfo custom = new() { PluralRule = PluralRules.GetPluralRule(language), Ptxt_week = new[] { "{0} week", "{0} weeks" }, Ptxt_day = new[] { "{0} day", "{0} days" }, Ptxt_hour = new[] { "{0} hour", "{0} hours" }, Ptxt_minute = new[] { "{0} minute", "{0} minutes" }, Ptxt_second = new[] { "{0} second", "{0} seconds" }, Ptxt_millisecond = new[] { "{0} millisecond", "{0} milliseconds" }, Ptxt_w = new[] { "{0}w" }, Ptxt_d = new[] { "{0}d" }, Ptxt_h = new[] { "{0}h" }, Ptxt_m = new[] { "{0}m" }, Ptxt_s = new[] { "{0}s" }, Ptxt_ms = new[] { "{0}ms" }, Ptxt_lessThan = "less than {0}" }; Assert.That(() => CommonLanguagesTimeTextInfo.AddLanguage(language, custom), Throws.Nothing); Assert.That(() => CommonLanguagesTimeTextInfo.GetTimeTextInfo(language), Is.EqualTo(custom)); }