Skip to content

Commit

Permalink
fix: 修复浏览器传递的多语言和Abp多语言映射 #97
Browse files Browse the repository at this point in the history
  • Loading branch information
WangJunZzz committed Jun 29, 2023
1 parent 37e5752 commit 5e70f90
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public override void Define(ISettingDefinitionContext context)
/// </summary>
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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -42,13 +43,18 @@ public override void ConfigureServices(ServiceConfigurationContext context)
ConfigureCap(context);
ConfigureAuditLog(context);
ConfigurationSignalR(context);
context.Services.Configure<RequestLocalizationOptions>(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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,7 @@ public override void ConfigureServices(ServiceConfigurationContext context)
ConfigAntiForgery();
ConfigureAbpExceptions(context);
}

public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
// 设置默认语言为简体中文
context.ServiceProvider.GetRequiredService<ISettingDefinitionManager>().Get(LocalizationSettingNames.DefaultLanguage).DefaultValue = "zh-Hans";
}


/// <summary>
/// 异常处理
/// </summary>
Expand All @@ -55,7 +49,7 @@ private void ConfigAntiForgery()
Configure<AbpAntiForgeryOptions>(options => { options.AutoValidate = false; });
}

private void ConfigureConsul(ServiceConfigurationContext context,IConfiguration configuration)
private void ConfigureConsul(ServiceConfigurationContext context, IConfiguration configuration)
{
if (configuration.GetValue<bool>("Consul:Enabled", false))
{
Expand Down Expand Up @@ -94,7 +88,6 @@ private void ConfigureCors(ServiceConfigurationContext context)
}



/// <summary>
/// 多语言配置
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,28 @@ public static IApplicationBuilder UseRequestLog(this IApplicationBuilder app)
{
return app.UseMiddleware<RequestLogMiddleware>();
}

/// <summary>
/// 多语言中间件
/// <remarks>浏览器传递的请求头: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</remarks>
/// <example>
/// app.UseLionRequestLocalization();
/// </example>
/// </summary>
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());
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace Microsoft.AspNetCore.Localization;

public class LionAcceptLanguageHeaderRequestCultureProvider : AcceptLanguageHeaderRequestCultureProvider
{
public override async Task<ProviderCultureResult> 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;
}
}

0 comments on commit 5e70f90

Please sign in to comment.