flower-story/App/Extensions.cs
2023-06-26 22:33:44 +08:00

37 lines
1.0 KiB
C#

using Microsoft.Extensions.Localization;
using System.Text;
namespace Blahblah.FlowerStory;
[ContentProperty(nameof(Key))]
public class LocalizeExtension : IMarkupExtension
{
//private IStringLocalizer<Localizations> Localizer { get; }
public string Key { get; set; }
//public LocalizeExtension()
//{
// Localizer = MauiApplication.Current.Services.GetService<IStringLocalizer<Localizations>>();
//}
public object ProvideValue(IServiceProvider _)
{
return LocalizationResource.Localizer.GetString(Key);
}
object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider) => ProvideValue(serviceProvider);
}
sealed class LocalizationResource
{
private static IStringLocalizer<Localizations> localizer;
public static IStringLocalizer<Localizations> Localizer => localizer ??= MauiApplication.Current.Services.GetService<IStringLocalizer<Localizations>>();
public static string GetText(string key)
{
return Localizer.GetString(key);
}
}