2016-06-08 49 views
3

我用ASP.NET Core創建了我的第一個應用程序。當我調試它,我看到有口音的話一個問題:如何更改默認文化?

text on html with accentuation problem decode

我該如何正確本地化應用程序?

更新:

我試圖實現喬的建議,但我並沒有得到預期的結果,你可以在此圖像中看到。

從數據庫中顯示的字符串沒問題,但視圖模板(如標題或文本)中使用的字符串顯示不正確。

我不想要一個多語言應用程序,只有一個português。

在舊asp.net這種配置是對的.config完成與元素

text html

+0

你能校驗編碼的意見(* .cshml)文件使用的是什麼?他們是UTF-8還是一些ISO-8859-x編碼? – Tseng

+0

你可以通過選擇文件並轉到文件>另存爲...>保存編碼...並選擇「Unicode(帶簽名的UTF-8)」 – Tseng

+0

完美,它的工作! –

回答

4

在project.json你需要這種依賴性

"Microsoft.Extensions.Localization": "1.0.0-rc2-final", 

在Startup.cs在ConfigureServices你需要這樣的代碼:

services.AddLocalization(options => options.ResourcesPath = "GlobalResources"); 

     services.Configure<RequestLocalizationOptions>(options => 
     { 
      var supportedCultures = new[] 
      { 
       new CultureInfo("en-US"), 
       new CultureInfo("en"), 
       new CultureInfo("fr-FR"), 
       new CultureInfo("fr"), 
      }; 

      // State what the default culture for your application is. This will be used if no specific culture 
      // can be determined for a given request. 
      options.DefaultRequestCulture = new RequestCulture(culture: "en-US", uiCulture: "en-US"); 

      // You must explicitly state which cultures your application supports. 
      // These are the cultures the app supports for formatting numbers, dates, etc. 
      options.SupportedCultures = supportedCultures; 

      // These are the cultures the app supports for UI strings, i.e. we have localized resources for. 
      options.SupportedUICultures = supportedCultures; 

      // You can change which providers are configured to determine the culture for requests, or even add a custom 
      // provider with your own logic. The providers will be asked in order to provide a culture for each request, 
      // and the first to provide a non-null result that is in the configured supported cultures list will be used. 
      // By default, the following built-in providers are configured: 
      // - QueryStringRequestCultureProvider, sets culture via "culture" and "ui-culture" query string values, useful for testing 
      // - CookieRequestCultureProvider, sets culture via "ASPNET_CULTURE" cookie 
      // - AcceptLanguageHeaderRequestCultureProvider, sets culture via the "Accept-Language" request header 
      //options.RequestCultureProviders.Insert(0, new CustomRequestCultureProvider(async context => 
      //{ 
      // // My custom request culture logic 
      // return new ProviderCultureResult("en"); 
      //})); 
     }); 

in配置你需要的代碼是這樣的:

var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>(); 
     app.UseRequestLocalization(locOptions.Value); 

我有一些working demo code here,如果你需要更多的