Merge pull request #796 from hishamco/localizable-labels
Support label localization
This commit is contained in:
commit
81475fd835
|
@ -1,5 +1,8 @@
|
||||||
@namespace Oqtane.Modules.Controls
|
@namespace Oqtane.Modules.Controls
|
||||||
@inherits ModuleControlBase
|
@inherits ModuleControlBase
|
||||||
|
@using Microsoft.AspNetCore.Http
|
||||||
|
@using Microsoft.Extensions.Localization
|
||||||
|
@inject IHttpContextAccessor HttpContextAccessor
|
||||||
|
|
||||||
@if (!string.IsNullOrEmpty(HelpText))
|
@if (!string.IsNullOrEmpty(HelpText))
|
||||||
{
|
{
|
||||||
|
@ -26,6 +29,9 @@ else
|
||||||
[Parameter]
|
[Parameter]
|
||||||
public string HelpText { get; set; } // optional - tooltip for this label
|
public string HelpText { get; set; } // optional - tooltip for this label
|
||||||
|
|
||||||
|
[Parameter]
|
||||||
|
public string ResourceKey { get; set; }
|
||||||
|
|
||||||
protected override void OnParametersSet()
|
protected override void OnParametersSet()
|
||||||
{
|
{
|
||||||
_openLabel = "<label";
|
_openLabel = "<label";
|
||||||
|
@ -40,5 +46,19 @@ else
|
||||||
}
|
}
|
||||||
|
|
||||||
_openLabel += ">";
|
_openLabel += ">";
|
||||||
|
|
||||||
|
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);
|
||||||
|
var localizer = (IStringLocalizer)HttpContextAccessor.HttpContext.RequestServices.GetService(localizerType);
|
||||||
|
|
||||||
|
ChildContent = @<text>@localizer[$"{ResourceKey}.Text"]</text>;
|
||||||
|
HelpText = localizer[$"{ResourceKey}.{nameof(HelpText)}"];
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,6 +32,7 @@
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Build" Version="3.2.0" PrivateAssets="all" />
|
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Build" Version="3.2.0" PrivateAssets="all" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="3.2.0" PrivateAssets="all" />
|
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="3.2.0" PrivateAssets="all" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="3.1.4" />
|
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="3.1.4" />
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Localization" Version="3.1.3" />
|
<PackageReference Include="Microsoft.Extensions.Localization" Version="3.1.3" />
|
||||||
<PackageReference Include="System.Net.Http.Json" Version="3.2.0" />
|
<PackageReference Include="System.Net.Http.Json" Version="3.2.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
|
@ -26,6 +26,7 @@ namespace Oqtane.Client
|
||||||
|
|
||||||
builder.Services.AddSingleton(httpClient);
|
builder.Services.AddSingleton(httpClient);
|
||||||
builder.Services.AddOptions();
|
builder.Services.AddOptions();
|
||||||
|
builder.Services.AddHttpContextAccessor();
|
||||||
|
|
||||||
// Register localization services
|
// Register localization services
|
||||||
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
|
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
|
||||||
|
|
Loading…
Reference in New Issue
Block a user