2014-10-29 24 views
4

我的ASP.NET MVC5應用程序存在問題。我的應用程序可以設置在瀏覽器中設置的lang/culture(現在只有英文和波蘭文(默認))。我想通過點擊Html.ActionLink讓用戶改變語言/文化。ASP.NET MVC5通過單擊Html.ActionLink更改語言/文化

我創建了一個類:

namespace Guestbook 
{ 
    public static class Click 
    { 
     public static void SetCulture(string name) 
     { 
      Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(name); 
      Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture; 
     } 

    } 
} 

而且我有我的觀點:

@Html.ActionLink("PL", "", "Guests", routeValues: null, htmlAttributes: new { onclick = "SetCulture(\"pl\");" }) 
@Html.ActionLink("EN", "", "Guests", routeValues: null, htmlAttributes: new { onclick = "SetCulture(\"en\");" }) 

。當然,這是行不通的。我需要更多什麼? JavaScript函數?

回答

7

最簡單的答案是您需要創建一個控制器,然後鏈接到該控制器。

public class LanguageController : Controller 
{ 
    public ActionResult SetLanguage(string name) 
    { 
     Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(name); 
     Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture; 

     HttpContext.Current.Session["culture"] = name; 

     return RedirectToAction("Index", "Home"); 
    } 
} 

然後在您的視圖:

<a href="@Url.Action("SetLanguage", "Language", new { @name = "pl" })">Polski</a> 
<a href="@Url.Action("SetLanguage", "Language", new { @name = "en" })">English</a> 

你可能會考慮在會議或類似的存儲用戶數據。

編輯:

例如,你可以使用在Global.asax中的Application_BeginRequest事件。

protected void Application_BeginRequest(Object sender, EventArgs e) 
{ 
    var name = HttpContext.Current.Session["culture"] as string; 

    if (string.IsNullOrEmpty(name)) 
    { 
     return; 
    } 

    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(name); 
    System.Threading.Thread.CurrentThread.CurrentUICulture = System.Threading.Thread.CurrentThread.CurrentCulture; 
} 

編輯:

保存的Cookie在SetLanguage行動:

var cookie = new HttpCookie("_culture", name); 
cookie.Expires = DateTime.Today.AddYears(1); 
Response.SetCookie(cookie); 

抓取的cookie中的Application_BeginRequest:

var cookie = HttpContext.Current.Request.Cookies["_culture"]; 
var name = cookie != null ? cookie.Value : null; 
+0

可以提供多一些存儲每個用戶的文化信息? – dariogriffo 2014-10-29 11:29:46

+0

正如你所說,我創建了一個控制器並編輯我的視圖,但它在我的應用程序中不起作用。 – Zashi 2014-10-29 12:26:31

+0

請您詳細說明一下嗎?什麼似乎是這個問題?行動是否受到打擊? – Olivier 2014-10-29 12:33:10

1

我創建了一個小型控制器和編輯我查看。 @Olivier(感謝隊友!)向我展示瞭如何做到這一點,但它沒有奏效,因爲我的應用程序將文化存儲在cookie中,而不是存儲在會話中。

控制器:

public class LanguageController : BaseController 
    { 
     // GET: Language 
     public ActionResult SetLanguage(string name) 
     { 
      Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(name); 
      Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture; 

      HttpCookie cultureCookie = new HttpCookie("_culture"); 
      cultureCookie.Value = name; 
      cultureCookie.Expires = DateTime.UtcNow.AddYears(1); 

      Response.Cookies.Remove("_culture"); 
      Response.SetCookie(cultureCookie); 


      return RedirectToAction("Index", "Guests"); 
     } 
    } 

LanguageController從BaseController(從控制器繼承)繼承,因爲我用這個教程:ASP.NET MVC 5 Internationalization by Nadeem Afana

在我看來:

<a href="@Url.Action("SetLanguage", "Language", new { @name = "pl" })">Polski</a> 
<a href="@Url.Action("SetLanguage", "Language", new { @name = "en" })">English</a>