39 lines
1.0 KiB
C#
39 lines
1.0 KiB
C#
using Avalonia.Data.Converters;
|
|
using Cowain.Base.Helpers;
|
|
using Ke.Bee.Localization.Localizer.Abstractions;
|
|
using System.Globalization;
|
|
|
|
namespace Cowain.Base.Converters;
|
|
|
|
/// <summary>
|
|
/// 枚举值本地化转换器
|
|
/// </summary>
|
|
public class EnumLocalizeConverter : IValueConverter
|
|
{
|
|
private readonly ILocalizer _l;
|
|
public EnumLocalizeConverter()
|
|
{
|
|
_l = ServiceLocator.GetRequiredService<ILocalizer>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 枚举值转换为本地化文本
|
|
/// </summary>
|
|
/// <param name="value">枚举值</param>
|
|
/// <param name="targetType"></param>
|
|
/// <param name="prefix">对应的本地化前缀</param>
|
|
/// <param name="culture"></param>
|
|
/// <returns></returns>
|
|
public object? Convert(object? value, Type targetType, object? prefix, CultureInfo culture)
|
|
{
|
|
if (value != null)
|
|
return _l[$"{prefix}.{value}"];
|
|
return null;
|
|
}
|
|
|
|
public object? ConvertBack(object? value, Type targetType, object? prefix, CultureInfo culture)
|
|
{
|
|
return null;
|
|
}
|
|
}
|