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

來源:互聯網
上載者:User

標籤:ted   cti   data   value   path   actor   orm   資源檔   str   

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.resxc.在資源檔中添加Key 和Value  d.視圖頂部@using Microsoft.AspNetCore.Mvc.Localization@inject IViewLocalizer Localizer 需要顯示的字串中將Learn More   修改 @Localizer["Learn More"] 測試回合搞點。

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.