2017-04-06 77 views
2

我跟着關於在Asp.Net核心本地化的微軟教程,我有一個問題,文化不會改變,除非我更改Startup.cs文件中的默認文化。Asp.net核心1.1本地化,不改變文化

// Startup.cs - ConfigureServices Method 
services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; }); 

services.AddMvc() 
    .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix) 
    .AddDataAnnotationsLocalization(); 



// Startup.cs - Configure Method 
var supportedCultures = new List<CultureInfo> 
{    
    new CultureInfo("en-GB"), 
    new CultureInfo("fr-FR"), 
    ... 
}; 

app.UseRequestLocalization(new RequestLocalizationOptions() 
{ 
    DefaultRequestCulture = new RequestCulture("en-GB"),    
    SupportedCultures = supportedCultures, 
    SupportedUICultures = supportedCultures, 
    RequestCultureProviders = new List<IRequestCultureProvider>() 
}); 


// HomeController 
public IActionResult SetLanguage(string culture, string returnUrl) 
    { 
     Response.Cookies.Append(
      CookieRequestCultureProvider.DefaultCookieName, 
      CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)), 
      new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) } 
     ); 

     return LocalRedirect(returnUrl); 
    } 

,我想用一個錨鏈接來改變語言,如:

<a asp-area="" asp-controller="Home" asp-action="SetLanguage" asp-route-culture="fr-FR" asp-route-returnUrl="@Context.Request.Path">FR</a>

當我點擊的cookie成功是越來越設置鏈接,頁面重新加載,但本地化的文本仍然是相同。當我在Startup.cs文件中更改DefaultRequestCulture時,文化顯得很好(這意味着我的Resx文件已正確命名等)

那麼,如何才能實現基於點擊鏈接來更改語言?

編輯 我仍然在尋找一個解決方案,如果有人能幫助

EDIT 2 我做了一個新的解決方案,但問題仍然發生在VS 2017年,我上載我的VS項目Github上,如果任何人都可以採取更好看,並解釋我什麼我做錯了https://github.com/adonis07/Localization

編輯3
我能夠通過添加NuGet包中要解決的問題我解決方案Microsoft.AspNetCore.Authentication.Cookies。現在文化運作正常。

+0

是指這樣的:https://stackoverflow.com/a/38499465/5417285 –

回答

-1

您是否正在配置線程以更改爲代碼中某個特定文化?像這樣的事情,無論你讀語言的cookie:

CultureInfo ci = new CultureInfo(culture); 
     System.Threading.Thread.CurrentThread.CurrentUICulture = ci; 
     System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name); 
+0

沒有和教程並沒有提到這樣的事情,我認爲文化提供程序將通過檢查是否在每個請求上設置了3個選項(Url,Cookie,Header)中的任何一個來處理該問題。 –

+0

這是ASP.NET Core 1.1網站中的默認方法嗎?到目前爲止,我還沒有做任何事情,我正在等待推薦的改變語言的方式 –