我爲自己的一個項目創建了自己的多語言支持。我有3個不同的數據庫表:languages
,languages_variables
和languages_words
(如下所示)。優化多語言支持功能
CREATE TABLE IF NOT EXISTS `languages` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_user` int(11) NOT NULL,
`data_name` text NOT NULL,
`data_currency` text NOT NULL,
`is_standard` tinyint(4) NOT NULL,
`data_note` text NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
)
CREATE TABLE IF NOT EXISTS `languages_variables` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`data_name` text NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
)
CREATE TABLE IF NOT EXISTS `languages_words` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_language` int(11) NOT NULL,
`id_variable` int(11) NOT NULL,
`data_name` text NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
)
當我想提取特定字符串的網站,我將使用一個名爲language()
功能(如下圖所示)。下面代碼中的$user
獲取有關已登錄用戶的信息,例如用戶選擇的語言。 (menu_about
只是一個變量的例子)。
function language($variable) {
global $user;
# DATABAS (kontrollera)
$check_language = sql("SELECT COUNT(lv.data_name)
FROM languages AS l
JOIN languages_variables AS lv
JOIN languages_words AS lw
ON l.id = lw.id_language
WHERE lv.data_name = :variable
AND lw.id_language = :idlanguage
", Array('idlanguage' => ($user['id_language'] == 0 ? '1' : (int)$user['id_language']),
'variable' => $variable), 'count');
# DATABAS (räkna)
$count_words = sql("SELECT COUNT(lw.id)
FROM languages_words AS lw
JOIN languages_variables AS lv
ON lw.id_variable = lv.id
WHERE lw.id_language = :idlanguage
AND lv.data_name = :variable
", Array('idlanguage' => ($user['id_language'] == 0 ? '1' : (int)$user['id_language']),
'variable' => $variable), 'count');
# KONTROLL
if($check_language == 0 OR $count_words == 0) {
return $variable;
# KONTROLL
} else {
# DATABAS (hämta)
$get_languages = sql("SELECT *,
lv.data_name AS name_variable,
lw.data_name AS name_translation
FROM languages AS l
JOIN languages_variables AS lv
JOIN languages_words AS lw
ON lv.id = lw.id_variable
WHERE lv.data_name = :variable
AND lw.id_language = :idlanguage
", Array('variable' => $variable,
'idlanguage' => ($user['id_language'] == 0 ? '1' : (int)$user['id_language'])));
# LOOP
foreach($get_languages AS $language) {
# KONTROLL
if($language['name_variable'] == $variable) {
return $language['name_translation'];
# KONTROLL
} else {
return $variable;
}
}
}
}
問題是,網站加載非常緩慢,所有在language()
內的東西。特別是當我將列出數據庫中的很多項目(例如一個循環)。加載速度從2秒到10秒不等。
是否有另一種更好,更快捷的方式來獲得所選的語言字符串?
UPDATE我的加載速度從7-8秒降到3-4秒。但它仍然太慢。
function language($variable) {
# DATABAS (hämta)
$language = sql("SELECT lv.data_name AS name_variable,
lw.data_name AS name_word
FROM languages AS l
JOIN languages_variables AS lv
JOIN languages_words AS lw
ON lv.id = lw.id_variable
WHERE lv.data_name = :variable
AND l.id = :idlanguage
", Array('variable' => $variable,
'idlanguage' => ($user['id_language'] == 0 ? '1' : (int)$user['id_language'])), 'fetch');
return $language['name_word'];
}
我正在考慮使用文件(sv.php,en.php等)而不是數據庫。但是,那麼用戶不能從瀏覽器翻譯網站(如果我不犧牲自己幾個小時的血液,汗水和眼淚)。
謝謝,但我知道問題所在,因爲我已經測試過刪除函數'language()'中的所有內容,只保留'return $ variable;'。之後,該網站的速度更快了。無論如何。我做了你的建議(在Google Chrome瀏覽器中查看檢查員),我只能找到網絡和資源,而不是你所說的。 – Erik
有一個常用的加入基於:https://developers.google.com/speed/pagespeed/ – Oliver
對不起,一個緩慢的激光。我在哪裏可以找到這些日誌?你給我的鏈接只允許我測試我的網站 - 不保存一些日誌。你提到關於網絡檢查員的是什麼?哪裏可以找到測試SQL調用並保存日誌? – Erik