更簡單的ASP.NET Core多語言(國際化和本地化, 歡迎轉載)
想到我還在Github上懟工程師對.NET Core多語言實現不好就生氣, 現在才感到學識淺薄是多麼可怕的一件事, 對不起, 是我太菜了, 是我太菜, 是我太菜, 學的東西太少, 對不起, 對不起, 我為我的魯莽而傷心
其實在.NET Core有更好的多語言實現方式
#新建專案(.NET Core MVC)
#在專案根目錄新建目錄SharedResources, 在SharedResources裡面新增SharedResource.cs, 這個類什麼都不用寫
#在專案根目錄新建Resources, 在Resources新建SharedResources目錄, 在目錄裡面新增SharedResource.en-US.resx || SharedResource.zh-CN.resx || SharedResource.xx-XX.resx等等你要使用的資原始檔
#這是我的Startup.cs
__________________________
>> services.AddMvc()
.AddDataAnnotationsLocalization(options =>//Model裡的ErrorMessage驗證 { options.DataAnnotationLocalizerProvider = (type, factory) => factory.Create(typeof(SharedResource)); }) .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.Configure<RequestLocalizationOptions>(opts => { var supportedCultures = new List<CultureInfo> { new CultureInfo("en-US"), new CultureInfo("zh-CN") }; opts.SupportedCultures = supportedCultures; opts.SupportedUICultures = supportedCultures; opts.RequestCultureProviders = new List<IRequestCultureProvider> { new XdoveRequestCultureProvider()//重寫的RequestCultureProvider() }; });
>>
//注意位置
app.UseRequestLocalization(); // app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>().Value);
app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); });
__________________________
#在Model裡我還未使用, 但是應該ok的
#Controlers>>
private readonly IStringLocalizer<SharedResource> _localizer; public HomeController(IStringLocalizer<SharedResource> localizer) { _localizer = localizer; } public IActionResult Index() { ViewData["Hello"] = _localizer["Hello"]; return View(); }
#Views>>
這是我的_ViewImports.cshtml
@using Microsoft.Extensions.Localization; @using XdoveF.SharedResources
@inject IStringLocalizer<SharedResource> Localizer
@*如果在Area裡面使用也是同樣using和inject這三個*@
好了, 在你喜歡的Views中Localizer["xxxx"]就可以了,
#貼一下XdoveRequestCultureProvider.cs
//就是如果QueryString裡面有culture=xx-XX的話使用QueryString, 否則使用Cookie裡的CULTURE, Cookie為空則使用預設
public class XdoveRequestCultureProvider : RequestCultureProvider { public override Task<ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext) { var CultureCookie = httpContext.Request.Cookies["CULTURE"]==null?"":httpContext.Request.Cookies["CULTURE"].ToString(); var CultureQueryString = httpContext.Request.Query["culture"].ToString(); if (CultureQueryString != null && CultureQueryString != "") { if (CultureQueryString != CultureCookie) { httpContext.Response.Cookies.Append(key: "CULTURE", value: CultureQueryString, options: new CookieOptions() { Expires = DateTime.Now.AddYears(1) }); } return Task.FromResult(new ProviderCultureResult(CultureQueryString)); } if(CultureCookie=="") return Task.FromResult(new ProviderCultureResult("zh-CN")); return Task.FromResult(new ProviderCultureResult(CultureCookie)); } }
現在你不用建立那麼多目錄, 也不用建立那麼多mvc resx了, 我為我的魯莽道歉
如果還有哪裡不懂的地方還可以私信我[email protected]
轉載還請附上鍊接哦 我不知道你有沒有喜歡上.NET Core, 但是我已經愛上.NET Core了