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