52 lines
1.6 KiB
C#
52 lines
1.6 KiB
C#
using System.ComponentModel;
|
|
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 (FieldInfo field in typeof(LocaleType).GetFields())
|
|
{
|
|
if (
|
|
Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute))
|
|
is not DescriptionAttribute descriptionAttribute
|
|
)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (descriptionAttribute.Description == localeType)
|
|
{
|
|
return (LocaleType)field.GetValue(null)!;
|
|
}
|
|
}
|
|
|
|
throw new InvalidEnumArgumentException();
|
|
}
|
|
/*
|
|
public static IQueryable<T> WhereLocale(
|
|
this IQueryable<T> query, Expression<Func<T, LocaleString>> expression, string searchTerm) where T : class
|
|
{
|
|
return query.Where(e => DbContext.LocaleSearch(e.Name.ToJson(), searchTerm));
|
|
}
|
|
|
|
public static IQueryable<Entity> WhereLocale(
|
|
this IQueryable<Entity> query, ICollection<string> searchTerms)
|
|
{
|
|
return query.Where(e => EF.Functions.LocaleSearchArray(e.Name.ToJson(), searchTerms));
|
|
}*/
|
|
}
|