44 lines
1.3 KiB
C#
44 lines
1.3 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 I18nLocalizeConverter : IValueConverter
|
|||
|
|
{
|
|||
|
|
private readonly ILocalizer _l;
|
|||
|
|
public I18nLocalizeConverter()
|
|||
|
|
{
|
|||
|
|
_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 "未配置";
|
|||
|
|
var key = $"{prefix}.{value}";
|
|||
|
|
var result = _l[key];
|
|||
|
|
// 如果本地化内容为空或与 key 相同,则返回原始 value 字符串
|
|||
|
|
if (string.IsNullOrEmpty(result) || result == key)
|
|||
|
|
return value.ToString();
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public object? ConvertBack(object? value, Type targetType, object? prefix, CultureInfo culture)
|
|||
|
|
{
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
}
|