Define localizable string

pull/279/head
Halil İbrahim Kalkan 7 years ago
parent 2e25a4a4a5
commit 41240d2cfe

@ -18,6 +18,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Volo.Abp.Localization\Volo.Abp.Localization.csproj" />
<ProjectReference Include="..\Volo.Abp.Security\Volo.Abp.Security.csproj" /> <ProjectReference Include="..\Volo.Abp.Security\Volo.Abp.Security.csproj" />
</ItemGroup> </ItemGroup>

@ -1,12 +1,16 @@
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Authorization.Permissions; using Volo.Abp.Authorization.Permissions;
using Volo.Abp.Localization;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
using Volo.Abp.Security; using Volo.Abp.Security;
namespace Volo.Abp.Authorization namespace Volo.Abp.Authorization
{ {
[DependsOn(typeof(AbpSecurityModule))] [DependsOn(
typeof(AbpSecurityModule),
typeof(AbpLocalizationModule)
)]
public class AbpAuthorizationModule : AbpModule public class AbpAuthorizationModule : AbpModule
{ {
public override void PreConfigureServices(IServiceCollection services) public override void PreConfigureServices(IServiceCollection services)

@ -1,5 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable; using System.Collections.Immutable;
using JetBrains.Annotations;
using Volo.Abp.Localization;
namespace Volo.Abp.Authorization.Permissions namespace Volo.Abp.Authorization.Permissions
{ {
@ -16,6 +18,8 @@ namespace Volo.Abp.Authorization.Permissions
/// </summary> /// </summary>
public PermissionDefinition Parent { get; private set; } public PermissionDefinition Parent { get; private set; }
public ILocalizableString DisplayName { get; set; }
public IReadOnlyList<PermissionDefinition> Children => _children.ToImmutableList(); public IReadOnlyList<PermissionDefinition> Children => _children.ToImmutableList();
private readonly List<PermissionDefinition> _children; private readonly List<PermissionDefinition> _children;
@ -35,17 +39,18 @@ namespace Volo.Abp.Authorization.Permissions
set => Properties[name] = value; set => Properties[name] = value;
} }
protected internal PermissionDefinition(string name) protected internal PermissionDefinition([NotNull] string name, ILocalizableString displayName = null)
{ {
Name = name; Name = Check.NotNull(name, nameof(name));
DisplayName = displayName ?? new FixedLocalizableString(name);
Properties = new Dictionary<string, object>(); Properties = new Dictionary<string, object>();
_children = new List<PermissionDefinition>(); _children = new List<PermissionDefinition>();
} }
public virtual PermissionDefinition AddChild(string name) public virtual PermissionDefinition AddChild([NotNull] string name, ILocalizableString displayName = null)
{ {
var child = new PermissionDefinition(name) var child = new PermissionDefinition(name, displayName)
{ {
Parent = this Parent = this
}; };

@ -1,5 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable; using System.Collections.Immutable;
using Volo.Abp.Localization;
namespace Volo.Abp.Authorization.Permissions namespace Volo.Abp.Authorization.Permissions
{ {
@ -37,9 +38,9 @@ namespace Volo.Abp.Authorization.Permissions
_permissions = new List<PermissionDefinition>(); _permissions = new List<PermissionDefinition>();
} }
public virtual PermissionDefinition AddPermission(string name) public virtual PermissionDefinition AddPermission(string name, ILocalizableString displayName = null)
{ {
var permission = new PermissionDefinition(name); var permission = new PermissionDefinition(name, displayName);
_permissions.Add(permission); _permissions.Add(permission);

@ -0,0 +1,19 @@
using Microsoft.Extensions.Localization;
namespace Volo.Abp.Localization
{
public class FixedLocalizableString : ILocalizableString
{
public string Value { get; }
public FixedLocalizableString(string value)
{
Value = value;
}
public LocalizedString Localize(IStringLocalizerFactory stringLocalizerFactory)
{
return new LocalizedString(Value, Value);
}
}
}

@ -0,0 +1,9 @@
using Microsoft.Extensions.Localization;
namespace Volo.Abp.Localization
{
public interface ILocalizableString
{
LocalizedString Localize(IStringLocalizerFactory stringLocalizerFactory);
}
}

@ -0,0 +1,28 @@
using System;
using Microsoft.Extensions.Localization;
namespace Volo.Abp.Localization
{
public class LocalizableString : ILocalizableString
{
public Type ResourceType { get; }
public string Name { get; }
public LocalizableString(Type resourceType, string name)
{
ResourceType = resourceType;
Name = name;
}
public LocalizedString Localize(IStringLocalizerFactory stringLocalizerFactory)
{
return stringLocalizerFactory.Create(ResourceType)[Name];
}
public static LocalizableString Create<TResource>(string name)
{
return new LocalizableString(typeof(TResource), name);
}
}
}
Loading…
Cancel
Save