摘 要:在設(shè)計(jì)和開發(fā)一個(gè)多語言版本的網(wǎng)站時(shí),通常把每一種語言的內(nèi)容保存在同一數(shù)據(jù)庫中,在數(shù)據(jù)庫設(shè)計(jì)時(shí)有多種方法,每一種方法根據(jù)網(wǎng)站的需求和數(shù)據(jù)的大小可以不同。本文通過PHP+MYSQL實(shí)現(xiàn)三種不同的多語言網(wǎng)站設(shè)計(jì)和開發(fā)方案。
關(guān)鍵詞:網(wǎng)站開發(fā);數(shù)據(jù)庫設(shè)計(jì);MYSQL;PHP
中圖分類號(hào): TP311 文獻(xiàn)標(biāo)識(shí)碼:A
1 引言(Introduction)
隨著全球經(jīng)濟(jì)一體化,我國各行各業(yè)融入國際社會(huì)的步伐在加快,開發(fā)一個(gè)多國語言網(wǎng)站是向國際社會(huì)展示自己的有效途徑。在設(shè)計(jì)和開發(fā)一個(gè)多語言版本的網(wǎng)站時(shí),我們通常把每一種語言的內(nèi)容保存在同一數(shù)據(jù)庫中,在數(shù)據(jù)庫設(shè)計(jì)時(shí)有多種方法,每一種方法根據(jù)網(wǎng)站的需求和數(shù)據(jù)的大小可以不同[1]。本文通過PHP+MYSQL實(shí)現(xiàn)三種不同的多語言網(wǎng)站設(shè)計(jì)和開發(fā)方案。
2 字段列方法(Column approach)
這是最簡單的方法,創(chuàng)建一個(gè)數(shù)據(jù)庫,如Test,數(shù)據(jù)庫中建立存放網(wǎng)站內(nèi)容的表App_product,為每種語言添加一個(gè)文本列,創(chuàng)建該表的MYSQL語句如下[2]:
CREATE TABLE app_product (
`id` int(10) NOT NULL AUTO_INCREMENT,
`date_created` datetime NOT NULL,
`price` decimal(10,2) unsigned NOT NULL DEFAULT '0.00',
`title_cn` varchar(255) NOT NULL,
`title_en` varchar(255) NOT NULL,
`title_jp` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
);
向表App_product中輸入網(wǎng)站內(nèi)容,如圖1所示。
圖1 字段列方法數(shù)據(jù)庫MYSQL截圖
Fig.1 MYSQL screenshot of column approach
用PHP實(shí)現(xiàn)查詢的代碼:
<?php
// 檢索所有語言的標(biāo)題列
$sql = "SELECT * FROM `app_product` WHERE 1";
if($result = mysql_query($sql)){
if($row = mysql_fetch_assoc($result)){
echo "Chinese: ".$row["title_cn"]."
";
echo " English: ".$row["title_en"]."
";
echo "Japanese: ".$row["title_jp"]."
";
}
}
//根據(jù)選擇的語言,顯示檢索結(jié)果
$sql = "SELECT `title_".$_SESSION['current_language']."` as `title`
FROM `app_product`";
if($result = mysql_query($sql)){
if($row = mysql_fetch_assoc($result)){
echo "Current Language: ".$row["title"];
}
}
?>
這種方法的優(yōu)點(diǎn)是:容易實(shí)現(xiàn),每個(gè)語言在數(shù)據(jù)庫表中為一列內(nèi)容,沒有重復(fù)內(nèi)容。缺點(diǎn)是:數(shù)據(jù)庫維護(hù)困難,適合于兩三種語言。
3 記錄行方法(Multirow approach)
這種方法與上面的字段列方法相似,只是每種語言的內(nèi)容占據(jù)一行,創(chuàng)建App_product_row表的MYSQL語句如下:
CREATE TABLE app_product_row (
`id` int(10) NOT NULL AUTO_INCREMENT,
`date_created` datetime NOT NULL,
`price` decimal(10,2) unsigned NOT NULL DEFAULT '0.00',
`language_id` varchar(2) NOT NULL,
`title` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
);
向表App_product中輸入網(wǎng)站內(nèi)容,如圖2所示。
圖2 記錄行方法數(shù)據(jù)庫MYSQL截圖
Fig.2 MYSQL Screenshot ofMultirow Approach
用PHP實(shí)現(xiàn)查詢的代碼:
<?php
// 檢索語言列
$sql = "SELECT * FROM `app_product` WHERE `id` = 1";
if($result = mysql_query($sql)){
while($row = mysql_fetch_assoc($result)){
echo "Language (".$row["language_id"]."): ".$row["title"]."
";endprint
}
}
//根據(jù)選擇的語言,顯示檢索結(jié)果
$sql = "SELECT `title`
FROM `app_product_row`
WHERE `language_id` = '".$_SESSION['current_language']."'";
if($result = mysql_query($sql)){
if($row = mysql_fetch_assoc($result)){
echo "Current Language: ".$row["title"];
}
}
?>
這種方法的優(yōu)點(diǎn)是:容易實(shí)現(xiàn),每一行就是一種語言的內(nèi)容。缺點(diǎn)是:數(shù)據(jù)大量重復(fù),數(shù)據(jù)庫維護(hù)困難,如要改變價(jià)格,所有語言的價(jià)格都要修改。
4 附加轉(zhuǎn)換表的方法(Additional translation table
approach)
這種方法有效解決了上述問題,并且易于維護(hù)。附加轉(zhuǎn)換表app_product_translation_4的內(nèi)容是每種語言對應(yīng)的信息,而表app_language_4和app_product_4中存放的是公共信息[3]。創(chuàng)建表的MYSQL語句如下:
CREATE TABLE IF NOT EXISTS `app_product_4` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date_created` datetime NOT NULL,
`price` decimal(10,2) NOT NULL DEFAULT '0.00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `app_product_translation_4` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`product_id` int(11) NOT NULL DEFAULT '0',
`language_code` char(2) NOT NULL,
`title` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`description` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
KEY `translation_id` (`product_id`),
KEY `language_code` (`language_code`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `app_language_4` (
`code` char(2) NOT NULL,
`name` varchar(20) NOT NULL,
PRIMARY KEY (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
分別向表app_product_translation_4、app_language_4和app_product_4中輸入網(wǎng)站內(nèi)容,及其三個(gè)表之間的關(guān)系,如圖3所示。
圖3 附加轉(zhuǎn)換表的方法數(shù)據(jù)庫MYSQL截圖及其關(guān)系
Fig.3 MYSQL Screenshot and Relation ofAdditional
Translation Table Approach
用PHP實(shí)現(xiàn)查詢的代碼:
<?php
// 檢索所有語言標(biāo)題列
$sql = "SELECT p.*, pt.title, pt.description, l.name as language_name
FROM `app_product_4` p
INNER JOIN `app_product_translation_4` pt ON p.id = pt.product_id
INNER JOIN `app_language_4` l ON pt.language_code = l.code
WHERE p.id = 1";
if($result = mysql_query($sql)){
while($row = mysql_fetch_assoc($result)){
echo "Language (".$row["language_name"]."): ".$row["title"]."
";
}
}
// 根據(jù)選擇的語言檢索符合條件的結(jié)果
$sql = "SELECT p.*, pt.title, pt.description
FROM `app_product_4` p
INNER JOIN `app_product_translation_4` pt ON p.id = pt.product_id
WHERE p.id = 1 AND pt.language_code = '".$_SESSION['current_language']."'";
if($result = mysql_query($sql)){
if($row = mysql_fetch_assoc($result)){
echo "Current Language: ".$row["title"];
}
}
?>
這種方法的優(yōu)點(diǎn)是:更符合數(shù)據(jù)庫設(shè)計(jì)的規(guī)范,增加新的語言更容易實(shí)現(xiàn),查詢的效率更高。缺點(diǎn)是:表的數(shù)量增加,需要增加轉(zhuǎn)換表。
5 結(jié)論(Conclusion)
在多語言網(wǎng)站設(shè)計(jì)時(shí),上述數(shù)據(jù)庫設(shè)計(jì)的方法不是唯一的選擇,也可采用其他方法,要根據(jù)項(xiàng)目需求而定,但上述方法是十分常用而有效的。當(dāng)網(wǎng)站的內(nèi)容涉及的語言不多,又固定那幾種語言時(shí),列或行方法較為簡單實(shí)用,否則使用增加附加表的方法。
參考文獻(xiàn)(References)
[1] 張菲菲,薛賀,李建良.多語言Web網(wǎng)站的設(shè)計(jì)與實(shí)現(xiàn)[J].微電
子學(xué)與計(jì)算機(jī),2008,25(5):43.
[2] 主福洋,郭坤.基于PHP技術(shù)的網(wǎng)站建設(shè)[J].軟件工程師,2013,
1:60.
[3] MathewHillier.Theroleofculturalcontextinmultilingualwe
bsiteusability[J].ElectronicCommerceResearchandApplications,
2003(2):2-14.
作者簡介:
常中華(1968-),男,碩士,副教授.研究領(lǐng)域:計(jì)算機(jī)應(yīng)用
技術(shù).endprint
}
}
//根據(jù)選擇的語言,顯示檢索結(jié)果
$sql = "SELECT `title`
FROM `app_product_row`
WHERE `language_id` = '".$_SESSION['current_language']."'";
if($result = mysql_query($sql)){
if($row = mysql_fetch_assoc($result)){
echo "Current Language: ".$row["title"];
}
}
?>
這種方法的優(yōu)點(diǎn)是:容易實(shí)現(xiàn),每一行就是一種語言的內(nèi)容。缺點(diǎn)是:數(shù)據(jù)大量重復(fù),數(shù)據(jù)庫維護(hù)困難,如要改變價(jià)格,所有語言的價(jià)格都要修改。
4 附加轉(zhuǎn)換表的方法(Additional translation table
approach)
這種方法有效解決了上述問題,并且易于維護(hù)。附加轉(zhuǎn)換表app_product_translation_4的內(nèi)容是每種語言對應(yīng)的信息,而表app_language_4和app_product_4中存放的是公共信息[3]。創(chuàng)建表的MYSQL語句如下:
CREATE TABLE IF NOT EXISTS `app_product_4` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date_created` datetime NOT NULL,
`price` decimal(10,2) NOT NULL DEFAULT '0.00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `app_product_translation_4` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`product_id` int(11) NOT NULL DEFAULT '0',
`language_code` char(2) NOT NULL,
`title` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`description` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
KEY `translation_id` (`product_id`),
KEY `language_code` (`language_code`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `app_language_4` (
`code` char(2) NOT NULL,
`name` varchar(20) NOT NULL,
PRIMARY KEY (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
分別向表app_product_translation_4、app_language_4和app_product_4中輸入網(wǎng)站內(nèi)容,及其三個(gè)表之間的關(guān)系,如圖3所示。
圖3 附加轉(zhuǎn)換表的方法數(shù)據(jù)庫MYSQL截圖及其關(guān)系
Fig.3 MYSQL Screenshot and Relation ofAdditional
Translation Table Approach
用PHP實(shí)現(xiàn)查詢的代碼:
<?php
// 檢索所有語言標(biāo)題列
$sql = "SELECT p.*, pt.title, pt.description, l.name as language_name
FROM `app_product_4` p
INNER JOIN `app_product_translation_4` pt ON p.id = pt.product_id
INNER JOIN `app_language_4` l ON pt.language_code = l.code
WHERE p.id = 1";
if($result = mysql_query($sql)){
while($row = mysql_fetch_assoc($result)){
echo "Language (".$row["language_name"]."): ".$row["title"]."
";
}
}
// 根據(jù)選擇的語言檢索符合條件的結(jié)果
$sql = "SELECT p.*, pt.title, pt.description
FROM `app_product_4` p
INNER JOIN `app_product_translation_4` pt ON p.id = pt.product_id
WHERE p.id = 1 AND pt.language_code = '".$_SESSION['current_language']."'";
if($result = mysql_query($sql)){
if($row = mysql_fetch_assoc($result)){
echo "Current Language: ".$row["title"];
}
}
?>
這種方法的優(yōu)點(diǎn)是:更符合數(shù)據(jù)庫設(shè)計(jì)的規(guī)范,增加新的語言更容易實(shí)現(xiàn),查詢的效率更高。缺點(diǎn)是:表的數(shù)量增加,需要增加轉(zhuǎn)換表。
5 結(jié)論(Conclusion)
在多語言網(wǎng)站設(shè)計(jì)時(shí),上述數(shù)據(jù)庫設(shè)計(jì)的方法不是唯一的選擇,也可采用其他方法,要根據(jù)項(xiàng)目需求而定,但上述方法是十分常用而有效的。當(dāng)網(wǎng)站的內(nèi)容涉及的語言不多,又固定那幾種語言時(shí),列或行方法較為簡單實(shí)用,否則使用增加附加表的方法。
參考文獻(xiàn)(References)
[1] 張菲菲,薛賀,李建良.多語言Web網(wǎng)站的設(shè)計(jì)與實(shí)現(xiàn)[J].微電
子學(xué)與計(jì)算機(jī),2008,25(5):43.
[2] 主福洋,郭坤.基于PHP技術(shù)的網(wǎng)站建設(shè)[J].軟件工程師,2013,
1:60.
[3] MathewHillier.Theroleofculturalcontextinmultilingualwe
bsiteusability[J].ElectronicCommerceResearchandApplications,
2003(2):2-14.
作者簡介:
常中華(1968-),男,碩士,副教授.研究領(lǐng)域:計(jì)算機(jī)應(yīng)用
技術(shù).endprint
}
}
//根據(jù)選擇的語言,顯示檢索結(jié)果
$sql = "SELECT `title`
FROM `app_product_row`
WHERE `language_id` = '".$_SESSION['current_language']."'";
if($result = mysql_query($sql)){
if($row = mysql_fetch_assoc($result)){
echo "Current Language: ".$row["title"];
}
}
?>
這種方法的優(yōu)點(diǎn)是:容易實(shí)現(xiàn),每一行就是一種語言的內(nèi)容。缺點(diǎn)是:數(shù)據(jù)大量重復(fù),數(shù)據(jù)庫維護(hù)困難,如要改變價(jià)格,所有語言的價(jià)格都要修改。
4 附加轉(zhuǎn)換表的方法(Additional translation table
approach)
這種方法有效解決了上述問題,并且易于維護(hù)。附加轉(zhuǎn)換表app_product_translation_4的內(nèi)容是每種語言對應(yīng)的信息,而表app_language_4和app_product_4中存放的是公共信息[3]。創(chuàng)建表的MYSQL語句如下:
CREATE TABLE IF NOT EXISTS `app_product_4` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date_created` datetime NOT NULL,
`price` decimal(10,2) NOT NULL DEFAULT '0.00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `app_product_translation_4` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`product_id` int(11) NOT NULL DEFAULT '0',
`language_code` char(2) NOT NULL,
`title` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`description` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
KEY `translation_id` (`product_id`),
KEY `language_code` (`language_code`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `app_language_4` (
`code` char(2) NOT NULL,
`name` varchar(20) NOT NULL,
PRIMARY KEY (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
分別向表app_product_translation_4、app_language_4和app_product_4中輸入網(wǎng)站內(nèi)容,及其三個(gè)表之間的關(guān)系,如圖3所示。
圖3 附加轉(zhuǎn)換表的方法數(shù)據(jù)庫MYSQL截圖及其關(guān)系
Fig.3 MYSQL Screenshot and Relation ofAdditional
Translation Table Approach
用PHP實(shí)現(xiàn)查詢的代碼:
<?php
// 檢索所有語言標(biāo)題列
$sql = "SELECT p.*, pt.title, pt.description, l.name as language_name
FROM `app_product_4` p
INNER JOIN `app_product_translation_4` pt ON p.id = pt.product_id
INNER JOIN `app_language_4` l ON pt.language_code = l.code
WHERE p.id = 1";
if($result = mysql_query($sql)){
while($row = mysql_fetch_assoc($result)){
echo "Language (".$row["language_name"]."): ".$row["title"]."
";
}
}
// 根據(jù)選擇的語言檢索符合條件的結(jié)果
$sql = "SELECT p.*, pt.title, pt.description
FROM `app_product_4` p
INNER JOIN `app_product_translation_4` pt ON p.id = pt.product_id
WHERE p.id = 1 AND pt.language_code = '".$_SESSION['current_language']."'";
if($result = mysql_query($sql)){
if($row = mysql_fetch_assoc($result)){
echo "Current Language: ".$row["title"];
}
}
?>
這種方法的優(yōu)點(diǎn)是:更符合數(shù)據(jù)庫設(shè)計(jì)的規(guī)范,增加新的語言更容易實(shí)現(xiàn),查詢的效率更高。缺點(diǎn)是:表的數(shù)量增加,需要增加轉(zhuǎn)換表。
5 結(jié)論(Conclusion)
在多語言網(wǎng)站設(shè)計(jì)時(shí),上述數(shù)據(jù)庫設(shè)計(jì)的方法不是唯一的選擇,也可采用其他方法,要根據(jù)項(xiàng)目需求而定,但上述方法是十分常用而有效的。當(dāng)網(wǎng)站的內(nèi)容涉及的語言不多,又固定那幾種語言時(shí),列或行方法較為簡單實(shí)用,否則使用增加附加表的方法。
參考文獻(xiàn)(References)
[1] 張菲菲,薛賀,李建良.多語言Web網(wǎng)站的設(shè)計(jì)與實(shí)現(xiàn)[J].微電
子學(xué)與計(jì)算機(jī),2008,25(5):43.
[2] 主福洋,郭坤.基于PHP技術(shù)的網(wǎng)站建設(shè)[J].軟件工程師,2013,
1:60.
[3] MathewHillier.Theroleofculturalcontextinmultilingualwe
bsiteusability[J].ElectronicCommerceResearchandApplications,
2003(2):2-14.
作者簡介:
常中華(1968-),男,碩士,副教授.研究領(lǐng)域:計(jì)算機(jī)應(yīng)用
技術(shù).endprint