Search code examples
asp.net-mvcglobalization

Changing culture only changing the language of model items


I have implemented the culture in my Asp.net application. But what I want is to change the language of the entire page but it's changing only model items language.

CultureHelper.cs

 public class CultureHelper
 {
        protected HttpSessionState session;

        public CultureHelper(HttpSessionState httpSessionState)
        {
            session = httpSessionState;
        }
        public static int CurrentCulture
        {
            get
            {
                switch (Thread.CurrentThread.CurrentUICulture.Name)
                {
                    case "en": return 0;
                    case "hi-IN": return 1;
                    default: return 0;
                }
            }
            set
            {
                if (value == 0)
                {
                    Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en");
                }
                else if(value==1)
                {
                    Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("hi-IN");
                }
                else
                {
                    Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.InvariantCulture;
                }
                Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture;
            }
        }
    }

BaseController.cs

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"];
            }
            // calling CultureHelper class properties for setting  
            CultureHelper.CurrentCulture = culture;

            base.ExecuteCore();
        }

        protected override bool DisableAsyncSupport
        {
            get { return true; }
        }

My model class

 [Display(Name="Name",ResourceType =typeof(Resource))]  

Language only changing for the model class properties. But I want to change the language for static/nonmodel properties too. Like I want to change button text too. I have added all contents in the resource file. How can I achieve this?


Solution

  • Add a resource file for every culture you want to support, e.g.

    Resources.en.resx 
    Resources.hi-IN.resx
    

    The framework will resolve which file to use based on the set CurrentCulture. Resources.resx (without culture name) will be used as fallback if no culture specific file can be found.

    Use the resource file to retrieve your translated strings, e.g. in your View.cshtml:

    @using MyProject.Resources
    
    <button type="submit">
        @Resources.Submit @* this retrieves the translation for the key "Submit" *@
    </button>