using System.ComponentModel; using System.Reflection; namespace Pf2e.Database.Model.Common.I18n; /// /// Расширения для работы с локализацией. /// public static class LocaleExtension { /// /// Получение строкового представления типа локализации. /// /// Тип локализации. /// Строковое представление типа локализации. public static string GetTypeString(this LocaleType localeType) { FieldInfo fi = localeType.GetType().GetField(localeType.ToString())!; DescriptionAttribute[] attributes = (DescriptionAttribute[]) fi.GetCustomAttributes(typeof(DescriptionAttribute), false); return (attributes.Length != 0 ? attributes.FirstOrDefault()?.Description : null) ?? localeType.ToString(); } /// /// Получение типа локализации по строковому представлению типа локализации. /// /// Строковое представление типа локализации. /// Тип локализации. /// Исключение, если строковое представление не соответсвует ни одному типу локализации. public static LocaleType GetTypeByString(this string localeTypeString) { foreach (FieldInfo field in typeof(LocaleType).GetFields()) { if ( Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) is not DescriptionAttribute descriptionAttribute ) { continue; } if (descriptionAttribute.Description == localeTypeString) { return (LocaleType)field.GetValue(null)!; } } throw new InvalidEnumArgumentException(); } /* public static IQueryable WhereLocale( this IQueryable query, Expression> expression, string searchTerm) where T : class { return query.Where(e => DbContext.LocaleSearch(e.Name.ToJson(), searchTerm)); } public static IQueryable WhereLocale( this IQueryable query, ICollection searchTerms) { return query.Where(e => EF.Functions.LocaleSearchArray(e.Name.ToJson(), searchTerms)); }*/ }