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?
问题是:处理不同数据库语言内容的最佳方式是什么?假设您有一个购物车,其产品可能具有多种语言,那么构建db方案的最佳方法是什么?
Cheers!
干杯!
2 个解决方案
#1
4
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
:
表的产品:
id: 15
name: product.foo_widget
attr: ...
... etc
table translation
:
表的翻译:
lang: en
key: product.foo_widget
trans: Foo Widget
#2
2
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.
将当前用户的语言作为用户对象的属性(或用于用户跟踪的任何模式)。在查询DB以获取字符串时,如果当前用户的语言可用,则添加一个条件来提取该语言。
In this example, I put the language_id in $_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). 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.
在本例中,我将语言_id放在$_SESSION中;我通常不会用这种方式进行用户跟踪,但我相信这个概念已经说明了。在您的数据库中,每个产品对每种语言都有多个条目,0是站点的默认值(可能是英语)。查询将获取特定于语言的行项,如果没有特定于语言的项可用,则返回到站点默认值。
// 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、文本和语言_id。
#1
4
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
:
表的产品:
id: 15
name: product.foo_widget
attr: ...
... etc
table translation
:
表的翻译:
lang: en
key: product.foo_widget
trans: Foo Widget
#2
2
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.
将当前用户的语言作为用户对象的属性(或用于用户跟踪的任何模式)。在查询DB以获取字符串时,如果当前用户的语言可用,则添加一个条件来提取该语言。
In this example, I put the language_id in $_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). 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.
在本例中,我将语言_id放在$_SESSION中;我通常不会用这种方式进行用户跟踪,但我相信这个概念已经说明了。在您的数据库中,每个产品对每种语言都有多个条目,0是站点的默认值(可能是英语)。查询将获取特定于语言的行项,如果没有特定于语言的项可用,则返回到站点默认值。
// 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、文本和语言_id。