简体   繁体   English

设置nHibernate以获取翻译

[英]Setup nHibernate to get translations

Im trying to set up NHibernate to get translations depending on witch languageId i provide. 我试图建立NHibernate来获取翻译,具体取决于我提供的女巫languageId。

I have a class that look something like this: 我有一堂课,看起来像这样:

public class Location
{
    public virtual Guid Id { get; set; }

    [Translate("Name")]
    public virtual string Name { get; set; }

    [Translate("Description")]
    public virtual string Description { get; set; }

    public virtual bool Popular { get; set; }
    public virtual bool Active { get; set; }
}

My Translate Attribute looks like this: 我的翻译属性如下所示:

[AttributeUsage(AttributeTargets.Property)]
public class TranslateAttribute : Attribute
{
    public readonly string DatabaseSuffix;

    public TranslateAttribute(string databaseSuffix)
    {
        DatabaseSuffix = databaseSuffix;
    }

    public string GetSuffix()
    {
        return DatabaseSuffix;
    }
}

I also have a thing that know what Language i am currently using: 我还有一个知道我当前正在使用哪种语言的东西:

public interface ILanguageProvider
{
    void SetLanguage(string language);
    string GetCurrentLanguage();
}

I would like to setup nhibernate with conventions so when fetching anything that has a property that has the Translate-attribute it should Query against my Translations-table like this for each property (or maybe get all translations at once): 我想使用约定来设置nhibernate,因此在获取具有Translate-attribute属性的任何内容时,应针对每个属性对我的Translations-table进行查询(或立即获取所有翻译):

SELECT TOP 1 Text 
FROM Translations 
WHERE Id = '{Id_From_my_Entity}_DataBaseSuffixFromTheTranslationAttribute' AND
      LanguageId = LanguageIdFromGetCurrentLanguageInTheImplentationOFILanguageProvider;

And set the result it gets back to its property it is getting the translation for. 并设置结果使其返回为其翻译的属性。

Is this possible? 这可能吗? and how do i implement this? 以及如何实现呢?

Im using nHibernate 3.3.1.4000 and Fluent NHibernate 1.3.0.733 我正在使用nHibernate 3.3.1.4000和Fluent NHibernate 1.3.0.733

i would not do this magically. 我不会神奇地做到这一点。

use a dictionary and a property which gives the current name. 使用字典和提供当前名称的属性。 This has the advantage that if the language is changed all objects will have the correct translation available. 这样做的好处是,如果更改语言,则所有对象都将具有正确的翻译。

public class MyClass1
{
    protected virtual IDictionary<string, string> Names { get; private set; }

    public virtual string Name { get { return Names[GetCurrentLanguageFromSomeWhere()]; } }
}


public class MyClass1Map : ClassMap<MyClass1>
{
    public MyClass1Map()
    {
        [...]
        HasMany(mc => mc.Names)
            .Table("Translations")
            .Where("property == 'Name'")
            .AsMap("language")
            .Element("text")
            .Not.LazyLoad();

        HasMany(mc => mc.Descriptions)
            .Table("Translations")
            .Where("property == 'Description'")
            .AsMap("language")
            .Element("text")
            .Not.LazyLoad();
    }
}

or use a Formula if the language is static 或使用公式(如果语言是静态的)

Map(mc => mc.Name).Formula("... WHERE Language=" + GetCurrentLanguage());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM