added extension method for creating a LocalizerFactory using a type name, refactored Pager and LocalizableComponent to use LocalizerFactory
This commit is contained in:
parent
657c71e94d
commit
1ce3cc4d7c
|
@ -1,3 +1,5 @@
|
|||
using System;
|
||||
|
||||
namespace Microsoft.Extensions.Localization
|
||||
{
|
||||
public static class OqtaneLocalizationExtensions
|
||||
|
@ -18,5 +20,42 @@ namespace Microsoft.Extensions.Localization
|
|||
}
|
||||
return localizedValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates an IStringLocalizer based on a type name. This extension method is useful in scenarios where the default IStringLocalizer is unable to locate the resources.
|
||||
/// </summary>
|
||||
/// <param name="localizerFactory"></param>
|
||||
/// <param name="fullTypeName">the full type name ie. GetType().FullName</param>
|
||||
/// <returns></returns>
|
||||
public static IStringLocalizer Create(this IStringLocalizerFactory localizerFactory, string fullTypeName)
|
||||
{
|
||||
var typename = fullTypeName;
|
||||
|
||||
// handle generic types
|
||||
var type = Type.GetType(fullTypeName);
|
||||
if (type.IsGenericType)
|
||||
{
|
||||
typename = type.GetGenericTypeDefinition().FullName;
|
||||
typename = typename.Substring(0, typename.IndexOf("`")); // remove generic type info
|
||||
}
|
||||
|
||||
// format typename
|
||||
if (typename.Contains(","))
|
||||
{
|
||||
typename = typename.Substring(0, typename.IndexOf(",")); // remove assembly info
|
||||
}
|
||||
|
||||
// remove rootnamespace
|
||||
var rootnamespace = "";
|
||||
var attributes = type.Assembly.GetCustomAttributes(typeof(RootNamespaceAttribute), false);
|
||||
if (attributes.Length > 0)
|
||||
{
|
||||
rootnamespace = ((RootNamespaceAttribute)attributes[0]).RootNamespace;
|
||||
}
|
||||
typename = typename.Replace(rootnamespace + ".", "");
|
||||
|
||||
// create IStringLocalizer using factory
|
||||
return localizerFactory.Create(typename, type.Assembly.GetName().Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,16 +1,14 @@
|
|||
using System;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Localization;
|
||||
using Oqtane.Shared;
|
||||
|
||||
namespace Oqtane.Modules.Controls
|
||||
{
|
||||
public class LocalizableComponent : ModuleControlBase
|
||||
{
|
||||
[Inject] public IStringLocalizerFactory LocalizerFactory { get; set; }
|
||||
|
||||
private IStringLocalizer _localizer;
|
||||
private IStringLocalizer _resourceLocalizer;
|
||||
|
||||
[Parameter]
|
||||
public string ResourceKey { get; set; }
|
||||
|
@ -20,37 +18,6 @@ namespace Oqtane.Modules.Controls
|
|||
|
||||
protected bool IsLocalizable { get; private set; }
|
||||
|
||||
protected IStringLocalizer T
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_resourceLocalizer == null)
|
||||
{
|
||||
using (var scope = ServiceActivator.GetScope())
|
||||
{
|
||||
var controlType = GetType();
|
||||
var controlTypeName = controlType.Name;
|
||||
var assemblyName = controlType.Assembly.GetName().Name;
|
||||
|
||||
if (controlType.IsGenericType)
|
||||
{
|
||||
// Trim generic type suffix
|
||||
controlTypeName = controlTypeName[..controlTypeName.IndexOf('`')];
|
||||
}
|
||||
|
||||
controlTypeName = controlType.FullName[..(controlType.FullName.IndexOf(controlTypeName) + controlTypeName.Length)];
|
||||
|
||||
var baseName = GetBaseName(controlTypeName, assemblyName);
|
||||
var localizerFactory = scope.ServiceProvider.GetService<IStringLocalizerFactory>();
|
||||
|
||||
_resourceLocalizer = localizerFactory.Create(baseName, assemblyName);
|
||||
}
|
||||
}
|
||||
|
||||
return _resourceLocalizer;
|
||||
}
|
||||
}
|
||||
|
||||
protected string Localize(string name) => _localizer?[name] ?? name;
|
||||
|
||||
protected string Localize(string propertyName, string propertyValue)
|
||||
|
@ -63,24 +30,17 @@ namespace Oqtane.Modules.Controls
|
|||
var key = $"{ResourceKey}.{propertyName}";
|
||||
var value = Localize(key);
|
||||
|
||||
if (value == key)
|
||||
if (value == key || value == String.Empty)
|
||||
{
|
||||
// Returns default property value (English version) instead of ResourceKey.PropertyName
|
||||
return propertyValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (value == String.Empty)
|
||||
{
|
||||
// Returns default property value (English version)
|
||||
// return default property value if key does not exist in resource file or value is empty
|
||||
return propertyValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
// return localized value
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnParametersSet()
|
||||
{
|
||||
|
@ -93,39 +53,9 @@ namespace Oqtane.Modules.Controls
|
|||
|
||||
if (!String.IsNullOrEmpty(ResourceKey) && !String.IsNullOrEmpty(ResourceType))
|
||||
{
|
||||
var moduleType = Type.GetType(ResourceType);
|
||||
if (moduleType != null)
|
||||
{
|
||||
using (var scope = ServiceActivator.GetScope())
|
||||
{
|
||||
var localizerFactory = scope.ServiceProvider.GetService<IStringLocalizerFactory>();
|
||||
_localizer = localizerFactory.Create(moduleType);
|
||||
|
||||
_localizer = LocalizerFactory.Create(ResourceType);
|
||||
IsLocalizable = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetBaseName(string typeName, string assemblyName)
|
||||
{
|
||||
var baseName = new StringBuilder(typeName);
|
||||
|
||||
var tokens = assemblyName.Split(".");
|
||||
|
||||
foreach (var token in tokens)
|
||||
{
|
||||
var index = baseName.ToString().IndexOf(token);
|
||||
|
||||
if (index == -1)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
baseName.Remove(index, token.Length + 1);
|
||||
}
|
||||
|
||||
return baseName.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
@namespace Oqtane.Modules.Controls
|
||||
@inherits LocalizableComponent
|
||||
|
||||
@inherits ModuleControlBase
|
||||
@inject IStringLocalizerFactory LocalizerFactory
|
||||
@typeparam TableItem
|
||||
|
||||
@if (ItemList != null)
|
||||
|
@ -49,7 +49,7 @@
|
|||
<a class="page-link" @onclick=@(async () => UpdateList(_pages))><span class="oi oi-media-step-forward" title="end" aria-hidden="true"></span></a>
|
||||
</li>
|
||||
<li class="page-item disabled">
|
||||
<a class="page-link" style="white-space: nowrap;">@T["PageOfPages", _page, _pages]</a>
|
||||
<a class="page-link" style="white-space: nowrap;">@Localizer["PageOfPages", _page, _pages]</a>
|
||||
</li>
|
||||
</ul>
|
||||
}
|
||||
|
@ -157,13 +157,14 @@
|
|||
<a class="page-link" @onclick=@(async () => UpdateList(_pages))><span class="oi oi-media-step-forward" title="end" aria-hidden="true"></span></a>
|
||||
</li>
|
||||
<li class="page-item disabled">
|
||||
<a class="page-link" style="white-space: nowrap;">@T["PageOfPages", _page, _pages]</a>
|
||||
<a class="page-link" style="white-space: nowrap;">@Localizer["PageOfPages", _page, _pages]</a>
|
||||
</li>
|
||||
</ul>
|
||||
}
|
||||
}
|
||||
|
||||
@code {
|
||||
private IStringLocalizer Localizer;
|
||||
private int _pages = 0;
|
||||
private int _page = 1;
|
||||
private int _maxItems = 10;
|
||||
|
@ -216,6 +217,11 @@
|
|||
|
||||
private IEnumerable<TableItem> ItemList { get; set; }
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
Localizer = LocalizerFactory.Create(GetType().FullName);
|
||||
}
|
||||
|
||||
protected override void OnParametersSet()
|
||||
{
|
||||
if (string.IsNullOrEmpty(Format))
|
||||
|
|
Loading…
Reference in New Issue
Block a user