我创建了一个空项目.
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddLocalization(s => s.ResourcesPath = "Resources");
var supportedCultures = new CultureInfo[]
{
new CultureInfo("de-CH"),
new CultureInfo("en-GB"),
};
services.Configure<RequestLocalizationOptions>(s =>
{
s.SupportedCultures = supportedCultures;
s.SupportedUICultures = supportedCultures;
s.DefaultRequestCulture = new RequestCulture(culture: "de-CH", uiCulture: "de-CH");
});
services.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseStaticFiles();
// Using localization
var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(locOptions.Value);
app.UseMvc();
}
Run Code Online (Sandbox Code Playgroud)
文件夹结构
Resources
|
|--Controllers
| HomeController.de.resx
| HomeController.en.resx
| HomeController.resx
Run Code Online (Sandbox Code Playgroud)
调节器
public class HomeController : Controller …
Run Code Online (Sandbox Code Playgroud) 如何全局重载错误消息,例如,从 [Required] 属性获取?也许,我应该更改 CultureInfo,但我找不到任何教程。语言仍为默认英语。谢谢!
asp.net-core-mvc .net-core asp.net-core asp.net-core-localization