I want to switch my views in MVC 3 between two languages - PL and EN. I created two folders in Views-EN and PL. Therefore, after clicking the appropriate language link on any site, I want to change the route:
routes.MapRoute(
"pl",
"{controller}/{action}/{id}",
new { controller = "PL", action = "Index", id = UrlParameter.Optional }
);
at
routes.MapRoute(
"en",
"{controller}/{action}/{id}",
new { controller = "EN", action = "Index", id = UrlParameter.Optional }
);
When I click on the appropriate link (language switch), it changes CultureInfo, which is constant for all threads. _Layout View with switch:
<ul>
<li>@Html.ActionLink("En", "ChangeCulture", null, new { lang = "en"}, null)</li>
<li>@Html.ActionLink("Pl", "ChangeCulture", null, new { lang = "pl"}, null)</li>
</ul>
and the controller (which also sets the static lang variable, which can be seen in every controller method and constant between requests):
public ActionResult ChangeCulture(string lang)
{
PLController.lang = lang;
CultureSettings setCulture = new CultureSettings();
setCulture.InitializeCulture(lang);
cookie.Value = CultureInfo.CurrentCulture.Name;
this.ControllerContext.HttpContext.Response.Cookies.Add(cookie);
return View("Index");
}
The InitializeCulture method is overridden from the page class as follows:
public class CultureSettings : Page{
public void InitializeCulture(string culture)
{
String selectedLanguage;
if(culture == null)
{
selectedLanguage = "pl";
}
else
{
selectedLanguage = culture;
}
UICulture = selectedLanguage;
Culture = selectedLanguage;
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture(selectedLanguage);
Thread.CurrentThread.CurrentUICulture = new
CultureInfo(selectedLanguage);
base.InitializeCulture();
}
}
CultureInfo. ( CultureInfo) mysite.com/PL/{controller}/{action} mysite.com/EN/{controller}/{action}.
- , , ? , : mysite.com/EN mysite.com/PL - (.. En.mysite.com)