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

Action link localization #828

Merged
merged 4 commits into from
Oct 20, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions Oqtane.Client/Modules/Controls/ActionLink.razor
@@ -1,5 +1,5 @@
@namespace Oqtane.Modules.Controls
@inherits ModuleControlBase
@namespace Oqtane.Modules.Controls
@inherits LocalizableComponent
@inject IUserService UserService

@if (_authorized)
Expand Down Expand Up @@ -56,6 +56,8 @@

protected override void OnParametersSet()
{
base.OnParametersSet();

_text = Action;
if (!string.IsNullOrEmpty(Text))
{
Expand Down Expand Up @@ -93,6 +95,7 @@

}

_text = Localize(nameof(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.

See how things got simpler after LocalizableComponent ;)

_url = EditUrl(Action, _parameters);
_authorized = IsAuthorized();
}
Expand Down
30 changes: 6 additions & 24 deletions Oqtane.Client/Modules/Controls/Label.razor
@@ -1,6 +1,5 @@
@namespace Oqtane.Modules.Controls
@inherits ModuleControlBase
@using Microsoft.Extensions.Localization
@namespace Oqtane.Modules.Controls
@inherits LocalizableComponent

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

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

protected override void OnParametersSet()
{
base.OnParametersSet();

_openLabel = "<label";
if (!string.IsNullOrEmpty(For))
{
Expand All @@ -45,23 +43,7 @@ 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);

// HACK: Use ServiceActivator instead of injecting IHttpContextAccessor, because HttpContext throws NRE in WebAssembly runtime
using (var scope = ServiceActivator.GetScope())
{
var localizer = (IStringLocalizer)scope.ServiceProvider.GetService(localizerType);

ChildContent = @<text>@localizer[$"{ResourceKey}.Text"]</text>;
HelpText = localizer[$"{ResourceKey}.{nameof(HelpText)}"];
}
}
}
ChildContent =@<text>@Localize("Text")</text>;
HelpText = Localize(nameof(HelpText));
}
}
41 changes: 41 additions & 0 deletions Oqtane.Client/Modules/Controls/LocalizableComponent.cs
@@ -0,0 +1,41 @@
using System;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Localization;
using Oqtane.Shared;

namespace Oqtane.Modules.Controls
{
public class LocalizableComponent : ModuleControlBase
{
private IStringLocalizer _localizer;

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

protected string Localize(string name)
{
var key = $"{ResourceKey}.{name}";

return _localizer?[key] ?? key;
}

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);
}
}
}
}
}
}