标签:多语言 public user addm service ODB asp oca microsoft
NET CORE 3.1多语言
控制器多语言
ConfigureServices 1
services.AddLocalization(o =>
{
o.ResourcesPath = "Resources";
});
services.AddMvc();
Configure 2
IList<CultureInfo> supportedCultures = new List<CultureInfo>
{
new CultureInfo("en-US"),
new CultureInfo("zh-CN"),
};
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
});
3
private readonly IStringLocalizer<HomeController> _localizer;
public HomeController( IStringLocalizer<HomeController> localizer)
{
_localizer = localizer;
}
public IActionResult Hello()
{
return Content(_localizer["Hello"]);
}
4.
Resources/Controllers/APIController.en-US.resx Hello HELLO
Resources/Controllers/APIController.zh-CN.resx Hello 你好
https://localhost:44398/home/hello
https://localhost:44398/home/hello?ui-culture=zh-CN
https://localhost:44398/home/hello?ui-culture=en-US
----------------------------------------------------
视图多语言
ConfigureServices 1
services.AddMvc().AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix);
2. HomeController
public IActionResult Hello()
{
return View();
}
3.
Views/Home/Hello.zh-CN.resx GoodBye 再见
4.Hello.cshtml
@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer
@{
ViewData["Title"] = "Hello";
}
<h1>Hello</h1>
<h2>@Localizer["GoodBye"]</h2>
4.
https://localhost:44398/home/hello
https://localhost:44398/home/hello?ui-culture=zh-CN
https://localhost:44398/home/hello?ui-culture=en-US
标签:多语言 public user addm service ODB asp oca microsoft
原文地址:https://www.cnblogs.com/LiuFengH/p/13052183.html