如何在数据库(MySQL)中存储多语言数据?
我有一张项目表,我们将其称为“ITEMS”。现在我需要以多种语言存储每个项目的名称。我正在寻找最有效的方法来做到这一点。到目前为止,我有以下可能的解决方案,但由于某些特定原因,我真的想选择一个。
为了问题起见,让事情变得简单,ITEMS 表的结构:
id INT PRIMARY KEY
name VARCHAR(20)
现在第一个最简单的解决方案可能是在 ITEMS 表中为每种语言添加列,例如 name_english、name_german、name_spanish 等...但这可能会涉及大量工作在 PHP 中添加新语言时。
第二个解决方案 - 我将创建表 LANGS:
id INT PRIMARY KEY,
language VARCHAR(10)
和表 ITEMS_LANGS:
id_items INT
id_langs INT
translated_name VARCHAR(20)
因此每次我都必须连接表 ITEMS 和 ITEMS_LANGS,但添加新语言会更容易。
我应该选择哪种解决方案,或者您有更好的解决方案吗?预先感谢,希望我已经足够详细地解释了我的问题。
I have one table of items, let's call it ITEMS. Now I need to store the name of each item in multiple languages. I am looking for most efficient way to do this. So far I have these possible solutions below, but I really would like to pick one for some specific reason.
For question sake, lets keep things simple, structure of ITEMS table:
id INT PRIMARY KEY
name VARCHAR(20)
Now first easiest solution would probably be adding columns for each language in ITEMS table, e.g. name_english, name_german, name_spanish, etc... But this would probably involve a lot of work in PHP when adding new languages.
Second solution - I would create table LANGS:
id INT PRIMARY KEY,
language VARCHAR(10)
and table ITEMS_LANGS:
id_items INT
id_langs INT
translated_name VARCHAR(20)
so each time I would have to join the tables ITEMS and ITEMS_LANGS, but it would be much more easier to add new languages.
Which solution should I choose, or do you have better solution ? Thanks in advance, hope I have explained my problem in enough detail.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果满足以下条件,第二个选项更好:
我将用 TEXTS 更改 ITEMS_LANGS(更通用):
The second option is better if:
I'll change ITEMS_LANGS by TEXTS (more generic):
我会为 ITEMS 创建一个表,正如您在开始时所描述的那样,以 id 作为主键,然后为每种语言创建单独的表(与 id 作为外键和名称相同)。
这将使您更容易添加不同的语言/删除一些/添加项目,并且如果您需要的语言中不存在该名称......等等,那么仍然有一个后备值......
I would create one table for ITEMS as you describe at the beginning, with an id as the primary key and then separate tables for each language (same with id as a foreign key and name).
This will make it easier for you to add different language / remove some / add items and still have a fallback value if the name does not exist in the language you need...etc...