多语言数据库,具有默认回退功能

时间:2014-11-05 19:11:58

标签: java database spring jpa multilingual

我有一个问题,我知道,已被广泛讨论过,但在我看来,还有一个方面需要澄清。

我正在创建一个带有多语言数据库的Web应用程序,我已经找到了一些好的练习文章(例如this),这里的回答是像this这样的堆栈溢出。

因此,我决定使用包含我的商品ID的主表和另一个包含每个商品的翻译的表格,例如

Content
ContentTranslation

Category
CategoryTranslation

等等。

现在我在做什么?我只是从数据库中获取所有翻译的项目,然后我迭代每个项目以根据当前用户的本地查找正确的翻译,如果我找到正确的本地我设置到主要对象中翻译要翻译的页面,否则我只是得到标记为"默认"的翻译。之一。

但是,如果有大量的对象和翻译,服务器响应时间可能会增长,即使用户可能没有注意到,我也不想要这样做。

那么,这个用例有没有好的做法呢?例如,一些特定的查询说"用语言环境选择翻译"它"但是如果你发现它只是得到一个带有"默认"国旗集?

现在我正在使用Spring MVC和Hibernate以及JPA(通过JPARepository)。

我的对象都扩展了我用这种方式创建的基本可翻​​译类

@MappedSuperclass
public abstract class Translatable<T extends Translation> extends BaseDTO {

    private static final long serialVersionUID = 562001309781752460L;

    private String title;

    @OneToMany(fetch=FetchType.EAGER, orphanRemoval=true, cascade=CascadeType.ALL)
    private Set<T> translations = new HashSet<T>();

    @Transient private T currentLocale;

    public void addLocale(T translation, boolean edit) {
        if (!edit)
            getTranslations().add(translation);
    }

    public void remLocale(String locale) {
        T tr = null;
        for (T candidate: getTranslations()) {
            if (candidate.getLocale().equals(locale))
                tr = candidate;
        }

        getTranslations().remove(tr);
    }

    public T getLocaleFromString(String locale) {
        if (locale == null)
            return null;
        for (T trans: translations) {
            if (trans.getLocale().equals(locale))
                return trans;
        }
        return null;
    }

    public T getDefaultLocale() {
        for (T tr: translations) {
            if (tr.isDefaultLocale())
                return tr;
        }
        return null;
    }

    public Set<T> getTranslations() {
        return translations;
    }

    public void setTranslations(Set<T> translations) {
        this.translations = translations;
    }

    public T getCurrentLocale() {
        return currentLocale;
    }

    public void setCurrentLocale(T currentLocale) {
        this.currentLocale = currentLocale;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

因此,在我的控制器中,我遍历翻译,找到具有正确语言环境的翻译并填充&#34; currentLocale&#34;属性,在我的页面中我只是采用它,用户可以按预期获得正确的语言。

我希望我已经清楚而且不凌乱,但如果你需要更多信息,我会很高兴告诉你更多。

2 个答案:

答案 0 :(得分:32)

预先提出一些注意事项:

  • 我的回答更多是对my answer to this question的补充,您在其中添加了评论,然后导致了这个问题
  • 在我的回答中我正在使用C#和MS SQL Server(我将省略任何OR映射特定代码)

在我的应用程序中,我使用两种不同的方法来加载多语言数据,具体取决于用例:

管理/ CRUD

如果用户输入数据或编辑现有数据(例如带有翻译的产品),我使用的方法与上述问题相同,例如:

public class Product
{
    public int ID {get; set;}
    public string SKU {get; set;}
    public IList<ProductTranslation> Translations {get; set;}
}
public class ProductTranslation
{
    public string Language {get; set;}
    public bool IsDefaultLanguage {get; set;}
    public string Title {get; set;}
    public string Description {get; set;}
}

即。我将让OR-mapper加载产品实例并附上所有翻译。然后我遍历翻译并选择所需的翻译。

前端/只读

在这种情况下,主要是前端代码,我通常只是向用户显示信息(最好是用户的语言),我使用的是另一种方法:

首先,我使用的是不同的数据模型,它不支持/知道多个翻译的概念。相反,它只是当前用户使用“最佳”语言表示的产品:

public class Product
{
    public int ID {get; set;}
    public string SKU {get; set;}

    // language-specific properties
    public string Title {get; set;}
    public string Description {get; set;}
}

要加载此数据,我正在使用不同的查询(或存储过程)。例如。要使用语言@Id加载ID为@Language的商品,我会使用以下查询:

SELECT
    p.ID,
    p.SKU,
    -- get title, description from the requested translation,
    -- or fall back to the default if not found:
    ISNULL(tr.Title, def.Title) Title,
    ISNULL(tr.Description, def.Description) Description
  FROM Products p
  -- join requested translation, if available:
  LEFT OUTER JOIN ProductTranslations tr
    ON p.ID = tr.ProductId AND tr.Language = @Language
  -- join default language of the product:
  LEFT OUTER JOIN ProductTranslations def
    ON p.ID = def.ProductId AND def.IsDefaultLanguage = 1
  WHERE p.ID = @Id

如果存在该语言的翻译,则以所请求的语言返回产品的标题和说明。如果不存在翻译,则将返回默认语言的标题和说明。

答案 1 :(得分:4)

对所有表的所有可翻译字段使用通用共享表

在上述方法中,转换表是父表的扩展。因此,ProductTranslation具有Product的所有可翻译字段。这是一个简洁快捷的方法,也很好。

但是有一个缺点(不确定它是否可以称为劣势)。如果有更多表需要可翻译字段,则需要许多新表。根据我的经验,我们采取了不同的方法。我们创建了一个用于翻译的通用表和一个链接表,用于将翻译链接到父表的可翻译字段。

所以我将使用前面的Product示例,它有两个字段标题和描述,可以翻译我们的方法。另请考虑另一个表ProductCategory,其中包含字段名称和描述,也需要翻译。

Product
(
   ID: Integer
   SKU: String
   titleID: Integer // ID of LocalizableText record corresponding title
   descriptionID: Integer // ID of LocalizableText record corresponding description
)

ProductCategory
(
   ID: Integer
   nameID: Integer // ID of LocalizableText record corresponding name
   descriptionID: Integer // ID of LocalizableText record corresponding description
)

LocalizableText // This is nothing but a link table
{
    ID: Integer
}

Translations //This is where all translations are stored.
{
    ID: Integer
    localizableTextID: Integer
    language: String
    text: String
}

要加载此数据,我正在使用不同的查询(修改上述内容)。例如。要使用@Language语言加载ID为@Id的产品,我将使用以下查询

SELECT
    p.ID,
    p.SKU,
    -- get title, description from the requested translation,
    -- or fall back to the default if not found:
    Title.text Title,
    description.text Description
  FROM Products p
  -- join requested translation for title, if available:
  LEFT OUTER JOIN Translations title
    ON p.titleID = title.localizableTextID
       AND title.Language = @Language
  -- join requested translation for description, if available:
  LEFT OUTER JOIN Translations description
    ON p.descriptionID = description.localizableTextID
       AND description.Language = @Language
  WHERE p.ID = @Id

此查询基于以下假设:Product的各个字段没有默认翻译

类似的查询可用于从ProductCategory

获取记录