Skip to content

Commit

Permalink
feat: add tw language
Browse files Browse the repository at this point in the history
  • Loading branch information
yaochangyu committed Dec 20, 2024
1 parent 433a516 commit cd1ee35
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
4 changes: 3 additions & 1 deletion i18N/Lab.i18N.WebApi/DemoController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ public class DemoController(IStringLocalizer<DemoController> localizer) : Contro
public IActionResult Get()
{
var desc = localizer["about.description"];
var key = localizer["exampleKey"];

return Ok(new
{
desc
desc,
key
});
}
}
3 changes: 3 additions & 0 deletions i18N/Lab.i18N.WebApi/Lab.i18N.WebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
<Content Update="wwwroot\locales\en\translation.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Update="wwwroot\locales\tw\translation.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>
17 changes: 12 additions & 5 deletions i18N/Lab.i18N.WebApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@
});

i18N.IntegrateToAspNetCore()
.AddBackend(new JsonFileBackend("wwwroot/locales"));
.AddBackend(new JsonFileBackend("wwwroot/locales"))
.AddBackend<InMemoryBackend>(p =>
{
var memoryBackend = new InMemoryBackend();
memoryBackend.AddTranslation("en", "translation", "exampleKey", "My English text.");
memoryBackend.AddTranslation("zh", "translation", "exampleKey", "我的中文字");
return memoryBackend;
})
;
});
var app = builder.Build();

Expand All @@ -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());
Expand Down
11 changes: 11 additions & 0 deletions i18N/Lab.i18N.WebApi/wwwroot/locales/tw/translation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"page": {
"title": "這是一個翻譯後的頁面標題"
},
"about": {
"description": "這是關於頁面的描述。",
"renderedOn": "此頁面生成於 {{date, yyyy/mm/dd}}。",
"additionalInformation": "額外資訊",
"infoText": "使用此區域提供 $t(about.additionalInformation)。"
}
}

0 comments on commit cd1ee35

Please sign in to comment.