30 changed files with 327 additions and 30 deletions
@ -0,0 +1,14 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
|
<Import Project="..\..\..\common.props" /> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<TargetFramework>net5.0</TargetFramework> |
||||
|
<RootNamespace /> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<PackageReference Include="Volo.Abp.AspNetCore" Version="4.4.0" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
||||
@ -0,0 +1,62 @@ |
|||||
|
using Microsoft.AspNetCore.Http; |
||||
|
using Microsoft.AspNetCore.Localization; |
||||
|
using Microsoft.AspNetCore.RequestLocalization; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using Microsoft.Extensions.Primitives; |
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace LINGYUN.Abp.Localization.CultureMap |
||||
|
{ |
||||
|
public class AbpCultureMapRequestCultureProvider : RequestCultureProvider |
||||
|
{ |
||||
|
public override async Task<ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext) |
||||
|
{ |
||||
|
if (httpContext == null) |
||||
|
{ |
||||
|
throw new ArgumentNullException(nameof(httpContext)); |
||||
|
} |
||||
|
|
||||
|
var option = httpContext.RequestServices.GetRequiredService<IOptions<AbpLocalizationCultureMapOptions>>().Value; |
||||
|
|
||||
|
var mapCultures = new List<StringSegment>(); |
||||
|
var mapUiCultures = new List<StringSegment>(); |
||||
|
|
||||
|
var requestLocalizationOptionsProvider = httpContext.RequestServices.GetRequiredService<IAbpRequestLocalizationOptionsProvider>(); |
||||
|
foreach (var provider in (await requestLocalizationOptionsProvider.GetLocalizationOptionsAsync()).RequestCultureProviders) |
||||
|
{ |
||||
|
if (provider == this) |
||||
|
{ |
||||
|
continue; |
||||
|
} |
||||
|
|
||||
|
var providerCultureResult = await provider.DetermineProviderCultureResult(httpContext); |
||||
|
if (providerCultureResult == null) |
||||
|
{ |
||||
|
continue; |
||||
|
} |
||||
|
|
||||
|
mapCultures.AddRange(providerCultureResult.Cultures.Where(x => x.HasValue) |
||||
|
.Select(culture => |
||||
|
{ |
||||
|
var map = option.CulturesMaps.FirstOrDefault(x => |
||||
|
x.SourceCultures.Contains(culture.Value, StringComparer.OrdinalIgnoreCase)); |
||||
|
return new StringSegment(map?.TargetCulture ?? culture.Value); |
||||
|
})); |
||||
|
|
||||
|
mapUiCultures.AddRange(providerCultureResult.UICultures.Where(x => x.HasValue) |
||||
|
.Select(culture => |
||||
|
{ |
||||
|
var map = option.UiCulturesMaps.FirstOrDefault(x => |
||||
|
x.SourceCultures.Contains(culture.Value, StringComparer.OrdinalIgnoreCase)); |
||||
|
return new StringSegment(map?.TargetCulture ?? culture.Value); |
||||
|
})); |
||||
|
} |
||||
|
|
||||
|
return new ProviderCultureResult(mapCultures, mapUiCultures); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,10 @@ |
|||||
|
using Volo.Abp.AspNetCore; |
||||
|
using Volo.Abp.Modularity; |
||||
|
|
||||
|
namespace LINGYUN.Abp.Localization.CultureMap |
||||
|
{ |
||||
|
[DependsOn(typeof(AbpAspNetCoreModule))] |
||||
|
public class AbpLocalizationCultureMapModule : AbpModule |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,17 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
namespace LINGYUN.Abp.Localization.CultureMap |
||||
|
{ |
||||
|
public class AbpLocalizationCultureMapOptions |
||||
|
{ |
||||
|
public List<CultureMapInfo> CulturesMaps { get; } |
||||
|
|
||||
|
public List<CultureMapInfo> UiCulturesMaps { get; } |
||||
|
|
||||
|
public AbpLocalizationCultureMapOptions() |
||||
|
{ |
||||
|
CulturesMaps = new List<CultureMapInfo>(); |
||||
|
UiCulturesMaps = new List<CultureMapInfo>(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
namespace LINGYUN.Abp.Localization.CultureMap |
||||
|
{ |
||||
|
public class CultureMapInfo |
||||
|
{ |
||||
|
public string TargetCulture { get; set; } |
||||
|
|
||||
|
public string[] SourceCultures { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,19 @@ |
|||||
|
using LINGYUN.Abp.Localization.CultureMap; |
||||
|
using System; |
||||
|
|
||||
|
namespace Microsoft.AspNetCore.Builder |
||||
|
{ |
||||
|
public static class AbpCultureMapApplicationBuilderExtensions |
||||
|
{ |
||||
|
public static IApplicationBuilder UseMapRequestLocalization( |
||||
|
this IApplicationBuilder app, |
||||
|
Action<RequestLocalizationOptions> optionsAction = null) |
||||
|
{ |
||||
|
return app.UseAbpRequestLocalization(options => |
||||
|
{ |
||||
|
options.RequestCultureProviders.Insert(0, new AbpCultureMapRequestCultureProvider()); |
||||
|
optionsAction?.Invoke(options); |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,53 @@ |
|||||
|
# LINGYUN.Abp.Localization.CultureMap |
||||
|
|
||||
|
## 模块说明 |
||||
|
|
||||
|
解决存在多种格式的区域性本地化问题 |
||||
|
|
||||
|
See: https://github.com/maliming/Owl.Abp.CultureMap |
||||
|
|
||||
|
### 基础模块 |
||||
|
|
||||
|
### 高阶模块 |
||||
|
|
||||
|
### 权限定义 |
||||
|
|
||||
|
### 功能定义 |
||||
|
|
||||
|
### 配置定义 |
||||
|
|
||||
|
### 如何使用 |
||||
|
|
||||
|
|
||||
|
```csharp |
||||
|
|
||||
|
[DependsOn( |
||||
|
typeof(AbpLocalizationCultureMapModule))] |
||||
|
public class YouProjectModule : AbpModule |
||||
|
{ |
||||
|
public override void ConfigureServices(ServiceConfigurationContext context) |
||||
|
{ |
||||
|
Configure<AbpLocalizationCultureMapOptions>(options => |
||||
|
{ |
||||
|
var zhHansCultureMapInfo = new CultureMapInfo |
||||
|
{ |
||||
|
TargetCulture = "zh-Hans", |
||||
|
SourceCultures = new string[] { "zh", "zh_CN", "zh-CN" } |
||||
|
}; |
||||
|
|
||||
|
options.CulturesMaps.Add(zhHansCultureMapInfo); |
||||
|
options.UiCulturesMaps.Add(zhHansCultureMapInfo); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
public override void OnApplicationInitialization(ApplicationInitializationContext context) |
||||
|
{ |
||||
|
var app = context.GetApplicationBuilder(); |
||||
|
|
||||
|
app.UseMapRequestLocalization(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
``` |
||||
|
|
||||
|
### 更新日志 |
||||
Loading…
Reference in new issue