Multi Language Website In MVC 4 C#

Globalization in asp.net razor MVC4 C#

Very easy to using App_GlobalResources make global language website, it also has a multi-language UI (using resource files). Multi language website without passing parameter in controller.


Download Full Example

BaseController
public class BaseController : Controller
{
    protected override void ExecuteCore()
    {
        int culture = 0;
        if (this.Session == null || this.Session["CurrentCulture"] == null)
        {
            int.TryParse(System.Configuration.ConfigurationManager.AppSettings["Culture"], out culture);
            this.Session["CurrentCulture"] = culture;
        }
        else
        {
            culture = (int)this.Session["CurrentCulture"];
        }
        //
        SessionManager.CurrentCulture = culture;
        //
        // Invokes the action in the current controller context.
        //
        base.ExecuteCore();
    }

    protected override bool DisableAsyncSupport
    {
        get { return true; }
    }
}
SessionManager
public class SessionManager
{
    protected HttpSessionState session;

    public SessionManager(HttpSessionState httpSessionState)
        {
            session = httpSessionState;
        }

    public static int CurrentCulture
    {
        get
        {
            if (Thread.CurrentThread.CurrentUICulture.Name == "en-US")
                return 0;
            else if (Thread.CurrentThread.CurrentUICulture.Name == "de-DE")
                return 1;
            else
                return 0;
        }
        set
        {
            //
            // Set the thread's CurrentUICulture.
            //
            if (value == 0)
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
            else if (value == 1)
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("de-DE");
            else
                Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
            //
            // Set the thread's CurrentCulture the same as CurrentUICulture.
            //
            Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture;
        }
    }
}
Update the Required attribute as shown in the following code.
To support validation error messages for another culture, we must create a resources file with the same name as the default one, but adding the culture to its name.
[Required(ErrorMessageResourceType = typeof(ValidationErrors), ErrorMessageResourceName = "UserName")]
[Display(Name = "User name")]
public string UserName { get; set; }

Multi Language Website In MVC 4 C# Multi Language Website In MVC 4 C# Reviewed by Bhaumik Patel on 10:58 PM Rating: 5