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



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

我正在创建一个带有多语言数据库的web应用程序,我已经找到了一些好的实践文章(比如这篇),并在堆栈溢出中给出了这样的答案。

因此,我决定使用一个包含项目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;
    }
}

因此,在我的控制器中,我迭代翻译,找到一个具有正确语言环境的翻译,并填充"currentLocale"属性,在页面中,我只接受它,用户就可以获得正确的语言。

我希望我说得很清楚,不乱,但如果你需要更多的信息,我很乐意告诉你更多。

前面的一些注释:

  • 我的回答更多的是对这个问题的补充,你添加了一条评论,然后导致了这个问题
  • 在我的回答中,我使用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映射器加载产品实例,并附上它们的所有翻译。然后我迭代翻译并选择需要的翻译。

前端/只读

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

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

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;}
}

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

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

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

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

在上述方法中,转换表是父表的扩展。因此,ProductTranslation拥有Product的所有可翻译字段。这是一个整洁快速的方法,也是一个不错的方法。

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

因此,我将使用前面的Product示例,它有两个字段title和description,可以翻译来解释我们的方法。还可以考虑另一个表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

该查询基于这样的假设,即产品的各个字段没有默认的翻译

类似的查询可用于从ProductCategory 中提取记录

最新更新