feat: establish i18n foundation
This commit is contained in:
1 parent
3e7d6e43d6
commit
c7499f05e3
20 files changed
+520
-28
No files matched your search
@@ -0,0 +1,29 @@
|
||||
using System.Globalization;
|
||||
|
||||
namespace Jiaowu.Api.Infrastructure.Localization;
|
||||
|
||||
public static class AppCultures
|
||||
{
|
||||
public const string Default = "zh-CN";
|
||||
public const string English = "en-US";
|
||||
|
||||
public static IReadOnlyList<CultureInfo> Supported { get; } =
|
||||
[
|
||||
CultureInfo.GetCultureInfo(Default),
|
||||
CultureInfo.GetCultureInfo(English)
|
||||
];
|
||||
|
||||
public static string Normalize(string? value)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(value)) return Default;
|
||||
var normalized = value.Replace('_', '-').Trim();
|
||||
if (normalized.Equals("zh", StringComparison.OrdinalIgnoreCase) ||
|
||||
normalized.StartsWith("zh-CN", StringComparison.OrdinalIgnoreCase) ||
|
||||
normalized.StartsWith("zh-Hans", StringComparison.OrdinalIgnoreCase))
|
||||
return Default;
|
||||
if (normalized.Equals("en", StringComparison.OrdinalIgnoreCase) ||
|
||||
normalized.StartsWith("en-", StringComparison.OrdinalIgnoreCase))
|
||||
return English;
|
||||
return Default;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Jiaowu.Api.Infrastructure.Localization;
|
||||
|
||||
/// <summary>
|
||||
/// Marker type for application-wide localized server messages.
|
||||
/// </summary>
|
||||
public sealed class SharedResource;
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Text;
|
||||
using System.Globalization;
|
||||
using System.Net;
|
||||
using System.Net.Mail;
|
||||
using System.Security.Claims;
|
||||
@@ -17,6 +18,7 @@ using Jiaowu.Api.Infrastructure.Caching;
|
||||
using Jiaowu.Api.Infrastructure.Exams;
|
||||
using Jiaowu.Api.Infrastructure.Facilities;
|
||||
using Jiaowu.Api.Infrastructure.Middleware;
|
||||
using Jiaowu.Api.Infrastructure.Localization;
|
||||
using Jiaowu.Api.Infrastructure.Observability;
|
||||
using Jiaowu.Api.Infrastructure.Notifications;
|
||||
using Jiaowu.Api.Infrastructure.OfficialDocuments;
|
||||
@@ -36,11 +38,13 @@ using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.HttpOverrides;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.RateLimiting;
|
||||
using Microsoft.AspNetCore.Localization;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using Microsoft.OpenApi;
|
||||
using Microsoft.Extensions.Localization;
|
||||
using Swashbuckle.AspNetCore.SwaggerUI;
|
||||
using System.Threading.RateLimiting;
|
||||
|
||||
@@ -77,6 +81,17 @@ if (confirmProductionDemoData && !seedDemoData)
|
||||
}
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
|
||||
builder.Services.Configure<RequestLocalizationOptions>(options =>
|
||||
{
|
||||
options.DefaultRequestCulture = new RequestCulture(AppCultures.Default);
|
||||
options.SupportedCultures = [.. AppCultures.Supported];
|
||||
options.SupportedUICultures = [.. AppCultures.Supported];
|
||||
options.RequestCultureProviders =
|
||||
[
|
||||
new AcceptLanguageHeaderRequestCultureProvider()
|
||||
];
|
||||
});
|
||||
var swaggerDocumentVersion = typeof(Program).Assembly
|
||||
.GetCustomAttributes(
|
||||
typeof(System.Reflection.AssemblyMetadataAttribute),
|
||||
@@ -768,7 +783,15 @@ builder.Services.AddCors(options =>
|
||||
});
|
||||
|
||||
builder.Services.AddResponseCompression(options => options.EnableForHttps = true);
|
||||
builder.Services.AddProblemDetails();
|
||||
builder.Services.AddProblemDetails(options =>
|
||||
{
|
||||
options.CustomizeProblemDetails = problemContext =>
|
||||
{
|
||||
var problem = problemContext.ProblemDetails;
|
||||
problem.Extensions.TryAdd("code", $"http.{problem.Status ?? 500}");
|
||||
problem.Extensions.TryAdd("traceId", problemContext.HttpContext.TraceIdentifier);
|
||||
};
|
||||
});
|
||||
builder.Services.AddExceptionHandler(options =>
|
||||
{
|
||||
options.ExceptionHandler = async context =>
|
||||
@@ -779,15 +802,26 @@ builder.Services.AddExceptionHandler(options =>
|
||||
var statusCode = isConstraintConflict
|
||||
? StatusCodes.Status409Conflict
|
||||
: StatusCodes.Status500InternalServerError;
|
||||
var localizer = context.RequestServices.GetRequiredService<IStringLocalizer<SharedResource>>();
|
||||
var code = isConstraintConflict
|
||||
? "data.constraint_conflict"
|
||||
: "server.unhandled_error";
|
||||
context.Response.StatusCode = statusCode;
|
||||
await Results.Problem(
|
||||
title: isConstraintConflict ? "数据约束冲突" : "服务器处理请求时发生错误",
|
||||
title: localizer[isConstraintConflict
|
||||
? "Errors.ConstraintConflict.Title"
|
||||
: "Errors.Unhandled.Title"],
|
||||
detail: isConstraintConflict
|
||||
? "编码可能已存在,或该数据正在被其他业务引用。"
|
||||
? localizer["Errors.ConstraintConflict.Detail"]
|
||||
: builder.Environment.IsDevelopment()
|
||||
? exception?.Message
|
||||
: "请稍后重试,并联系系统管理员查看日志。",
|
||||
statusCode: statusCode)
|
||||
: localizer["Errors.Unhandled.Detail"],
|
||||
statusCode: statusCode,
|
||||
extensions: new Dictionary<string, object?>
|
||||
{
|
||||
["code"] = code,
|
||||
["traceId"] = context.TraceIdentifier
|
||||
})
|
||||
.ExecuteAsync(context);
|
||||
};
|
||||
});
|
||||
@@ -802,6 +836,7 @@ builder.Services.AddHostedService<PluginActivationReconciler>();
|
||||
builder.Services.AddHostedService<PluginBackgroundTaskWorker>();
|
||||
|
||||
var mvcBuilder = builder.Services.AddControllers()
|
||||
.AddDataAnnotationsLocalization()
|
||||
.AddJsonOptions(options =>
|
||||
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()));
|
||||
pluginStartup.AddApplicationParts(mvcBuilder);
|
||||
@@ -832,6 +867,20 @@ builder.Services.AddSwaggerGen(options =>
|
||||
var app = builder.Build();
|
||||
|
||||
app.UseForwardedHeaders();
|
||||
app.UseRequestLocalization();
|
||||
app.Use(async (context, next) =>
|
||||
{
|
||||
if (context.Request.Path.StartsWithSegments("/api"))
|
||||
{
|
||||
context.Response.OnStarting(() =>
|
||||
{
|
||||
context.Response.Headers.ContentLanguage = CultureInfo.CurrentUICulture.Name;
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
}
|
||||
|
||||
await next();
|
||||
});
|
||||
app.UseExceptionHandler();
|
||||
app.UseResponseCompression();
|
||||
app.Use(async (context, next) =>
|
||||
|
||||
Loaded 3 of 20 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user