using Microsoft.Extensions.Localization;

namespace Blahblah.FlowerApp;

sealed class LocalizationResource
{
    private static IStringLocalizer<Localizations>? localizer;

    public static IStringLocalizer<Localizations>? Localizer => localizer ??=
#if ANDROID
        MauiApplication
#elif IOS
        MauiUIApplicationDelegate
#endif
        .Current.Services.GetService<IStringLocalizer<Localizations>>();

    public static string GetText(string key, string defaultValue = "")
    {
        return Localizer?.GetString(key) ?? defaultValue;
    }
}

[ContentProperty(nameof(Key))]
public class LangExtension : IMarkupExtension
{
    public required string Key { get; set; }

    public string Default { get; set; } = string.Empty;

    public object ProvideValue(IServiceProvider _)
    {
        return LocalizationResource.GetText(Key, Default);
    }

    object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider) => ProvideValue(serviceProvider);
}
