diff --git a/i18N/Lab.i18N.WebApi/DemoController.cs b/i18N/Lab.i18N.WebApi/DemoController.cs index c807214b..2c855e2d 100644 --- a/i18N/Lab.i18N.WebApi/DemoController.cs +++ b/i18N/Lab.i18N.WebApi/DemoController.cs @@ -11,10 +11,12 @@ public class DemoController(IStringLocalizer localizer) : Contro public IActionResult Get() { var desc = localizer["about.description"]; + var key = localizer["exampleKey"]; return Ok(new { - desc + desc, + key }); } } \ No newline at end of file diff --git a/i18N/Lab.i18N.WebApi/Lab.i18N.WebApi.csproj b/i18N/Lab.i18N.WebApi/Lab.i18N.WebApi.csproj index 4e28fa30..68bb857d 100644 --- a/i18N/Lab.i18N.WebApi/Lab.i18N.WebApi.csproj +++ b/i18N/Lab.i18N.WebApi/Lab.i18N.WebApi.csproj @@ -31,6 +31,9 @@ Always + + Always + diff --git a/i18N/Lab.i18N.WebApi/Program.cs b/i18N/Lab.i18N.WebApi/Program.cs index 617251dc..ac619ea0 100644 --- a/i18N/Lab.i18N.WebApi/Program.cs +++ b/i18N/Lab.i18N.WebApi/Program.cs @@ -23,7 +23,15 @@ }); i18N.IntegrateToAspNetCore() - .AddBackend(new JsonFileBackend("wwwroot/locales")); + .AddBackend(new JsonFileBackend("wwwroot/locales")) + .AddBackend(p => + { + var memoryBackend = new InMemoryBackend(); + memoryBackend.AddTranslation("en", "translation", "exampleKey", "My English text."); + memoryBackend.AddTranslation("zh", "translation", "exampleKey", "我的中文字"); + return memoryBackend; + }) + ; }); var app = builder.Build(); @@ -34,14 +42,13 @@ app.UseSwaggerUI(); } -app.UseRequestLocalization(options => { options.AddSupportedCultures("de", "en"); }); - -var supportedCultures = new[] { "en-US", "zh-TW" }; +// var supportedCultures = new[] { "en-US", "de-DE", "zh-TW" }; +var supportedCultures = new[] { "en", "de", "zh" }; var cultureInfos = supportedCultures.Select(c => new CultureInfo(c)).ToList(); app.UseRequestLocalization(p => { - p.DefaultRequestCulture = new RequestCulture("en-US"); + p.DefaultRequestCulture = new RequestCulture("zh-TW"); p.SupportedCultures = cultureInfos; p.SupportedUICultures = cultureInfos; p.RequestCultureProviders.Insert(0, new HeaderRequestCultureProvider()); diff --git a/i18N/Lab.i18N.WebApi/wwwroot/locales/tw/translation.json b/i18N/Lab.i18N.WebApi/wwwroot/locales/tw/translation.json new file mode 100644 index 00000000..c073e81b --- /dev/null +++ b/i18N/Lab.i18N.WebApi/wwwroot/locales/tw/translation.json @@ -0,0 +1,11 @@ +{ + "page": { + "title": "這是一個翻譯後的頁面標題" + }, + "about": { + "description": "這是關於頁面的描述。", + "renderedOn": "此頁面生成於 {{date, yyyy/mm/dd}}。", + "additionalInformation": "額外資訊", + "infoText": "使用此區域提供 $t(about.additionalInformation)。" + } +}