“一种解决方案是为要翻译的字符串使用参考 (id),并且每个可翻译列都有一个包含主键、字符串 id、语言 id 和翻译的表。”
我实现了一次,我所做的是我采用现有的数据库模式,查找所有具有可翻译文本列的表,并且对于每个这样的表,我创建了一个单独的表,其中只包含那些文本列,以及一个额外的语言 id 和 id它到原始表中的“数据”行。所以如果我有:
create table product (
id int not null primary key
, sku varchar(12) not null
, price decimal(8,2) not null
, name varchar(64) not null
, description text
)
我会创建:
create table product_text (
product_id int not null
, language_id int not null
, name varchar(64) not null
, description text
, primary key (product_id, language_id)
, foreign key (product_id) references product(id)
, foreign key (language_id) references language(id)
)
我会这样查询:
SELECT product.id
, COALESCE(product_text.name, product.name) name
, COALESCE(product_text.description, product.description) description
FROM product
LEFT JOIN product_text
ON product.id = product_text.product_id
AND 10 = product_text.language_id
(10 恰好是您现在感兴趣的语言 ID。)
如您所见,原始表格保留了文本列 - 这些作为默认值,以防当前语言没有可用的翻译。
因此无需为每个文本列创建单独的表,只需为所有文本列创建一个表(每个原始表)
就像其他人指出的那样,JSON 想法的问题是几乎不可能查询它,这反过来意味着无法仅提取特定时间所需的翻译。