From 796fe8e158a05a1e1931f9386af794509fb20283 Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 14 Mar 2022 14:36:06 +0800 Subject: [PATCH] Cache localization keys. Resolve #11887 --- ...AutoLocalizationMetadataDetailsProvider.cs | 61 +++++++++++++------ 1 file changed, 43 insertions(+), 18 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpDataAnnotationAutoLocalizationMetadataDetailsProvider.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpDataAnnotationAutoLocalizationMetadataDetailsProvider.cs index 397c59a407..cb0d9547a2 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpDataAnnotationAutoLocalizationMetadataDetailsProvider.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpDataAnnotationAutoLocalizationMetadataDetailsProvider.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; @@ -16,14 +17,16 @@ public class AbpDataAnnotationAutoLocalizationMetadataDetailsProvider : IDisplay private readonly Lazy _stringLocalizerFactory; private readonly Lazy> _localizationOptions; + private readonly ConcurrentDictionary _displayNameKeys; public AbpDataAnnotationAutoLocalizationMetadataDetailsProvider(IServiceCollection services) { _stringLocalizerFactory = services.GetRequiredServiceLazy(); _localizationOptions = services.GetRequiredServiceLazy>(); + _displayNameKeys = new ConcurrentDictionary(); } - public void CreateDisplayMetadata(DisplayMetadataProviderContext context) + public virtual void CreateDisplayMetadata(DisplayMetadataProviderContext context) { var displayMetadata = context.DisplayMetadata; if (displayMetadata.DisplayName != null) @@ -54,36 +57,58 @@ public class AbpDataAnnotationAutoLocalizationMetadataDetailsProvider : IDisplay displayMetadata.DisplayName = () => { - /* - * DisplayName:ClassName:PropertyName - * DisplayName:PropertyName - * ClassName:PropertyName - * PropertyName - */ + var key = _displayNameKeys.GetOrAdd(context, _ => GetDisplayNameKey(context, localizer)); + return key != null ? localizer[key] : null; + }; + } + + protected virtual string GetDisplayNameKey(DisplayMetadataProviderContext context, IStringLocalizer localizer) + { + /* + * DisplayName:ClassName:PropertyName + * DisplayName:PropertyName + * ClassName:PropertyName + * PropertyName + */ - LocalizedString localizedString = null; + LocalizedString localizedString = null; - if (context.Key.ContainerType != null) + if (context.Key.ContainerType != null) + { + localizedString = localizer[PropertyLocalizationKeyPrefix + context.Key.ContainerType.Name + ":" + context.Key.Name]; + if (!localizedString.ResourceNotFound) { - localizedString = localizer[PropertyLocalizationKeyPrefix + context.Key.ContainerType.Name + ":" + context.Key.Name]; + return localizedString.Name; } + } - if (localizedString == null || localizedString.ResourceNotFound) + if (localizedString == null || localizedString.ResourceNotFound) + { + localizedString = localizer[PropertyLocalizationKeyPrefix + context.Key.Name]; + if (!localizedString.ResourceNotFound) { - localizedString = localizer[PropertyLocalizationKeyPrefix + context.Key.Name]; + return localizedString.Name; } + } - if (localizedString.ResourceNotFound && context.Key.ContainerType != null) + if (localizedString.ResourceNotFound && context.Key.ContainerType != null) + { + localizedString = localizer[context.Key.ContainerType.Name + ":" + context.Key.Name]; + if (!localizedString.ResourceNotFound) { - localizedString = localizer[context.Key.ContainerType.Name + ":" + context.Key.Name]; + return localizedString.Name; } + } - if (localizedString.ResourceNotFound) + if (localizedString.ResourceNotFound) + { + localizedString = localizer[context.Key.Name]; + if (!localizedString.ResourceNotFound) { - localizedString = localizer[context.Key.Name]; + return localizedString.Name; } + } - return localizedString; - }; + return null; } }