From 41240d2cfeeeb09f957e214ccce931aca5f442ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 3 May 2018 11:55:41 +0300 Subject: [PATCH] Define localizable string --- .../Volo.Abp.Authorization.csproj | 1 + .../Authorization/AbpAuthorizationModule.cs | 6 +++- .../Permissions/PermissionDefinition.cs | 13 ++++++--- .../Permissions/PermissionGroupDefinition.cs | 5 ++-- .../Localization/FixedLocalizableString.cs | 19 +++++++++++++ .../Abp/Localization/ILocalizableString.cs | 9 ++++++ .../Abp/Localization/LocalizableString.cs | 28 +++++++++++++++++++ 7 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 src/Volo.Abp.Localization/Volo/Abp/Localization/FixedLocalizableString.cs create mode 100644 src/Volo.Abp.Localization/Volo/Abp/Localization/ILocalizableString.cs create mode 100644 src/Volo.Abp.Localization/Volo/Abp/Localization/LocalizableString.cs diff --git a/src/Volo.Abp.Authorization/Volo.Abp.Authorization.csproj b/src/Volo.Abp.Authorization/Volo.Abp.Authorization.csproj index 34e22cdf4c..7065a2c374 100644 --- a/src/Volo.Abp.Authorization/Volo.Abp.Authorization.csproj +++ b/src/Volo.Abp.Authorization/Volo.Abp.Authorization.csproj @@ -18,6 +18,7 @@ + diff --git a/src/Volo.Abp.Authorization/Volo/Abp/Authorization/AbpAuthorizationModule.cs b/src/Volo.Abp.Authorization/Volo/Abp/Authorization/AbpAuthorizationModule.cs index 3ddf6e1132..bb4c941da7 100644 --- a/src/Volo.Abp.Authorization/Volo/Abp/Authorization/AbpAuthorizationModule.cs +++ b/src/Volo.Abp.Authorization/Volo/Abp/Authorization/AbpAuthorizationModule.cs @@ -1,12 +1,16 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Authorization.Permissions; +using Volo.Abp.Localization; using Volo.Abp.Modularity; using Volo.Abp.Security; namespace Volo.Abp.Authorization { - [DependsOn(typeof(AbpSecurityModule))] + [DependsOn( + typeof(AbpSecurityModule), + typeof(AbpLocalizationModule) + )] public class AbpAuthorizationModule : AbpModule { public override void PreConfigureServices(IServiceCollection services) diff --git a/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/PermissionDefinition.cs b/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/PermissionDefinition.cs index dddfcce41c..082cfd5622 100644 --- a/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/PermissionDefinition.cs +++ b/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/PermissionDefinition.cs @@ -1,5 +1,7 @@ using System.Collections.Generic; using System.Collections.Immutable; +using JetBrains.Annotations; +using Volo.Abp.Localization; namespace Volo.Abp.Authorization.Permissions { @@ -16,6 +18,8 @@ namespace Volo.Abp.Authorization.Permissions /// public PermissionDefinition Parent { get; private set; } + public ILocalizableString DisplayName { get; set; } + public IReadOnlyList Children => _children.ToImmutableList(); private readonly List _children; @@ -35,17 +39,18 @@ namespace Volo.Abp.Authorization.Permissions 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(); _children = new List(); } - 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 }; diff --git a/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/PermissionGroupDefinition.cs b/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/PermissionGroupDefinition.cs index 6e88379882..ca8a61899c 100644 --- a/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/PermissionGroupDefinition.cs +++ b/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/PermissionGroupDefinition.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Collections.Immutable; +using Volo.Abp.Localization; namespace Volo.Abp.Authorization.Permissions { @@ -37,9 +38,9 @@ namespace Volo.Abp.Authorization.Permissions _permissions = new List(); } - 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); diff --git a/src/Volo.Abp.Localization/Volo/Abp/Localization/FixedLocalizableString.cs b/src/Volo.Abp.Localization/Volo/Abp/Localization/FixedLocalizableString.cs new file mode 100644 index 0000000000..725ac01709 --- /dev/null +++ b/src/Volo.Abp.Localization/Volo/Abp/Localization/FixedLocalizableString.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/src/Volo.Abp.Localization/Volo/Abp/Localization/ILocalizableString.cs b/src/Volo.Abp.Localization/Volo/Abp/Localization/ILocalizableString.cs new file mode 100644 index 0000000000..ffe11bcda2 --- /dev/null +++ b/src/Volo.Abp.Localization/Volo/Abp/Localization/ILocalizableString.cs @@ -0,0 +1,9 @@ +using Microsoft.Extensions.Localization; + +namespace Volo.Abp.Localization +{ + public interface ILocalizableString + { + LocalizedString Localize(IStringLocalizerFactory stringLocalizerFactory); + } +} \ No newline at end of file diff --git a/src/Volo.Abp.Localization/Volo/Abp/Localization/LocalizableString.cs b/src/Volo.Abp.Localization/Volo/Abp/Localization/LocalizableString.cs new file mode 100644 index 0000000000..6698ba4645 --- /dev/null +++ b/src/Volo.Abp.Localization/Volo/Abp/Localization/LocalizableString.cs @@ -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(string name) + { + return new LocalizableString(typeof(TResource), name); + } + } +} \ No newline at end of file