.NET 8.0 の ASP.NET Core アプリ(MVC)でローカライズ機能を使い、多国言語対応したサンプルを作成しました。
ソースコードは GitHub で公開しています。
ソースコード構成
今回使った Visual Studio プロジェクト テンプレートは、Visual Studio 2022 + .NET 8 + ASP.NET Core MVC、HTTPS無し。



ソースコード変更内容を解説

WebApplication1/Controllers/HomeController.cs
コントローラーに、Resources/Controllers/HomeController.**.resx リソースを参照する処理を追加。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Localization; using System.Diagnostics; using WebApplication1.Models; namespace WebApplication1.Controllers { public class HomeController : Controller { private readonly ILogger<HomeController> _logger; private readonly IStringLocalizer<HomeController> _localizer; public HomeController(ILogger<HomeController> logger, IStringLocalizer<HomeController> localizer) { _logger = logger; _localizer = localizer; } public IActionResult Index() { ViewBag.Message = _localizer["ControllerString1"]; return View(); } public IActionResult Privacy() { return View(); } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } } } |
WebApplication1/Views/Home/Index.cshtml
元々Top画面に表示されてた文は削除。
ビューに、Resources/Views/Home/Index.**.resx リソースを参照する処理を追加。

1 2 3 4 5 6 7 8 9 10 11 12 |
@using Microsoft.AspNetCore.Mvc.Localization @inject IViewLocalizer Localizer @{ ViewData["Title"] = Localizer["About"]; } <h1>Localizer["String1"]: @Localizer["ViewString1"]</h1> <h1>ViewBag.Message: @ViewBag.Message</h1> |
WebApplication1/Program.cs
Asp.net core 標準のローカライズ機能を開始処理に追加。
対応した言語は ja en のみ。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
using Microsoft.AspNetCore.Mvc.Razor; var builder = WebApplication.CreateBuilder(args); builder.Services.AddLocalization(options => options.ResourcesPath = "Resources"); builder.Services.Configure<RequestLocalizationOptions>(options => { var supportedCultures = new[] { "ja", "en" }; options.SetDefaultCulture(supportedCultures[0]) .AddSupportedCultures(supportedCultures) .AddSupportedUICultures(supportedCultures); }); builder.Services.AddMvc() .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix) .AddDataAnnotationsLocalization(); // Add services to the container. builder.Services.AddControllersWithViews() .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix) .AddDataAnnotationsLocalization(); builder.Services.Configure<RequestLocalizationOptions>(options => { var supportedCultures = new[] { new System.Globalization.CultureInfo("en"), new System.Globalization.CultureInfo("ja") }; options.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("ja"); options.SupportedCultures = supportedCultures; options.SupportedUICultures = supportedCultures; }); var app = builder.Build(); app.UseRequestLocalization(); // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); app.Run(); |
WebApplication1/Resources/Controllers/HomeController.en.resxWebApplication1/Resources/Controllers/HomeController.ja.resx
テスト用リソースを追加したのみ。

WebApplication1/Resources/Views/Home/Index.en.resxWebApplication1/Resources/Views/Home/Index.ja.resx
テスト用リソースを追加したのみ。

コメント