From 5bb7c63d44d042952502dc81378f118f57e80c7c Mon Sep 17 00:00:00 2001 From: hishamco Date: Mon, 19 Oct 2020 11:16:46 +0300 Subject: [PATCH] Introduce LocalizableComponent --- .../Modules/Controls/LocalizableComponent.cs | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Oqtane.Client/Modules/Controls/LocalizableComponent.cs diff --git a/Oqtane.Client/Modules/Controls/LocalizableComponent.cs b/Oqtane.Client/Modules/Controls/LocalizableComponent.cs new file mode 100644 index 00000000..e442c152 --- /dev/null +++ b/Oqtane.Client/Modules/Controls/LocalizableComponent.cs @@ -0,0 +1,34 @@ +using System; +using Microsoft.AspNetCore.Components; +using Microsoft.Extensions.Localization; +using Oqtane.Shared; + +namespace Oqtane.Modules.Controls +{ + public class LocalizableComponent : ModuleControlBase + { + [Parameter] + public string ResourceKey { get; set; } + + protected IStringLocalizer Localizer { get; private set; } + + protected override void OnParametersSet() + { + if (!String.IsNullOrEmpty(ResourceKey)) + { + if (ModuleState?.ModuleType != null) + { + var moduleType = Type.GetType(ModuleState.ModuleType); + var localizerTypeName = $"Microsoft.Extensions.Localization.IStringLocalizer`1[[{moduleType.AssemblyQualifiedName}]], Microsoft.Extensions.Localization.Abstractions"; + var localizerType = Type.GetType(localizerTypeName); + + // HACK: Use ServiceActivator instead of injecting IHttpContextAccessor, because HttpContext throws NRE in WebAssembly runtime + using (var scope = ServiceActivator.GetScope()) + { + Localizer = (IStringLocalizer)scope.ServiceProvider.GetService(localizerType); + } + } + } + } + } +}