简体   繁体   English

PHP和MySQL - 处理不同数据库语言内容的最佳方式

[英]PHP & MySQL - Best way to handle different database language contents

The question is as follows : what's the best way to handle different database language contents? 问题如下:处理不同数据库语言内容的最佳方法是什么? Suppose you have a shopping cart whose products may have several languages, what would be the best approach to build the db scheme? 假设您的购物车的产品可能有多种语言,那么构建数据库方案的最佳方法是什么?

Cheers! 干杯!

I would store a unique translation key in the table, and then either have another table or perhaps translation files so that you can reference the translation from the key. 我会在表格中存储一个唯一的翻译密钥,然后要么有另一个表格,要么翻译文件,以便您可以从密钥中引用翻译。

table product : product

id: 15
name: product.foo_widget
attr: ...
... etc

table translation : 表格translation

lang: en
key: product.foo_widget
trans: Foo Widget

Have the language of the current user as a property of your user object (or whatever pattern you use for user tracking). 将当前用户的语言作为用户对象的属性(或用于用户跟踪的任何模式)。 When you query the DB for strings, add a conditional to pull the language of the current user if it is available. 在查询数据库中的字符串时,添加条件以拉出当前用户的语言(如果可用)。

In this example, I put the language_id in $_SESSION ; 在这个例子中,我将language_id放在$_SESSION ; I usually would not do user tracking in this fashion but I believe the concept is illustrated. 我通常不会以这种方式进行用户跟踪,但我相信这个概念已经说明了。 In your database, each product would have multiple entries for each language with 0 being the default of the site (presumably English). 在您的数据库中,每种产品对每种语言都有多个条目,其中0表示站点的默认值(可能是英语)。 The query will grab the language-specific line item or fall back to the site default if there isn't a language-specific item available. 如果没有可用的语言特定项,查询将获取特定于语言的行项目或回退到站点默认值。

// the id of the item you're after
$item_id = 1;
$sql = 'SELECT id, name FROM product_names WHERE item_id = '.$item_id.' AND (language_id = '.$_SESSION['user']['language'].' OR language_id = 0) ORDER BY language_id DESC LIMIT 1';

I use this same concept for general strings around the site - I have a database table called "content_strings" with the fields id, text, and language_id. 我对网站周围的一般字符串使用相同的概念 - 我有一个名为“content_strings”的数据库表,其中包含字段id,text和language_id。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM