全球化与本地化
This commit is contained in:
1 parent
6fef1a9ea0
commit
77b9f64039
15 files changed
+649
-7
No files matched your search
@@ -0,0 +1,53 @@
|
||||
using FluentValidation;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Localization;
|
||||
using System.ComponentModel;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Reflection;
|
||||
|
||||
namespace HelloShop.ServiceDefaults.Extensions;
|
||||
|
||||
public static class LocalizationExtensions
|
||||
{
|
||||
public static IServiceCollection AddCustomLocalization(this IServiceCollection services)
|
||||
{
|
||||
services.AddLocalization(options => options.ResourcesPath = "Resources");
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IApplicationBuilder UseCustomLocalization(this IApplicationBuilder app)
|
||||
{
|
||||
var supportedCultures = new[] { "zh-CN", "en-US" };
|
||||
|
||||
var localizationOptions = new RequestLocalizationOptions().SetDefaultCulture(supportedCultures.First())
|
||||
.AddSupportedCultures(supportedCultures)
|
||||
.AddSupportedUICultures(supportedCultures);
|
||||
|
||||
app.UseRequestLocalization(localizationOptions);
|
||||
|
||||
IStringLocalizerFactory localizerFactory = app.ApplicationServices.GetRequiredService<IStringLocalizerFactory>();
|
||||
|
||||
ValidatorOptions.Global.DisplayNameResolver = (type, memberInfo, lambdaExpression) =>
|
||||
{
|
||||
string displayName = memberInfo.Name;
|
||||
|
||||
DisplayAttribute? displayAttribute = memberInfo.GetCustomAttribute<DisplayAttribute>(true);
|
||||
|
||||
displayName = displayAttribute?.Name ?? displayName;
|
||||
|
||||
DisplayNameAttribute? displayNameAttribute = memberInfo.GetCustomAttribute<DisplayNameAttribute>(true);
|
||||
|
||||
displayName = displayNameAttribute?.DisplayName ?? displayName;
|
||||
|
||||
var localizer = localizerFactory.Create(type);
|
||||
|
||||
return localizer[displayName];
|
||||
};
|
||||
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||
// See the license file in the project root for more information.
|
||||
|
||||
using HelloShop.ServiceDefaults.Infrastructure;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.OpenApi.Models;
|
||||
@@ -25,6 +26,8 @@ namespace HelloShop.ServiceDefaults.Extensions
|
||||
|
||||
services.Configure<SwaggerGenOptions>(options =>
|
||||
{
|
||||
options.OperationFilter<AcceptLanguageHeaderOperationFilter>();
|
||||
|
||||
options.AddSecurityDefinition("bearerAuth", new OpenApiSecurityScheme
|
||||
{
|
||||
Type = SecuritySchemeType.Http,
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
using Microsoft.Net.Http.Headers;
|
||||
using Microsoft.OpenApi.Any;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
|
||||
namespace HelloShop.ServiceDefaults.Infrastructure;
|
||||
|
||||
public class AcceptLanguageHeaderOperationFilter : IOperationFilter
|
||||
{
|
||||
public void Apply(OpenApiOperation operation, OperationFilterContext context)
|
||||
{
|
||||
var parameter = new OpenApiParameter
|
||||
{
|
||||
Name = HeaderNames.AcceptLanguage,
|
||||
In = ParameterLocation.Header,
|
||||
Required = false,
|
||||
Schema = new OpenApiSchema
|
||||
{
|
||||
Default = new OpenApiString("zh-CN"),
|
||||
Type = "string",
|
||||
Enum = [new OpenApiString("zh-CN"), new OpenApiString("en-US")]
|
||||
}
|
||||
};
|
||||
|
||||
operation.Parameters.Add(parameter);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user