/// <summary> /// Tries to convert a given <paramref name="text"/> into the enum value of a given enum-type <typeparamref name="T"/> using <see cref="ResourceUtil"/>. /// </summary> /// <typeparam name="T">The type of the enum.</typeparam> /// <param name="localeId">The locale id for passing to the resources.</param> /// <param name="text">The name of the enum member or the value of its <see cref="EnumMemberAttribute"/>.</param> /// <returns>The enum value.</returns> public static T LocalizedTextToEnum <T>(string localeId, string text) where T : struct { CheckUtil.ThrowIfNullOrEmpty(() => text); var enumType = typeof(T); if (!enumType.IsEnum) { throw new InvalidOperationException("Type is no enumration."); } foreach (var field in enumType.GetFields()) { var attribute = Attribute.GetCustomAttribute(field, typeof(LocalizedEnumMemberAttribute)) as LocalizedEnumMemberAttribute; if (attribute != null && !string.IsNullOrEmpty(attribute.ResourceKey)) { // We found a LocalizedEnumMemberAttribute with a given Value-property ... var textToSearch = PortableResourceUtil.Get <string>(localeId, attribute.ResourceKey, attribute.ResourceType); if (textToSearch == text) { // ... and we got a match on the value -> we will retrieve the name of the value. return((T)field.GetValue(null)); } } else { // We did not find a LocalizedEnumMemberAttribute or it's Value-property was empty ... if (string.Equals(text, field.Name, StringComparison.OrdinalIgnoreCase)) { // ... and we got a match on the value -> we will retrieve the name of the value. return((T)field.GetValue(null)); } } } // If we reach this point, GetFields did not contain the field provided in value throw new InvalidOperationException("The provided value is not a member of the given Enum type."); }
/// <summary> /// Generates a speaking text for a given amount of <paramref name="seconds" />. /// </summary> /// <param name="localeId">The locale ID.</param> /// <param name="seconds">The amount of seconds representing the amount of time.</param> /// <param name="resourceResolver">An optional function that this method uses to retrieve resource-strings.</param> /// <returns>The speaking text.</returns> public static string GetSpeakingTimeString(string localeId, long seconds, Func <string, string> resourceResolver = null) { if (resourceResolver == null) { resourceResolver = key => PortableResourceUtil.Get(localeId, key); } var span = TimeSpan.FromSeconds(seconds); var builder = new StringBuilder(); var resourceKey = string.Empty; if (span.Days > 365) { var years = span.Days / 365; span = span.Subtract(TimeSpan.FromSeconds(years * UnitConversionCalculator.SecondsPerYear)); resourceKey = years != 1 ? "TimePartYearPlural" : "TimePartYearSingular"; builder.AppendFormat("{0} {1} ", years, resourceResolver(resourceKey)); } if (span.Days > 29) { var months = span.Days / 30; span = span.Subtract(TimeSpan.FromSeconds(months * UnitConversionCalculator.SecondsPerMonth)); resourceKey = months != 1 ? "TimePartMonthPlural" : "TimePartMonthSingular"; builder.AppendFormat("{0} {1} ", months, resourceResolver(resourceKey)); } if (span.Days > 6) { var weeks = span.Days / 7; span = span.Subtract(TimeSpan.FromSeconds(weeks * UnitConversionCalculator.SecondsPerWeek)); resourceKey = weeks != 1 ? "TimePartWeekPlural" : "TimePartWeekSingular"; builder.AppendFormat("{0} {1} ", weeks, resourceResolver(resourceKey)); } if (span.Days >= 1) { resourceKey = span.Days != 1 ? "TimePartDayPlural" : "TimePartDaySingular"; builder.AppendFormat("{0} {1} ", span.Days, resourceResolver(resourceKey)); span = span.Subtract(TimeSpan.FromSeconds(span.Days * UnitConversionCalculator.SecondsPerDay)); } resourceKey = span.Hours != 1 ? "TimePartHourPlural" : "TimePartHourSingular"; builder.AppendFormat("{0} {1} ", span.Hours, resourceResolver(resourceKey)); span = span.Subtract(TimeSpan.FromSeconds(span.Hours * 60 * 60)); resourceKey = span.Minutes != 1 ? "TimePartMinutePlural" : "TimePartMinuteSingular"; builder.AppendFormat("{0} {1} ", span.Minutes, resourceResolver(resourceKey)); span = span.Subtract(TimeSpan.FromSeconds(span.Hours * 60)); resourceKey = span.Seconds != 1 ? "TimePartSecondPlural" : "TimePartSecondSingular"; builder.AppendFormat("{0} {1} ", span.Seconds, resourceResolver(resourceKey)); return(builder.ToString().Trim()); }
public static string LocalizedEnumValueToText <T>(string localeId, Enum value) { if (value == null) { return(string.Empty); } var enumType = typeof(T); if (!enumType.IsEnum) { throw new InvalidOperationException("Type is no enumration."); } // get the source-code name of the enum-value var valueAsText = value.ToString("G"); // try to get the LocalizedEnumMemberAttribute for this enum-value var enumMemberAttribute = ((LocalizedEnumMemberAttribute[])enumType.GetField(valueAsText).GetCustomAttributes(typeof(LocalizedEnumMemberAttribute), true)).SingleOrDefault(); // return the attributes value of the name of the enum-value if no LocalizedEnumMemberAttribute was found return((enumMemberAttribute != null) ? PortableResourceUtil.Get <string>(localeId, enumMemberAttribute.ResourceKey, enumMemberAttribute.ResourceType) : valueAsText); }
/// <summary> /// Generates a speaking text for a given amount of <paramref name="seconds" /> with abbrevations for HTML-output. /// </summary> /// <param name="localeId">The locale ID.</param> /// <param name="seconds">The amount of seconds representing the amount of time.</param> /// <param name="classNameUnits">The name of the CSS-class to format the units with.</param> /// <param name="resourceResolver">An optional function that this method uses to retrieve resource-strings.</param> /// <returns>The speaking text in HTML.</returns> public static string GetSpeakingTimeStringShortHtml(string localeId, long seconds, string classNameUnits, Func <string, string> resourceResolver = null) { if (resourceResolver == null) { resourceResolver = key => PortableResourceUtil.Get(localeId, key); } var span = TimeSpan.FromSeconds(seconds); var builder = new StringBuilder(); if (span.Days > 365) { var years = span.Days / 365; span = span.Subtract(TimeSpan.FromSeconds(years * UnitConversionCalculator.SecondsPerYear)); builder.AppendFormat("{0}<span class=\"{1}\">{2}</span>", years, classNameUnits, resourceResolver("TimePartYearShort")); } if (span.Days > 29) { var months = span.Days / 30; span = span.Subtract(TimeSpan.FromSeconds(months * UnitConversionCalculator.SecondsPerMonth)); builder.AppendFormat("{0}<span class=\"{1}\">{2}</span>", months, classNameUnits, resourceResolver("TimePartMonthShort")); } if (span.Days > 6) { var weeks = span.Days / 7; span = span.Subtract(TimeSpan.FromSeconds(weeks * UnitConversionCalculator.SecondsPerWeek)); builder.AppendFormat("{0}<span class=\"{1}\">{2}</span>", weeks, classNameUnits, resourceResolver("TimePartWeekShort")); } if (span.Days >= 1) { builder.AppendFormat("{0}<span class=\"{1}\">{2}</span>", span.Days, classNameUnits, resourceResolver("TimePartDayShort")); span = span.Subtract(TimeSpan.FromSeconds(span.Days * UnitConversionCalculator.SecondsPerDay)); } builder.AppendFormat("{0}<span class=\"{1}\">{2}</span>", span.Hours, classNameUnits, resourceResolver("TimePartHourShort")); span = span.Subtract(TimeSpan.FromSeconds(span.Hours * 60 * 60)); builder.AppendFormat("{0}<span class=\"{1}\">{2}</span>", span.Minutes, classNameUnits, resourceResolver("TimePartMinuteShort")); span = span.Subtract(TimeSpan.FromSeconds(span.Hours * 60)); builder.AppendFormat("{0}<span class=\"{1}\">{2}</span>", span.Seconds, classNameUnits, resourceResolver("TimePartSecondShort")); return(builder.ToString().Trim()); }