Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support label localization #796

Merged
merged 2 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Oqtane.Client/Modules/Controls/Label.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
@namespace Oqtane.Modules.Controls
@inherits ModuleControlBase
@using Microsoft.AspNetCore.Http
@using Microsoft.Extensions.Localization
@inject IHttpContextAccessor HttpContextAccessor

@if (!string.IsNullOrEmpty(HelpText))
{
Expand All @@ -26,6 +29,9 @@ else
[Parameter]
public string HelpText { get; set; } // optional - tooltip for this label

[Parameter]
public string ResourceKey { get; set; }

protected override void OnParametersSet()
{
_openLabel = "<label";
Expand All @@ -40,5 +46,19 @@ else
}

_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>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sbwalker I don't see any text property for a label component, but I find this line work to set the label text, please let me know your feedback about this line

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks good! what would the RESX file look like to define the localized values?

Copy link
Contributor Author

@hishamco hishamco Oct 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically as:

Name.Text => Nom

Name.HelpText => Entrez Le num do site

Where Name is the resource name provided in ResourceKey property

I think this what I understand from your comment before ;)

HelpText = localizer[$"{ResourceKey}.{nameof(HelpText)}"];
}
}
}
}
1 change: 1 addition & 0 deletions Oqtane.Client/Oqtane.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<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.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="System.Net.Http.Json" Version="3.2.0" />
</ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions Oqtane.Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static async Task Main(string[] args)

builder.Services.AddSingleton(httpClient);
builder.Services.AddOptions();
builder.Services.AddHttpContextAccessor();

// Register localization services
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
Expand Down