Avoid resource check in child components

This commit is contained in:
hishamco 2020-10-19 11:36:05 +03:00
parent fd5d777d3a
commit fed56098a0
2 changed files with 11 additions and 7 deletions

View File

@ -43,10 +43,7 @@ else
_openLabel += ">"; _openLabel += ">";
if (!string.IsNullOrEmpty(ResourceKey)) ChildContent =@<text>@Localize("Text")</text>;
{ HelpText = Localize(nameof(HelpText));
ChildContent =@<text>@Localizer[$"{ResourceKey}.Text"]</text>;
HelpText = Localizer[$"{ResourceKey}.{nameof(HelpText)}"];
}
} }
} }

View File

@ -7,10 +7,17 @@ namespace Oqtane.Modules.Controls
{ {
public class LocalizableComponent : ModuleControlBase public class LocalizableComponent : ModuleControlBase
{ {
private IStringLocalizer _localizer;
[Parameter] [Parameter]
public string ResourceKey { get; set; } public string ResourceKey { get; set; }
protected IStringLocalizer Localizer { get; private set; } protected string Localize(string name)
{
var key = $"{ResourceKey}.{name}";
return _localizer?[key] ?? key;
}
protected override void OnParametersSet() protected override void OnParametersSet()
{ {
@ -25,7 +32,7 @@ namespace Oqtane.Modules.Controls
// HACK: Use ServiceActivator instead of injecting IHttpContextAccessor, because HttpContext throws NRE in WebAssembly runtime // HACK: Use ServiceActivator instead of injecting IHttpContextAccessor, because HttpContext throws NRE in WebAssembly runtime
using (var scope = ServiceActivator.GetScope()) using (var scope = ServiceActivator.GetScope())
{ {
Localizer = (IStringLocalizer)scope.ServiceProvider.GetService(localizerType); _localizer = (IStringLocalizer)scope.ServiceProvider.GetService(localizerType);
} }
} }
} }