1. 程式人生 > >用app.net Core搞點多國語言網站

用app.net Core搞點多國語言網站

configure cor cati addm ons 文章 nop aspnet info

Asp.net Core 中文文檔很少,你可以看英文的,不過英文的也是說的有點亂。這篇文章是幹貨。 1. 配置好你的WebApplication,使他可以支持國際化語言,修改文檔Startup.cs publicvoid ConfigureServices(IServiceCollection services) { services.AddLocalization(options => options.ResourcesPath = "Resources"); services.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix) .AddDataAnnotationsLocalization(); } 2.修改好你的配置 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {... 這裏面寫你的網站需要支持的語言。
var supportedCultures = new[] { new CultureInfo("en-US"), new CultureInfo("zh-CN") }; 這裏是寫你的默認語言的 app.UseRequestLocalization(new RequestLocalizationOptions { DefaultRequestCulture = new RequestCulture
("en-US"), // Formatting numbers, dates, etc. SupportedCultures = supportedCultures, // UI strings that we have localized. SupportedUICultures = supportedCultures }); } 3.給你的視圖增加資源文檔 a.增加Resources目錄 b.按你的視圖路徑,給出資源文檔的結構 例如你的視圖Views\Home\Index.cshtml 你的資源Resources\Views\Home\Index.zh-CN.resx 或 Resources\Views\Home\Index.en-US.resx c.在資源文件中添加Key 和Value d.視圖頂部 @using Microsoft.AspNetCore.Mvc.Localization @inject IViewLocalizer Localizer 需要顯示的字符串中 Learn More 修改 @Localizer["Learn More"] 測試運行 搞點。

用app.net Core搞點多國語言網站