From 5648a1b515dbe148a6194f1e8c060bb9ca0e60a0 Mon Sep 17 00:00:00 2001 From: Ahmed Fwela Date: Fri, 22 Jul 2022 14:57:41 +0200 Subject: [PATCH 1/4] Improve IMultiLingualObjectManager --- .../IMultiLingualObjectManager.cs | 9 +++++- .../MultiLingualObjectManager.cs | 28 +++++++++++-------- .../MultiLingualObjectManager_Tests.cs | 11 ++++++++ 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/framework/src/Volo.Abp.MultiLingualObjects/Volo/Abp/MultiLingualObjects/IMultiLingualObjectManager.cs b/framework/src/Volo.Abp.MultiLingualObjects/Volo/Abp/MultiLingualObjects/IMultiLingualObjectManager.cs index 81b7646a1c..904f3abd32 100644 --- a/framework/src/Volo.Abp.MultiLingualObjects/Volo/Abp/MultiLingualObjects/IMultiLingualObjectManager.cs +++ b/framework/src/Volo.Abp.MultiLingualObjects/Volo/Abp/MultiLingualObjects/IMultiLingualObjectManager.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System.Collections.Generic; +using System.Threading.Tasks; namespace Volo.Abp.MultiLingualObjects; @@ -10,4 +11,10 @@ public interface IMultiLingualObjectManager bool fallbackToParentCultures = true) where TMultiLingual : IMultiLingualObject where TTranslation : class, IObjectTranslation; + + Task GetTranslationAsync( + ICollection translations, + string culture = null, + bool fallbackToParentCultures = true) + where TTranslation : class, IObjectTranslation; } diff --git a/framework/src/Volo.Abp.MultiLingualObjects/Volo/Abp/MultiLingualObjects/MultiLingualObjectManager.cs b/framework/src/Volo.Abp.MultiLingualObjects/Volo/Abp/MultiLingualObjects/MultiLingualObjectManager.cs index b60e137e76..c8739991b7 100644 --- a/framework/src/Volo.Abp.MultiLingualObjects/Volo/Abp/MultiLingualObjects/MultiLingualObjectManager.cs +++ b/framework/src/Volo.Abp.MultiLingualObjects/Volo/Abp/MultiLingualObjects/MultiLingualObjectManager.cs @@ -19,22 +19,18 @@ public class MultiLingualObjectManager : IMultiLingualObjectManager, ITransientD { SettingProvider = settingProvider; } - - public virtual async Task GetTranslationAsync( - TMultiLingual multiLingual, - string culture = null, - bool fallbackToParentCultures = true) - where TMultiLingual : IMultiLingualObject + public virtual async Task GetTranslationAsync(ICollection translations, string culture, bool fallbackToParentCultures) where TTranslation : class, IObjectTranslation + { culture ??= CultureInfo.CurrentUICulture.Name; - if (multiLingual.Translations.IsNullOrEmpty()) + if (translations.IsNullOrEmpty()) { return null; } - var translation = multiLingual.Translations.FirstOrDefault(pt => pt.Language == culture); + var translation = translations.FirstOrDefault(pt => pt.Language == culture); if (translation != null) { return translation; @@ -44,7 +40,7 @@ public class MultiLingualObjectManager : IMultiLingualObjectManager, ITransientD { translation = GetTranslationBasedOnCulturalRecursive( CultureInfo.CurrentUICulture.Parent, - multiLingual.Translations, + translations, 0 ); @@ -56,16 +52,26 @@ public class MultiLingualObjectManager : IMultiLingualObjectManager, ITransientD var defaultLanguage = await SettingProvider.GetOrNullAsync(LocalizationSettingNames.DefaultLanguage); - translation = multiLingual.Translations.FirstOrDefault(pt => pt.Language == defaultLanguage); + translation = translations.FirstOrDefault(pt => pt.Language == defaultLanguage); if (translation != null) { return translation; } - translation = multiLingual.Translations.FirstOrDefault(); + translation = translations.FirstOrDefault(); return translation; } + public virtual Task GetTranslationAsync( + TMultiLingual multiLingual, + string culture = null, + bool fallbackToParentCultures = true) + where TMultiLingual : IMultiLingualObject + where TTranslation : class, IObjectTranslation + { + return GetTranslationAsync(multiLingual.Translations, culture: culture, fallbackToParentCultures: fallbackToParentCultures); + } + protected virtual TTranslation GetTranslationBasedOnCulturalRecursive( CultureInfo culture, ICollection translations, int currentDepth) where TTranslation : class, IObjectTranslation diff --git a/framework/test/Volo.Abp.MultiLingualObjects.Tests/Volo/Abp/MultiLingualObjects/MultiLingualObjectManager_Tests.cs b/framework/test/Volo.Abp.MultiLingualObjects.Tests/Volo/Abp/MultiLingualObjects/MultiLingualObjectManager_Tests.cs index 1b26b06297..afe15bff99 100644 --- a/framework/test/Volo.Abp.MultiLingualObjects.Tests/Volo/Abp/MultiLingualObjects/MultiLingualObjectManager_Tests.cs +++ b/framework/test/Volo.Abp.MultiLingualObjects.Tests/Volo/Abp/MultiLingualObjects/MultiLingualObjectManager_Tests.cs @@ -51,6 +51,17 @@ public class MultiLingualObjectManager_Tests : AbpIntegratedTest Date: Fri, 22 Jul 2022 15:01:08 +0200 Subject: [PATCH 2/4] keep old format --- .../Abp/MultiLingualObjects/MultiLingualObjectManager.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/framework/src/Volo.Abp.MultiLingualObjects/Volo/Abp/MultiLingualObjects/MultiLingualObjectManager.cs b/framework/src/Volo.Abp.MultiLingualObjects/Volo/Abp/MultiLingualObjects/MultiLingualObjectManager.cs index c8739991b7..afb4509917 100644 --- a/framework/src/Volo.Abp.MultiLingualObjects/Volo/Abp/MultiLingualObjects/MultiLingualObjectManager.cs +++ b/framework/src/Volo.Abp.MultiLingualObjects/Volo/Abp/MultiLingualObjects/MultiLingualObjectManager.cs @@ -19,7 +19,10 @@ public class MultiLingualObjectManager : IMultiLingualObjectManager, ITransientD { SettingProvider = settingProvider; } - public virtual async Task GetTranslationAsync(ICollection translations, string culture, bool fallbackToParentCultures) + public virtual async Task GetTranslationAsync( + ICollection translations, + string culture, + bool fallbackToParentCultures) where TTranslation : class, IObjectTranslation { From acb7620b5e4a1a588cecfabf293b664f73f7dde2 Mon Sep 17 00:00:00 2001 From: maliming Date: Tue, 26 Jul 2022 15:57:27 +0800 Subject: [PATCH 3/4] Update AbpExceptionFilter.cs --- .../Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs index 7b5e2aee2e..fdaf0e8ff1 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs @@ -20,7 +20,7 @@ namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling; public class AbpExceptionFilter : IAsyncExceptionFilter, ITransientDependency { - public async Task OnExceptionAsync(ExceptionContext context) + public virtual async Task OnExceptionAsync(ExceptionContext context) { if (!ShouldHandleException(context)) { From fc1e7956eb428f67564371238729e2cd3d7e5a22 Mon Sep 17 00:00:00 2001 From: maliming Date: Tue, 26 Jul 2022 15:57:41 +0800 Subject: [PATCH 4/4] Update AbpExceptionPageFilter.cs --- .../AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs index eb335d6fd9..c60a00bbe9 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs @@ -25,7 +25,7 @@ public class AbpExceptionPageFilter : IAsyncPageFilter, ITransientDependency return Task.CompletedTask; } - public async Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next) + public virtual async Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next) { if (context.HandlerMethod == null || !ShouldHandleException(context)) {