From 5e70f9051d35ed8b739312dd10401d3302357ae8 Mon Sep 17 00:00:00 2001 From: wangjun Date: Thu, 29 Jun 2023 21:43:51 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=B5=8F=E8=A7=88?= =?UTF-8?q?=E5=99=A8=E4=BC=A0=E9=80=92=E7=9A=84=E5=A4=9A=E8=AF=AD=E8=A8=80?= =?UTF-8?q?=E5=92=8CAbp=E5=A4=9A=E8=AF=AD=E8=A8=80=E6=98=A0=E5=B0=84=20#97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Seeds/AbpSettingDataSeedContributor.cs | 16 +++++----- ...asicManagementSettingDefinitionProvider.cs | 5 ++++ .../AbpProHttpApiHostModule.cs | 8 ++++- .../GlobalUsings.cs | 2 ++ .../AbpPro/SharedHostingMicroserviceModule.cs | 11 ++----- .../Builder/ApplicationBuilderExtensions.cs | 23 +++++++++++++++ ...eptLanguageHeaderRequestCultureProvider.cs | 29 +++++++++++++++++++ 7 files changed, 77 insertions(+), 17 deletions(-) create mode 100644 aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Microsoft/AspNetCore/Localization/LionAcceptLanguageHeaderRequestCultureProvider.cs diff --git a/aspnet-core/modules/BasicManagement/src/Lion.AbpPro.BasicManagement.Domain/Data/Seeds/AbpSettingDataSeedContributor.cs b/aspnet-core/modules/BasicManagement/src/Lion.AbpPro.BasicManagement.Domain/Data/Seeds/AbpSettingDataSeedContributor.cs index ce4fc26ed..c8e3e3f2e 100644 --- a/aspnet-core/modules/BasicManagement/src/Lion.AbpPro.BasicManagement.Domain/Data/Seeds/AbpSettingDataSeedContributor.cs +++ b/aspnet-core/modules/BasicManagement/src/Lion.AbpPro.BasicManagement.Domain/Data/Seeds/AbpSettingDataSeedContributor.cs @@ -1,22 +1,24 @@ using Volo.Abp.Data; using Volo.Abp.DependencyInjection; +using Volo.Abp.Guids; +using Volo.Abp.Localization; namespace Lion.AbpPro.BasicManagement.Data.Seeds { public class AbpSettingDataSeedContributor : IDataSeedContributor, ITransientDependency { - private readonly ISettingManager _settingManager; - private const string DefaultLanguageKey = "Abp.Localization.DefaultLanguage"; - private const string DefaultLanguage = "zh-Hans"; - public AbpSettingDataSeedContributor(ISettingManager settingManager) + private const string Value = "zh-Hans"; + private const string ProviderName = "G"; + private readonly ISettingManagementStore _settingManagementStore; + + public AbpSettingDataSeedContributor(ISettingManagementStore settingManagementStore) { - _settingManager = settingManager; + _settingManagementStore = settingManagementStore; } public async Task SeedAsync(DataSeedContext context) { - // 设置默认语言 - await _settingManager.SetGlobalAsync(DefaultLanguageKey, DefaultLanguage); + await _settingManagementStore.SetAsync(LocalizationSettingNames.DefaultLanguage, Value, ProviderName, null); } } } \ No newline at end of file diff --git a/aspnet-core/modules/BasicManagement/src/Lion.AbpPro.BasicManagement.Domain/Settings/BasicManagementSettingDefinitionProvider.cs b/aspnet-core/modules/BasicManagement/src/Lion.AbpPro.BasicManagement.Domain/Settings/BasicManagementSettingDefinitionProvider.cs index fae9db6fa..56c541923 100644 --- a/aspnet-core/modules/BasicManagement/src/Lion.AbpPro.BasicManagement.Domain/Settings/BasicManagementSettingDefinitionProvider.cs +++ b/aspnet-core/modules/BasicManagement/src/Lion.AbpPro.BasicManagement.Domain/Settings/BasicManagementSettingDefinitionProvider.cs @@ -19,6 +19,11 @@ public override void Define(ISettingDefinitionContext context) /// private static void OverrideDefaultSettings(ISettingDefinitionContext context) { + + context.GetOrNull(LocalizationSettingNames.DefaultLanguage) + .WithProperty(BasicManagementSettings.Group.Default, BasicManagementSettings.Group.SystemManagement) + .WithProperty(BasicManagementSettings.ControlType.Default, BasicManagementSettings.ControlType.TypeText); + context.Add( new SettingDefinition(TimingSettingNames.TimeZone, "China Standard Time", diff --git a/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/AbpProHttpApiHostModule.cs b/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/AbpProHttpApiHostModule.cs index ad27a4574..36c218c1c 100644 --- a/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/AbpProHttpApiHostModule.cs +++ b/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/AbpProHttpApiHostModule.cs @@ -1,5 +1,6 @@ using Hangfire.Redis; using Lion.AbpPro.CAP.EntityFrameworkCore; +using Microsoft.AspNetCore.Localization; using Swagger; using Volo.Abp.BackgroundJobs.Hangfire; using Volo.Abp.Timing; @@ -42,13 +43,18 @@ public override void ConfigureServices(ServiceConfigurationContext context) ConfigureCap(context); ConfigureAuditLog(context); ConfigurationSignalR(context); + context.Services.Configure(options => + { + //options.RequestCultureProviders.RemoveAll(provider => provider is AcceptLanguageHeaderRequestCultureProvider); + options.RequestCultureProviders.Add(new LionAcceptLanguageHeaderRequestCultureProvider()); + }); } public override void OnApplicationInitialization(ApplicationInitializationContext context) { var app = context.GetApplicationBuilder(); var configuration = context.GetConfiguration(); - app.UseAbpRequestLocalization(); + app.UseLionRequestLocalization(); app.UseCorrelationId(); app.UseStaticFiles(); if (configuration.GetValue("MiniProfiler:Enabled", false)) diff --git a/aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/GlobalUsings.cs b/aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/GlobalUsings.cs index d64e89074..a39b17112 100644 --- a/aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/GlobalUsings.cs +++ b/aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/GlobalUsings.cs @@ -12,6 +12,7 @@ global using Microsoft.AspNetCore.Builder; global using Microsoft.AspNetCore.Cors; global using Microsoft.AspNetCore.Http; +global using Microsoft.AspNetCore.Localization; global using Microsoft.AspNetCore.Mvc; global using Microsoft.AspNetCore.Mvc.Abstractions; global using Microsoft.AspNetCore.Mvc.ApiExplorer; @@ -22,6 +23,7 @@ global using Microsoft.Extensions.Logging; global using Microsoft.Extensions.Logging.Abstractions; global using Microsoft.Extensions.Options; +global using Microsoft.Extensions.Primitives; global using Microsoft.OpenApi.Any; global using Microsoft.OpenApi.Models; global using Serilog; diff --git a/aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Lion/AbpPro/SharedHostingMicroserviceModule.cs b/aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Lion/AbpPro/SharedHostingMicroserviceModule.cs index 0dbdd91f3..9e317a134 100644 --- a/aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Lion/AbpPro/SharedHostingMicroserviceModule.cs +++ b/aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Lion/AbpPro/SharedHostingMicroserviceModule.cs @@ -24,13 +24,7 @@ public override void ConfigureServices(ServiceConfigurationContext context) ConfigAntiForgery(); ConfigureAbpExceptions(context); } - - public override void OnApplicationInitialization(ApplicationInitializationContext context) - { - // 设置默认语言为简体中文 - context.ServiceProvider.GetRequiredService().Get(LocalizationSettingNames.DefaultLanguage).DefaultValue = "zh-Hans"; - } - + /// /// 异常处理 /// @@ -55,7 +49,7 @@ private void ConfigAntiForgery() Configure(options => { options.AutoValidate = false; }); } - private void ConfigureConsul(ServiceConfigurationContext context,IConfiguration configuration) + private void ConfigureConsul(ServiceConfigurationContext context, IConfiguration configuration) { if (configuration.GetValue("Consul:Enabled", false)) { @@ -94,7 +88,6 @@ private void ConfigureCors(ServiceConfigurationContext context) } - /// /// 多语言配置 /// diff --git a/aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Microsoft/AspNetCore/Builder/ApplicationBuilderExtensions.cs b/aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Microsoft/AspNetCore/Builder/ApplicationBuilderExtensions.cs index c84612334..8be56a2a0 100644 --- a/aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Microsoft/AspNetCore/Builder/ApplicationBuilderExtensions.cs +++ b/aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Microsoft/AspNetCore/Builder/ApplicationBuilderExtensions.cs @@ -52,5 +52,28 @@ public static IApplicationBuilder UseRequestLog(this IApplicationBuilder app) { return app.UseMiddleware(); } + + /// + /// 多语言中间件 + /// 浏览器传递的请求头:Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6而abp钟简体中文为:zh-Hans + /// + /// app.UseLionRequestLocalization(); + /// + /// + public static IApplicationBuilder UseLionRequestLocalization(this IApplicationBuilder app) + { + if (app == null) + { + throw new ArgumentNullException(nameof(app)); + } + + return app.UseAbpRequestLocalization(options => + { + // 移除自带header解析器 + options.RequestCultureProviders.RemoveAll(provider=> provider is AcceptLanguageHeaderRequestCultureProvider); + // 添加header解析器 + options.RequestCultureProviders.Add(new LionAcceptLanguageHeaderRequestCultureProvider()); + }); + } } } \ No newline at end of file diff --git a/aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Microsoft/AspNetCore/Localization/LionAcceptLanguageHeaderRequestCultureProvider.cs b/aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Microsoft/AspNetCore/Localization/LionAcceptLanguageHeaderRequestCultureProvider.cs new file mode 100644 index 000000000..8719fc1a7 --- /dev/null +++ b/aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Microsoft/AspNetCore/Localization/LionAcceptLanguageHeaderRequestCultureProvider.cs @@ -0,0 +1,29 @@ +namespace Microsoft.AspNetCore.Localization; + +public class LionAcceptLanguageHeaderRequestCultureProvider : AcceptLanguageHeaderRequestCultureProvider +{ + public override async Task DetermineProviderCultureResult(HttpContext httpContext) + { + var result = await base.DetermineProviderCultureResult(httpContext); + + try + { + if (result == null || result.Cultures.Count <= 0) return result; + + var culture = result.Cultures.First(); + + // 判断是否以 zh-CN(浏览器默认),zh-HK(中国香港),zh-MO(中国澳门),zh-TW(中国台湾)区域开头,如果是一律采用简体中文 + if (culture.Buffer != null && (culture.Buffer.StartsWith("zh-CN") || culture.Buffer.StartsWith("zh-HK") || culture.Buffer.StartsWith("zh-TW") || culture.Buffer.StartsWith("zh-MO"))) + { + culture = new StringSegment("zh-Hans"); + return new ProviderCultureResult(culture.Buffer, culture.Buffer); + } + } + catch + { + // ignore + } + + return result; + } +} \ No newline at end of file