优化多语言支持功能

时间:2016-03-01 00:00:44

标签: php sql optimization

我为我的一个项目创建了自己的多语言支持。我有3个不同的数据库表:languageslanguages_variableslanguages_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获取有关登录用户的信息,例如用户选择的语言。

使用该功能,我可以简单地调用languages_variables中带有language('menu_about')的变量(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秒不等。

是否有另一种更好更快的方法来获取所选择的语言字符串?

更新我将加载速度从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等)而不是数据库。但是,用户无法从浏览器翻译网站(如果我不牺牲自己几个小时的血,汗水和眼泪)。

1 个答案:

答案 0 :(得分:0)

首先使用Xhprof分析构建以验证PHP性能。对于SQL调用,Google Chrome有一个内置的Web检查器,可以让您知道每个组件的使用时间(Firebug for Firefox)。

如果您发布日志,那么建议相关的改进领域可能会有很长的路要走。