40 lines
1.2 KiB
C#
40 lines
1.2 KiB
C#
using Avalonia.Data;
|
||
using Avalonia.Data.Converters;
|
||
using Cowain.Base.Helpers;
|
||
using Ke.Bee.Localization.Localizer.Abstractions;
|
||
using System.Globalization;
|
||
|
||
namespace Cowain.Base.Converters;
|
||
|
||
public class MultiI18nLocalizeConverter : IMultiValueConverter
|
||
{
|
||
private readonly ILocalizer _l;
|
||
|
||
public MultiI18nLocalizeConverter()
|
||
{
|
||
_l = ServiceLocator.GetRequiredService<ILocalizer>();
|
||
}
|
||
|
||
public object? Convert(IList<object?> values, Type targetType, object? parameter, CultureInfo culture)
|
||
{
|
||
try
|
||
{
|
||
// 参数说明:values[0]=项, values[1]=集合对象
|
||
if (values.Count < 2 || values[0] is null || values[1] is null)
|
||
return BindingOperations.DoNothing;
|
||
|
||
string? val = values[0] is string ss ? ss : "Null";
|
||
IEnumerable<string>? collection = values[1] as IEnumerable<string>;
|
||
|
||
// 从集合中查找项
|
||
object? item = collection?.FirstOrDefault(val);
|
||
if (item is null) return "Not Found";
|
||
return _l[$"{parameter}.{item}"];
|
||
}
|
||
catch
|
||
{
|
||
return BindingOperations.DoNothing;
|
||
}
|
||
}
|
||
}
|