【问题标题】:Drupal - redirection after language changeDrupal - 语言更改后的重定向
【发布时间】:2011-02-16 01:45:28
【问题描述】:

我想在我的 Drupal 网站上更改语言时将用户重定向到主页。 这有可能吗?

【问题讨论】:

    标签: drupal redirect


    【解决方案1】:

    您应该将用户的当前语言存储在会话中,然后如果更改,则重定向到首页,然后设置为该会话更改的语言。
    在您的 template.php 中:

    /**
     * Override or insert variables into the page templates.
     *
     * @param $vars
     *   An array of variables to pass to the theme template.
     * @param $hook
     *   The name of the template being rendered ("page" in this case.)
     */
    function THEMENAME_preprocess_page(&$vars, $hook) {
      global $language;
      $currentlanguage = isset($_SESSION['currentlanguage']) ? $_SESSION['currentlanguage'] : $language->language;
      if ($language->language != $currentlanguage) {
        drupal_goto(url().'/'.$language->language); //goto current language version, if you use http://SITEURL/{languagecode} version, otherwise change it to appropriate.
      }
    }
    

    【讨论】:

    • 我应该把这段代码放在哪里,在哪里设置会话?我对 Drupal 很陌生。很抱歉成为新手。
    • 将代码放入主题的 template.php 文件中。请务必将上面 sn-p 中的“THEMENAME”替换为您的主题名称(或“phptemplate”)。
    • 还要检查 THEMENAME_preprocess_page 函数是否存在(当然将 THEMENAME 更改为您的主题名称)。如果它在那里,只需将代码添加到此...
    • 我添加了代码,但没有发生任何事情。如果没有像这样设置,我还尝试更改它以将语言添加到会话中:code function salamanderskins_preprocess_page($vars, $hook) {全球$语言; $currentlanguage = isset($_SESSION['currentlanguage']) ? $_SESSION['currentlanguage'] : $language->language; if ($language->language != $currentlanguage) { drupal_goto(url().'/'.$language->language); //转到当前语言版本,如果您使用SITEURL{languagecode}版本,否则将其更改为适当的。 } $_SESSION['currentlanguage'] = $language->language; }
    • drupal_goto() 不接受语言代码 (D7)。
    【解决方案2】:

    在 drupal 6 中写入 template.php:

    function THEMENAME_preprocess_page(&$vars, $hook) {
      global $language;
      $previouselanguage = isset($_SESSION['previouselanguage']) ? $_SESSION['previouselanguage'] : $language->language;
      $_SESSION['previouselanguage'] = $language->language;
      if ($language->language != $previouselanguage) {
        drupal_goto('');
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-04
      相关资源
      最近更新 更多