Luc*_*uca 16 java database multilingual spring jpa
我有一个问题,我知道,已被广泛讨论过,但在我看来,还有一个方面需要澄清.
我正在创建一个带有多语言数据库的Web应用程序,我已经找到了一些好的练习文章(例如 这个),这里的答案就像这样堆栈溢出.
所以我决定使用一个带有我的项目ID的主表和另一个带有每个项目翻译的表格,比方说,例如
Content
ContentTranslation
Run Code Online (Sandbox Code Playgroud)
要么
Category
CategoryTranslation
Run Code Online (Sandbox Code Playgroud)
等等.
现在我在做什么?我只是从数据库中获取所有翻译的项目,然后我迭代每一个,以根据当前用户的本地查找正确的翻译,如果我找到正确的本地我设置到主要对象,该页面的翻译渲染,否则我只是得到标记为"默认"的翻译.
但是,对于大量对象和翻译,服务器响应时间可能会增长,即使用户可能没有注意到,我也不希望这样.
那么,这个用例有没有好的做法呢?例如,一些特定的查询说"选择带语言环境的翻译",但是如果你没有找到它只是设置了一个"默认"标志?
现在,我正在使用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;
}
}
Run Code Online (Sandbox Code Playgroud)
因此,在我的控制器中,我迭代翻译,找到具有正确语言环境的语言并填充"currentLocale"属性,在我的页面中我只是采用它并且用户获得正确的语言.
我希望我一直很清楚,而不是凌乱,但如果你需要更多的信息,我会很高兴告诉你更多.
M4N*_*M4N 36
一些注意事项:
在我的应用程序中,我使用两种不同的方法来加载多语言数据,具体取决于用例:
在用户输入数据或编辑现有数据(例如带有翻译的产品)的情况下,我使用的方法与您在问题中所示的方法相同,例如:
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;}
}
Run Code Online (Sandbox Code Playgroud)
即我会让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;}
}
Run Code Online (Sandbox Code Playgroud)
要加载此数据,我使用不同的查询(或存储过程).例如,要加载具有@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
Run Code Online (Sandbox Code Playgroud)
如果存在该语言的翻译,则以所请求的语言返回产品的标题和描述.如果不存在翻译,则将返回默认语言的标题和说明.
小智 5
对所有表的所有可翻译字段使用公共共享表
在上述方法中,转换表是父表的扩展。因此 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
}
Run Code Online (Sandbox Code Playgroud)
为了加载这些数据,我使用了不同的查询(修改了上面的内容)。例如,要在语言 @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
Run Code Online (Sandbox Code Playgroud)
此查询基于产品的各个字段没有默认翻译的假设
类似的查询可用于从 ProductCategory 中获取记录
| 归档时间: |
|
| 查看次数: |
19741 次 |
| 最近记录: |