2017-08-28 47 views
1

我已經在我的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))] 

語言只是改變了模型類的屬性。但我想改變靜態/非模型屬性的語言。就像我想改變按鈕文字一樣。我在資源文件中添加了所有內容。我怎樣才能做到這一點?

+2

所以你的按鈕已經在使用類似這樣的資源文件了嗎? '' –

+0

這是我寫的'' –

+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>