Refactor LocalizableComponent

This commit is contained in:
hishamco 2020-12-08 16:37:55 +03:00
parent d953587e4b
commit df1d646083
6 changed files with 33 additions and 56 deletions

View File

@ -97,12 +97,9 @@
_iconSpan = $"<span class=\"oi oi-{IconName}\"></span>&nbsp;"; _iconSpan = $"<span class=\"oi oi-{IconName}\"></span>&nbsp;";
} }
if (IsLocalizable)
{
Text = Localize(nameof(Text), Text); Text = Localize(nameof(Text), Text);
Header = Localize(nameof(Header), Header); Header = Localize(nameof(Header), Header);
Message = Localize(nameof(Message), Message); Message = Localize(nameof(Message), Message);
}
_authorized = IsAuthorized(); _authorized = IsAuthorized();
} }

View File

@ -95,11 +95,7 @@
} }
if (IsLocalizable)
{
_text = Localize(nameof(Text), _text); _text = Localize(nameof(Text), _text);
}
_url = EditUrl(Action, _parameters); _url = EditUrl(Action, _parameters);
_authorized = IsAuthorized(); _authorized = IsAuthorized();
} }

View File

@ -45,11 +45,11 @@ else
if (IsLocalizable) if (IsLocalizable)
{ {
var value = Localize("Text");
var key = $"{ResourceKey}.Text"; var key = $"{ResourceKey}.Text";
var value = Localize(key);
if (!value.Equals(key)) if (!value.Equals(key))
{ {
ChildContent =@<text>@Localize("Text")</text>; ChildContent =@<text>@value</text>;
} }
HelpText = Localize(nameof(HelpText), HelpText); HelpText = Localize(nameof(HelpText), HelpText);

View File

@ -15,27 +15,22 @@ namespace Oqtane.Modules.Controls
protected bool IsLocalizable { get; private set; } protected bool IsLocalizable { get; private set; }
protected string Localize(string name) protected string Localize(string name) => _localizer?[name] ?? name;
{
var key = $"{ResourceKey}.{name}";
// TODO: we should have a ShowMissingResourceKeys option which developers/translators can enable to find missing translations which would display the key rather than the name protected string Localize(string propertyName, string propertyValue)
{
if (!IsLocalizable) if (!IsLocalizable)
{ {
return name; return propertyValue;
} }
return _localizer?[key] ?? name; var key = $"{ResourceKey}.{propertyName}";
} var value = Localize(key);
protected string Localize(string name, string defaultValue)
{
var key = $"{ResourceKey}.{name}";
var value = Localize(name);
if (value == key) if (value == key)
{ {
return defaultValue; // Returns default property value (English version) instead of ResourceKey.PropertyName
return propertyValue;
} }
else else
{ {
@ -45,9 +40,9 @@ namespace Oqtane.Modules.Controls
protected override void OnParametersSet() protected override void OnParametersSet()
{ {
if (!String.IsNullOrEmpty(ResourceKey)) IsLocalizable = false;
{
if (ModuleState?.ModuleType != null) if (!String.IsNullOrEmpty(ResourceKey) && ModuleState?.ModuleType != null)
{ {
var moduleType = Type.GetType(ModuleState.ModuleType); var moduleType = Type.GetType(ModuleState.ModuleType);
if (moduleType != null) if (moduleType != null)
@ -56,15 +51,10 @@ namespace Oqtane.Modules.Controls
{ {
var localizerFactory = scope.ServiceProvider.GetService<IStringLocalizerFactory>(); var localizerFactory = scope.ServiceProvider.GetService<IStringLocalizerFactory>();
_localizer = localizerFactory.Create(moduleType); _localizer = localizerFactory.Create(moduleType);
}
}
}
IsLocalizable = true; IsLocalizable = true;
} }
else }
{
IsLocalizable = false;
} }
} }
} }

View File

@ -46,11 +46,8 @@
{ {
base.OnParametersSet(); base.OnParametersSet();
if (IsLocalizable)
{
_heading = !string.IsNullOrEmpty(Heading) _heading = !string.IsNullOrEmpty(Heading)
? Localize(nameof(Heading), Heading) ? Localize(nameof(Heading), Heading)
: Localize(nameof(Name), Name); : Localize(nameof(Name), Name);
} }
}
} }

View File

@ -40,8 +40,6 @@ else
{ {
base.OnParametersSet(); base.OnParametersSet();
if (IsLocalizable)
{
if (string.IsNullOrEmpty(Heading)) if (string.IsNullOrEmpty(Heading))
{ {
Name = Localize(nameof(Name), Name); Name = Localize(nameof(Name), Name);
@ -51,7 +49,6 @@ else
Heading = Localize(nameof(Heading), Heading); Heading = Localize(nameof(Heading), Heading);
} }
} }
}
public string DisplayHeading() public string DisplayHeading()
{ {