/// <summary> /// Returns all enum values sorted by their display string for the specified language and use case. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="language"></param> /// <param name="translationType"></param> /// <returns></returns> public T[] GetValuesSortedByTranslation <T>(CultureInfo language = null, EnumTranslationType translationType = EnumTranslationType.Default) where T : struct, IComparable, IConvertible, IFormattable { lock (m_lockObject) { Type type = typeof(T); if (!type.IsEnum) { throw new ArgumentException("generic argument T must be an enum"); } if (language == null) { language = CultureInfo.InvariantCulture; } List <EnumTranslationMapping <T> > mappings = new List <EnumTranslationMapping <T> >(); Array arr = System.Enum.GetValues(type); for (int i = 0; i < arr.Length; i++) { T value = (T)arr.GetValue(i); mappings.Add(new EnumTranslationMapping <T>(value, GetStringForValue(value, language, translationType))); } return(mappings .OrderBy(mapping => mapping.Translation, StringComparer.Create(language, true)) .Select(mapping => mapping.EnumValue).ToArray()); } }
private string GetStringForValueInternal(T enumValue, EnumTranslationType translationType) { IDictionary <T, string> translations = GetTranslationsForType(translationType); if (translations is not null) { translations.TryGetValue(enumValue, out string translation); return(translation); } else { return(null); } }
/// <summary> /// Returns the display string of the specified enum value for the specified use case. /// </summary> /// <param name="enumValue"></param> /// <param name="translationType"></param> /// <returns></returns> public override string GetStringForValue(T enumValue, EnumTranslationType translationType = EnumTranslationType.Default) { string translation = GetStringForValueInternal(enumValue, translationType); if (translation == null && translationType != EnumTranslationType.Default) { translation = GetStringForValueInternal(enumValue, EnumTranslationType.Default); } if (translation == null) { translation = enumValue.ToString(); } return(translation); }
/// <summary> /// Returns the display string of the specified enum value for the specified use case. /// </summary> /// <param name="enumValue"></param> /// <param name="translationType"></param> /// <returns></returns> public abstract string GetStringForValue(T enumValue, EnumTranslationType translationType = EnumTranslationType.Default);
/// <summary> /// Returns the display string of the specified enum value for the specified language and use case. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="enumValue"></param> /// <param name="language"></param> /// <param name="translationType"></param> /// <returns></returns> public string GetStringForValue <T>(T enumValue, CultureInfo language = null, EnumTranslationType translationType = EnumTranslationType.Default) where T : struct, IComparable, IConvertible, IFormattable { lock (m_lockObject) { Type type = typeof(T); if (!type.IsEnum) { throw new ArgumentException("generic argument T must be an enum"); } IDictionary <string, object> translationSourcesByLanguage = m_translationSourcesByType[type]; if (language == null) { language = CultureInfo.InvariantCulture; } string[] languageNames = GetHierarichalLanguageNamesDesc(language); for (int i = 0; i < languageNames.Length; i++) { if (translationSourcesByLanguage.TryGetValue(languageNames[i], out object rawTranslationSource)) { return(((IEnumTranslationSource <T>)rawTranslationSource).GetStringForValue(enumValue, translationType)); } } throw new ArgumentException($"Display string for the specified language '{language.DisplayName}' and translation type '{translationType}' not found"); } }
public void Test_GetStringForValue_Ok(VehicleType enumValue, string languageName, EnumTranslationType translationType, string expected) { EnumTranslator enumTranslator = InitTranslations(); string translation = enumTranslator.GetStringForValue(enumValue, new CultureInfo(languageName), translationType); Assert.Equal(expected, translation); }
private IDictionary <T, string> GetTranslationsForType(EnumTranslationType translationType) { m_translations.TryGetValue(translationType, out IDictionary <T, string> translations); return(translations); }