I'm using LocalizationAttribute
which implements ActionFilterAttribute
to localize views. I simply put [Localize]
on controller. I was using LocalizeStrings.resx files to apply based on which language is on current thread. Everything works for this simple case (with localized strings). Now I want to localize complete pages (not just strings).
To acomplish this which approach do you use?
Do I need to identify which thread is current on controller and based on that value to call view:
public ActionResult AboutUs()
{
switch (Thread.CurrentThread.CurrentUICulture.Name)
{
case "en-US":
return View("EnglishUSView");
case "de-DE":
return View("GermanView");
default:
return View();
}
return View();
}
or do you recommend something else ?
I would recommend to simply extended RazorViewEngine
and override FindPartialView
and FindView
in order to inform ViewEngine to look is there view with current culture inside thread. If that page cannot be found than proceed as usuall.
RazorViewEngine
Index.cshtml
and Index.de-DE.cshtml
Global.asax.cs
file, inside application start you should call localized view engine (the one which implements RazorViewEngine)Controller
no further modification needed
Views
/Views/Home/Index.cshtml
/Views/Home/Index.de.DE.cshtml
Helper
public class LocalizedRazorViewEngine : RazorViewEngine
{
public override ViewEngineResult FindPartialView ...
public override ViewEngineResult FindView...
}
ApplicationStart
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new LocalizedRazorViewEngine());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With