数据库表中的多语言字段

数据库表中的多语言字段

本文介绍了数据库表中的多语言字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要支持多语言界面的应用程序,需要五种语言。对于接口的主要部分,可以使用标准的Res​​ourceBundle方法来处理此问题。



但是,数据库包含许多表,其元素包含可读的名称,描述,摘要等。需要使用所有五种语言输入这些表。 / p>

虽然我想我可以简单地在每个表格上有字段,如

  NameLang1 
NameLang2
...

我觉得这会导致一个显着的当编写bean代表每个表时,大量相同代码的数量。



从纯对象的角度来看,解决方案是简单的。每个类只有一个Text对象,其中包含每种语言的相关文本。这更有帮助,因为只有一种语言是强制性的,其他的则有后备规则(例如,如果语言4缺少返回语言2,这种语言可能会退回到强制性语言1)。



不幸的是,将其映射回关系数据库,意味着我用一个表,其中约10-12个其他表FK(有些表实际上有多个FK)结束。



这种方法似乎有效,我已经能够使用Hibernate将数据映射到POJO。关于你唯一不能做的事情就是将Text对象映射到其父对象(因为你无法知道应该链接哪个表),但是几乎没有必要这样做。



所以,总体而言,这似乎是有效的,但是如果有多个表引用一个这样的表,那就觉得错误了。任何人有一个更好的主意?



如果重要的是我使用MySQL ...

解决方案

我不得不这样做一次...一些表的多语言文本...我不知道如果我找到最好的解决方案,但我做的是具有与语言无关的信息和然后是一个带有所有多语言字段的小孩桌。对于默认语言,子表中至少需要一条记录;更多的语言可以稍后添加。



在Hibernate中,您可以将来自子表的信息映射为Map,并获取所需语言的信息,实现回退就像你说的那样你的POJO。您可以为多语言字段使用不同的getter,内部调用回退方法以获取所需语言的适当子对象,然后返回所需的字段。



方法使用更多的表(每个表需要多语言信息一个额外的表),但性能要好得多,以及我认为的维护...


I have an application that needs to support a multilingual interface, five languages to be exact. For the main part of the interface the standard ResourceBundle approach can be used to handle this.

However, the database contains numerous tables whose elements contain human readable names, descriptions, abstracts etc. It needs to be possible to enter each of these in all five languages.

While I suppose I could simply have fields on each table like

NameLang1
NameLang2
...

I feel that that leads to a significant amount of largely identical code when writing the beans the represent each table.

From a purely object oriented point of view the solution is however simple. Each class simply has a Text object that contains the relevant text in each of the languages. This is further helpful in that only one of the language is mandated, the others have fallback rules (e.g. if language 4 is missing return language 2 which fall back to language 1 which is mandatory).

Unfortunately, mapping this back to a relational database, means that I wind up with a single table that some 10-12 other tables FK to (some tables have more than one FK to it in fact).

This approach seems to work and I've been able to map the data to POJOs with Hibernate. About the only thing you cant do is map from a Text object to its parent (since you have no way of knowing which table you should link to), but then there is hardly any need to do that.

So, overall this seems to work but it just feels wrong to have multiple tables reference one table like this. Anyone got a better idea?

If it matters I'm using MySQL...

解决方案

I had to do that once... multilingual text for some tables... I don't know if I found the best solution but what I did was have the table with the language-agnostic info and then a child table with all the multilingual fields. At least one record was required in the child table, for the default language; more languages could be added later.

On Hibernate you can map the info from the child tables as a Map, and get the info for the language you want, implementing the fallback on your POJO like you said. You can have different getters for the multilingual fields, that internally call the fallback method to get the appropiate child object for the needed language and then just return the required field.

This approach uses more table (one extra table for every table that needs multilingual info) but the performance is much better, as well as the maintenance I think...

这篇关于数据库表中的多语言字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 18:30