改变文化只会改变模型项的语言

时间:2017-08-28 07:42:40

标签: asp.net-mvc globalization

我已在Asp.net应用程序中实现了这种文化。但我想要的是改变整个页面的语言,但它只改变模型项语言。

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; }
        }

我的模特课

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

仅针对模型类属性更改的语言。但我也希望改变静态/非模型属性的语言。就像我想要更改按钮文本一样。我已在资源文件中添加了所有内容。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

为您要支持的每种文化添加资源文件,例如

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

框架将根据集合CurrentCulture解析要使用的文件。 如果找不到特定于文化的文件,Resources.resx(没有文化名称)将用作后备。

使用资源文件检索已翻译的字符串,例如在View.cshtml中:

@using MyProject.Resources

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