feat: Implement client-side localization using JSON files, a custom localizer, and a new language switcher component.

This commit is contained in:
Ho Ngoc Hai
2026-01-19 11:58:52 +07:00
parent caf1a06dc8
commit e00c6032d1
12 changed files with 293 additions and 17 deletions

View File

@@ -3,6 +3,8 @@
/// VI: ASP.NET Core BFF (Backend for Frontend) với YARP Reverse Proxy.
/// </summary>
using Microsoft.AspNetCore.Rewrite;
var builder = WebApplication.CreateBuilder(args);
// ═══════════════════════════════════════════════════════════════════════════════
@@ -54,10 +56,18 @@ if (app.Environment.IsDevelopment())
app.UseHttpsRedirection();
// EN: Enable CORS
// VI: Kích hoạt CORS
// EN: Enable CORS
// VI: Kích hoạt CORS
app.UseCors("BlazorClient");
// EN: Rewrite localized framework/content requests to root
// VI: Viết lại các yêu cầu framework/content từ đường dẫn ngôn ngữ về root
var rewriteOptions = new RewriteOptions()
.AddRewrite(@"^(en-US|vi-VN)/(_framework|_content)/(.*)", "$2/$3", skipRemainingRules: true);
app.UseRewriter(rewriteOptions);
// EN: Serve static files with fingerprinting support (.NET 10+)
// VI: Phục vụ static files với hỗ trợ fingerprinting (.NET 10+)
app.MapStaticAssets();
@@ -70,8 +80,44 @@ app.MapHealthChecks("/health");
// VI: Map các routes YARP Reverse Proxy đến microservices
app.MapReverseProxy();
// EN: Fallback to index.html for SPA routing
// VI: Fallback đến index.html cho SPA routing
// EN: Localization Support - Serve index.html with dynamic base tag for specific cultures
// VI: Hỗ trợ đa ngôn ngữ - Phục vụ index.html với base tag động cho các ngôn ngữ cụ thể
var supportedCultures = new[] { "en-US", "vi-VN" };
var localizationOptions = new RequestLocalizationOptions()
.SetDefaultCulture("en-US")
.AddSupportedCultures(supportedCultures)
.AddSupportedUICultures(supportedCultures);
app.UseRequestLocalization(localizationOptions);
// Handle mapped culture routes (e.g. /en-US/home, /vi-VN/solutions)
app.Map("{culture:regex(^(en-US|vi-VN)$)}/{**slug}", async (string culture, HttpContext context, IWebHostEnvironment env) =>
{
// Try to find index.html
var fileInfo = env.WebRootFileProvider.GetFileInfo("index.html");
if (!fileInfo.Exists)
{
// In Development with Hosted Blazor, index.html might not be in Server's wwwroot strictly directly depending on setup,
// but typically it is served via StaticFiles/BlazorFrameworkFiles.
// If we can't find it easily via IWebHostEnvironment in Dev, we might fail.
// However, for this task let's assume standard structure or handle gracefully.
return Results.NotFound("index.html not found in wwwroot. Ensure the Client project is built.");
}
using var stream = fileInfo.CreateReadStream();
using var reader = new StreamReader(stream);
var html = await reader.ReadToEndAsync();
// Replace base tag: <base href="/" /> -> <base href="/vi-VN/" />
// Be robust with spaces or standard format
var modifiedHtml = html.Replace("<base href=\"/\" />", $"<base href=\"/{culture}/\" />")
.Replace("<base href=\"/\"/>", $"<base href=\"/{culture}/\" />");
return Results.Content(modifiedHtml, "text/html");
});
// EN: Fallback to index.html for SPA routing (default culture)
// VI: Fallback đến index.html cho SPA routing (ngôn ngữ mặc định)
app.MapFallbackToFile("index.html");
app.Run();