Play 框架中数据库级别的国际化
JPA 中的多语言字段是否有干净的解决方案?
我考虑这个数据库结构:
表product
+---------+--------------+-----+
| Field | Type | Key |
+---------+--------------+-----+
| id | bigint(20) | PRI |
| price | varchar(255) | |
+---------+--------------+-----+
表product_lang
+------------------+--------------+-----+
| Field | Type | Key |
+------------------+--------------+-----+
| id | bigint(20) | PRI |
| lang | varchar(3) | PRI |
| title | varchar(255) | |
| description | varchar(255) | |
+------------------+--------------+-----+
这是我想使用的类:
@Entity
public class Product {
@Id
public Long id;
public Double price;
public Locale lang;
@Translateable
public String title;
@Translateable
public String description;
}
Is there a clean solution for multilingual fields in JPA?
I think about this db structure:
table product
+---------+--------------+-----+
| Field | Type | Key |
+---------+--------------+-----+
| id | bigint(20) | PRI |
| price | varchar(255) | |
+---------+--------------+-----+
table product_lang
+------------------+--------------+-----+
| Field | Type | Key |
+------------------+--------------+-----+
| id | bigint(20) | PRI |
| lang | varchar(3) | PRI |
| title | varchar(255) | |
| description | varchar(255) | |
+------------------+--------------+-----+
And this is the class I would like to use:
@Entity
public class Product {
@Id
public Long id;
public Double price;
public Locale lang;
@Translateable
public String title;
@Translateable
public String description;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不太确定您是否可以按照您建议的方式将一个实体映射到两个表。与您想要的类似,您可以将翻译存储为:
其中键是用户正在浏览的语言的字符串,值是要显示的标题。这将在数据库中创建两个表并允许快速访问翻译。
I'm not really sure you can map one Entity to two tables the way you propose. Similar to what you want, you may store the translations as:
where the key is the string for the Lang the user is browsing with and the value the title to show. This would create two tables in the database and allow for quick access to the translations.
IMO,错误的解决方案。如果你真的想要数据库国际化,我宁愿创建多个数据库,而不是一个包含所有数据的大数据库。
只需检查语言即可获得正确的数据库连接。
IMO, wrong solution. If you really want to have db internationalisation, I would rather create multiple databases rather than one big containing all the data.
Simply check langage in order to get right DB connection.