4

我使用MVC與CodeFirst創建一個多語種的網站,我已經按照本教程(http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application),但我面臨的一些問題...本地化實體使用EF-CodeFirst

我我會盡力詳細解釋,所以對於長問題很抱歉...

假設我有一個實體T,其中name屬性應該是本地化的,具有以下屬性(Date,Name)我已經建立了以下實體來表示實體及其本地化版本:

public class T 
{ 
    #region Primitive Properties 

    public int TID { get; set; } 

    [DisplayFormat(DataFormatString = "{0:d}")] 
    [Required] 
    public DateTime DateCreated { get; set; } 

    #endregion 

    #region Localized Properties 

    protected T_Locale TWithCurrentLocaleOrCreate 
    { 
     get 
     { 
      T_Locale t = this.T_Locales.SingleOrDefault(record => record.LocaleID == Locale.CurrentLocale.LocaleID); 
      // If the object is not available with the current locale, 
      // create it 
      if (t == null) 
      { 
       t = new T_Locale 
       { 
        Locale = Locale.CurrentLocale 
       }; 

       this.T_Locales.Add(t); 
      } 

      return t; 
     } 
    } 

    protected T_Locale TWithCurrentLocaleOrDefault 
    { 
     get 
     { 
      T_Locale t = this.T_Locales.SingleOrDefault(record => record.LocaleID == Locale.CurrentLocale.LocaleID); 
      // If the object is not available with the current locale, 
      // return it with the default locale 
      if (t == null) 
      { 
       t = this.T_Locales.SingleOrDefault(record => record.LocaleID == Locale.DefaultLocale.LocaleID); 
       // If the object is not available with the current locale, 
       // return it with any available locale 
       if (t == null) 
        t = this.T_Locales.First(); 
      } 

      return t; 
     } 
    } 

    [NotMapped] 
    public string Name 
    { 
     get 
     { 
      return this.TWithCurrentLocaleOrDefault.Name; 
     } 
     set 
     { 
      this.TWithCurrentLocaleOrCreate.Name = value; 
     } 
    } 

    #endregion 

    #region Navigation Properties 

    public virtual ICollection<T_Locale> T_Locales { get; set; } 

    #endregion 
} 


public class T_Locale 
{ 
    #region Primitive Properties 

    [Key] 
    [Column(Order = 0)] 
    public int TID { get; set; } 

    [Key] 
    [Column(Order = 1)] 
    public int LocaleID { get; set; } 

    [Required] 
    public string Name { get; set; } 

    #endregion 

    #region Navigation Properties 

    public virtual T T { get; set; } 

    public virtual Locale Locale { get; set; } 

    #endregion 
} 


public class Locale 
{ 
    #region Primitive Properties 

    public int LocaleID { get; set; } 

    [Required] 
    public string Name { get; set; } 

    [Required] 
    public string DisplayName { get; set; } 

    #endregion 

    #region Navigation Properties 

    public virtual ICollection<T_Locale> T_Locales { get; set; } 

    #endregion 

    #region Current Locale Properties 

    protected static string DefaultLocaleName 
    { 
     get 
     { 
      return "en"; 
     } 
    } 

    private static Locale _DefaultLocale; 
    public static Locale DefaultLocale 
    { 
     get 
     { 
      DatabaseContext database = new DatabaseContext(); 
      _DefaultLocale = database.Locales.SingleOrDefault(record => record.Name == Locale.DefaultLocaleName); 

      return _DefaultLocale; 
     } 
    } 

    private static Locale _CurrentLocale; 
    public static Locale CurrentLocale 
    { 
     get 
     { 
      DatabaseContext database = new DatabaseContext(); 

      if (_CurrentLocale == null) 
      { 
       // To Avoid the large logic behind for getting the current locale I’m using the default one here… 
       _CurrentLocale = Locale.DefaultLocale; 
      } 

      return _CurrentLocale; 
     } 
    } 

    #endregion 
} 

其中,T:是我感興趣的實體,並且T_Locale是T的本地化版本僅包含應屬性被本地化,並且您可能注意到我在T(Name)中編寫了一個NotMapped屬性以獲取屬性Name的當前本地化版本...此屬性應該返回名稱與當前語言環境,並且在調用setter時應該修改當前語言環境的名稱。

我創建了一個帶有Razor視圖的T控制器,沒有任何進一步的修改,當瀏覽到創建視圖時,我得到正確的視圖,但是當點擊創建按鈕時,我有一個來自方法的異常「TWithCurrentLocaleOrDefault」,因爲我已經注意到它在調用setter之前從Name屬性的getter被調用......並且由於沒有剛創建的T實例的本地化版本,所以我得到了該方法的異常。

我不知道我是否在這裏錯過了一些東西,或者如果我使用了錯誤的邏輯,那麼請你向我解釋什麼是錯的,或者指向一個好的教程或示例代碼來處理本地化使用Mvc。

UPDATE:

問題是,我創建多個上下文實例,所以我想我應該改變,我使用在訪問任何本地化的實體的當前局部實例(當前T_Locale邏輯T),如果有什麼好的邏輯來處理這種情況,請指點我...

對不起,對於很長的問題,任何幫助將不勝感激,並提前很多感謝。

回答

0

我決定使用Griffin本地化,因爲它非常易於實施並且非常易於管理。有一個很好的代碼項目教程:Localization in ASP.NET MVC with Griffin.MvcContrib

+0

我沒看過這篇文章,但我沒有得到它如何可以幫助我存儲任何本地化的實體所需的本地化版本......我需要一個數據庫表,其中包含每個實體的翻譯,因爲修改翻譯將是我的項目的用例之一,數據翻譯不是視圖...我無法下載演示演示的鏈接可能已損壞。但對於視圖本地化它非常有用......謝謝。 – Mousa 2013-04-29 11:23:25

+3

-1因爲這個答案與問題無關。 – 2013-09-09 18:33:38