61 lines
2.0 KiB
C#
61 lines
2.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.UI.Xaml.Data;
|
|
using Microsoft.UI.Xaml.Markup;
|
|
using Microsoft.Windows.AI.Text;
|
|
using WinWARP.Core.Services.Interfaces;
|
|
using WinWARP.Ui.Helpers;
|
|
|
|
namespace WinWARP.Ui.Converters;
|
|
|
|
[MarkupExtensionReturnType(ReturnType = typeof(IValueConverter))]
|
|
public class UpdateIntervalToTextConverter : MarkupExtension, IValueConverter
|
|
{
|
|
private ILocalizationService _localizationService;
|
|
|
|
protected override object ProvideValue()
|
|
{
|
|
_localizationService ??= App.Services.GetRequiredService<ILocalizationService>();
|
|
return this;
|
|
}
|
|
|
|
public object Convert(object value, Type targetType, object parameter, string language)
|
|
{
|
|
if (value is double interval)
|
|
{
|
|
interval = Math.Truncate(interval);
|
|
return interval switch
|
|
{
|
|
-1 => _localizationService.GetString("UpdateInterval_never"),
|
|
0 => _localizationService.GetString("UpdateInterval_auto"),
|
|
_ => NumberToTranslatedString(interval)
|
|
};
|
|
}
|
|
return "Unknown";
|
|
}
|
|
|
|
public object ConvertBack(object value, Type targetType, object parameter, string language)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
private string NumberToTranslatedString(double value)
|
|
{
|
|
var category = PluralCategory.GetCategory(value, new CultureInfo(_localizationService.GetCurrentLanguage()));
|
|
var resourceKey = $"UpdateInterval_{category}";
|
|
var localizedTemplate = _localizationService.GetString(resourceKey);
|
|
|
|
if (string.IsNullOrEmpty(localizedTemplate))
|
|
{
|
|
// Fallback to a default template if the localized string is not found
|
|
localizedTemplate = "Every {0} hours";
|
|
}
|
|
|
|
return string.Format(localizedTemplate, value);
|
|
}
|
|
}
|