其实在.NET Core有更好的多语言实现方式
#新建项目(.NET Core MVC)
#在项目根目录新建Resources, 在Resources目录里面添加Program.en-US.resx || Program.zh-Hans.resx || Program.xx-XX.resx等等你要使用的资源文件(中缀要符合 RFC 4646, 你也可以自己起中缀, 但是效果不出来不要怪我)
#这是我的Startup.cs(部分)
__________________________>>
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddMvc()
.AddJsonOptions(options => { options.SerializerSettings.ContractResolver = new DefaultContractResolver(); })
.AddDataAnnotationsLocalization(options =>
{
options.DataAnnotationLocalizerProvider = (type, factory) =>
factory.Create(typeof(Program));
})
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddSessionStateTempDataProvider()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.Configure<RequestLocalizationOptions>(opts =>{
var supportedCultures = new List<CultureInfo>
{
new CultureInfo("en-US"),
new CultureInfo("zh-Hans")
};
opts.SupportedCultures = supportedCultures;
opts.SupportedUICultures = supportedCultures;
opts.RequestCultureProviders = new List<IRequestCultureProvider>{
new X_DOVERequestCultureProvider()
};
});
>>
//注意位置
app.UseRequestLocalization(); // app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>().Value);
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
__________________________
#Model >>
public class XAdCustomer // ViewModel
{
public Guid Id { get; set; } = Guid.NewGuid();
// Program.xx-XX.resx里面添加Display的Name值, 下同
[Display(Name = "Store name")]
[StringLength(maximumLength:20,MinimumLength =2)]
public string Name { get; set; }
[NotMapped]
[Display(Name="Registrant")]
public string UserName{get;set;}
[Display(Name="Date of registering")]
public DateTimeOffset DORegistering { get; set; } = DateTimeOffset.Now;
[Display(Name = "Business address")]
public string Address { get; set; }
[Display(Name= "Introduction")]
[StringLength(maximumLength:128)]
public string Intro { get; set; }
// . . .
}
#Controlers>>
private readonly IStringLocalizer<Program> _localizer;
public HomeController(IStringLocalizer<Program> localizer)
{
_localizer = localizer;
}
public IActionResult Index()
{
ViewData["Hello"] = _localizer["Hello"];
return View();
}
#Views>>
这是我的_ViewImports.cshtml
@using Microsoft.Extensions.Localization;
@using xxxxx.Program;
@inject IStringLocalizer<Program> Localizer;
@*如果在Area里面使用也是同样using和inject这三个*@
好了, 在你喜欢的Views中@Localizer["xxxx"]就可以了,
#贴一下X_DOVERequestCultureProvider.cs
public class X_DOVERequestCultureProvider : RequestCultureProvider
{
public override Task<ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext)
{
var CULTURE_String="CULTURE";
var CultureCookie = httpContext.Request.Cookies[CULTURE_String]?.ToString() ?? "";
if (string.IsNullOrWhiteSpace(CultureCookie))
{
CultureCookie = "zh-Hans";
httpContext.Response.Cookies.Append(key:CULTURE_String, value:CultureCookie, options: new CookieOptions() { Expires = DateTime.Now.AddYears(1) });
}
return Task.FromResult(new ProviderCultureResult(CultureCookie));
}
}
(看得懂吗? js修改Cookies的"CULTURE"的值然后location.reload()就可以换语言)
现在你不用创建那么多目录, 也不用创建那么多mvc resx了
如果还有哪里不懂的地方还可以私信我2034643712@qq.com
转载还请附上链接
要大四了, 一分钱没有, 过完今天都不知道明天怎么过, 有上顿没下顿