net core 3.1 Global

2021-02-10 11:20

阅读:553

标签:多语言   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 supportedCultures = new List
{
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 _localizer;
public HomeController( IStringLocalizer 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";
}

Hello

@Localizer["GoodBye"]


4.


https://localhost:44398/home/hello
https://localhost:44398/home/hello?ui-culture=zh-CN
https://localhost:44398/home/hello?ui-culture=en-US

net core 3.1 Global

标签:多语言   public   user   addm   service   ODB   asp   oca   microsoft   

原文地址:https://www.cnblogs.com/LiuFengH/p/13052183.html


评论


亲,登录后才可以留言!