Pager should inherits from LocalizableComponent

This commit is contained in:
Hisham Bin Ateya
2022-05-27 15:33:48 +03:00
parent c272238539
commit 583383aee1
8 changed files with 185 additions and 21 deletions

View File

@ -9,6 +9,7 @@ namespace Oqtane.Modules.Controls
public class LocalizableComponent : ModuleControlBase
{
private IStringLocalizer _localizer;
private IStringLocalizer _resourceLocalizer;
[Parameter]
public string ResourceKey { get; set; }
@ -18,9 +19,27 @@ namespace Oqtane.Modules.Controls
protected bool IsLocalizable { get; private set; }
protected string Localize(string name) => _localizer?[name] ?? name;
protected string Localize(string name)
{
if (_localizer == null)
{
_localizer = CreateLocalizer();
}
protected string Localize(string propertyName, string propertyValue)
return _localizer[name];
}
protected string Localize(string name, params object[] arguments)
{
if (_localizer == null)
{
_localizer = CreateLocalizer();
}
return _localizer[name, arguments];
}
protected string LocalizeResource(string propertyName, string propertyValue)
{
if (!IsLocalizable)
{
@ -28,7 +47,7 @@ namespace Oqtane.Modules.Controls
}
var key = $"{ResourceKey}.{propertyName}";
var value = Localize(key);
var value = _resourceLocalizer?[key] ?? key;
if (value == key)
{
@ -53,12 +72,12 @@ namespace Oqtane.Modules.Controls
{
IsLocalizable = false;
if (string.IsNullOrEmpty(ResourceType))
if (String.IsNullOrEmpty(ResourceType))
{
ResourceType = ModuleState?.ModuleType;
}
if (!String.IsNullOrEmpty(ResourceKey) && !string.IsNullOrEmpty(ResourceType))
if (!String.IsNullOrEmpty(ResourceKey) && !String.IsNullOrEmpty(ResourceType))
{
var moduleType = Type.GetType(ResourceType);
if (moduleType != null)
@ -66,12 +85,34 @@ namespace Oqtane.Modules.Controls
using (var scope = ServiceActivator.GetScope())
{
var localizerFactory = scope.ServiceProvider.GetService<IStringLocalizerFactory>();
_localizer = localizerFactory.Create(moduleType);
_resourceLocalizer = localizerFactory.Create(moduleType);
IsLocalizable = true;
}
}
}
}
private IStringLocalizer CreateLocalizer()
{
using (var scope = ServiceActivator.GetScope())
{
var controlType = GetType();
var controlTypeName = controlType.Name;
// Remove `1 if the control is generic type
if (controlTypeName.EndsWith("`1"))
{
controlTypeName = controlTypeName.TrimEnd('`', '1');
}
var baseName = controlType.FullName[0..controlType.FullName.IndexOf(controlTypeName)].Substring("Oqtane.".Length);
var localizerFactory = scope.ServiceProvider.GetService<IStringLocalizerFactory>();
_localizer = localizerFactory.Create(baseName + controlTypeName, controlType.Assembly.GetName().Name);
}
return _localizer;
}
}
}