using Microsoft.EntityFrameworkCore; using Pf2eModel.Model.Base; using System.ComponentModel; using System.Linq.Expressions; using System.Reflection; namespace Pf2eModel.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 localeType) { foreach (System.Reflection.FieldInfo field in typeof(LocaleType).GetFields()) { if (Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) is DescriptionAttribute descriptionAttribute) { if (descriptionAttribute.Description == localeType) { 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)); }*/ }