在PHP中停留在同一页面上时切换语言

时间:2018-12-02 19:30:30

标签: php

我是PHP的新手,正在努力寻找解决问题的方法。 基本上,正如标题所述,我想做的是更改语言,但正如下面的代码所示,它将我重定向到index.php。 if(){echo ...}语句使更多内容变得更加复杂。我已经阅读了一些主题,但是找不到正确的答案。所以这是我的header.php:

<?php
if ($lang == 'EN')
{
echo '
<!DOCTYPE html>
 <html lang="EN">
 <head>
    <meta charset="UTF-8">
    <title> iPhone 8 </title>
    <link rel="stylesheet" href="style.css"/>
 </head>
 <body>
 <header>
    <div id="navbar">
        <div id="container">
            <a href="index.php?lang='.$lang.'"><img src="Logos/mylogo1.svg" alt="logo" id="logo"></a>
        </div>
        <ul class="menu">
            <li><a href="index.php?lang='.$lang.'"> Homepage </a></li>
            <li class="dropdown">
            <a href="javascript:void(0)" class="dropbtn"> iPhone 8 </a>
                <div class="dropdown-content">
                    <a href="Review.php?lang='.$lang.'"> Review </a>
                    <a href="Specifications.php?lang='.$lang.'"> Specifications </a>
                </div>
            <li><a href="News.php?lang='.$lang.'"> News </a></li>
            <li class="dropdown">
            <a href="javascript:void(0)" class="dropbtn"> Media </a>
                <div class="dropdown-content">
                    <a href="Photos.php?lang='.$lang.'"> Photos & GIFs</a>
                    <a href="Videos.php?lang='.$lang.'"> Audio & Video </a>
                </div>
            <li><a href="CompareiPhones.php?lang='.$lang.'"> Compare </a></li>
            <li><a href="Contact.php?lang='.$lang.'"> Contact </a></li>
            <li><a class="current" href="index.php?lang=EN"><img class="iconclass" src="Icons/ukicon.ico" alt="ENG"></a></li>
            <li><a href="index.php?lang=SK"><img class="iconclass" src="Icons/slovakicon.ico" alt="SK"></a></li>
        </ul>
    </div>
 </header>';
}

else
{
$lang = 'SK';
echo '
<!DOCTYPE html>
 <html lang="SK">
 <head>
    <meta charset="UTF-8">
    <title> iPhone 8 </title>
    <link rel="stylesheet" href="style.css"/>
 </head>
 <body>
 <header>
    <div id="navbar">
        <div id="container">
            <a href="index.php"><img src="Logos/mylogo1.svg" alt="logo" id="logo"></a>
        </div>
        <ul class="menu">
            <li><a href="index.php?lang='.$lang.'"> D.Stránka </a></li>
            <li class="dropdown">
            <a href="javascript:void(0)" class="dropbtn"> iPhone 8 </a>
                <div class="dropdown-content">
                    <a href="Review.php?lang='.$lang.'"> Recenzia </a>
                    <a href="Specifications.php?lang='.$lang.'"> Špecifikácie </a>
                </div>
            <li><a href="News.php?lang='.$lang.'"> Novinky </a></li>
            <li class="dropdown">
            <a href="javascript:void(0)" class="dropbtn"> Média </a>
                <div class="dropdown-content">
                    <a href="Photos.php?lang='.$lang.'"> Fotky & GIFy</a>
                    <a href="Videos.php?lang='.$lang.'"> Audio & Video </a>
                </div>
            <li><a href="CompareiPhones.php?lang='.$lang.'"> Porovnanie </a></li>
            <li><a href="Contact.php?lang='.$lang.'"> Kontakt </a></li>
            <li><a href="index.php?lang=EN"><img class="iconclass" src="Icons/ukicon.ico" alt="ENG"></a></li>
            <li><a class="current"href="index.php?lang=SK"><img class="iconclass" src="Icons/slovakicon.ico" alt="SK"></a></li>
        </ul>
    </div>
 </header>';

}
?>

1 个答案:

答案 0 :(得分:1)

您可能想做的是:

1)具有一个可根据当前语言翻译单词的功能(也可能有一个库/服务器实现为您完成此操作,但是对于有限的单词,此方法可能会很好地工作) 2)在会话中存储语言 3)正如其他人所建议的那样,请勿复制整个代码块

/config.php

<?php
# Set a root define for easy root access
define('ROOT_DIR', __DIR__);
# Set a root define folder for including functions
define('FUNCTIONS', ROOT_DIR.'/functions');
# Include our simple translator function (defined below)
include_once(FUNCTIONS.'/__.php');
# Include our language retrieval function
include_once(FUNCTIONS.'/getLang.php');
# Start session to store lang
session_start();

/ functions / __。php

function __($text, $lang = false)
{
    # If lang is set, try to set from session
    if(empty($lang))
        $lang = (!empty($_SESSION['lang']))? $_SESSION['lang'] : 'EN'
    # Just return the text if the language is EN
    if($lang == 'EN')
        return $text;
    # Include our translator array
    if(!is_file($file = ROOT_DIR.'trans_'.$lang.'.php'))
        return $text;
    include($file);
    # See if there is a translation, if not just return the input
    return (isset($trans[$text]))? $trans[$text] : $text;
}

/functions/getLang.php

function getLang($default = 'EN')
{
    # Return the language if set, else send back default
    return (!empty($_SESSION['lang']))? $_SESSION['lang'] : $default;
}

/trans_SK.php

<?php
$trans = [
    'Homepage' => 'D.Stránka',
    'Review' => 'Recenzia',
    'Specifications' => 'Špecifikácie',
    'News' => 'Novinky',
    'Media' => 'Média',
    'Photos and GIFs' => 'Fotky & GIFy',
    'Compare' => 'Porovnanie',
    'Contact' => 'Kontakt'
];

标题:

<?php
# Add config to top level pages
require_once(__DIR__.'/config.php');
# Set the language
if(!empty($_GET['lang']))
    $_SESSION['lang'] = strtoupper($_GET['lang']);

?><!DOCTYPE html>
 <html lang="<?php echo getLang() ?>">
 <head>
    <meta charset="UTF-8">
    <title> iPhone 8 </title>
    <link rel="stylesheet" href="style.css"/>
 </head>
 <body>
 <header>
    <div id="navbar">
        <div id="container">
            <a href="index.php"><img src="Logos/mylogo1.svg" alt="logo" id="logo"></a>
        </div>
        <ul class="menu">
            <li><a href="index.php?lang=<?php echo getLang() ?>"><?php echo __('Homepage') ?></a></li>
            <li class="dropdown">
            <a href="javascript:void(0)" class="dropbtn"> iPhone 8 </a>
                <div class="dropdown-content">
                    <a href="Review.php?lang=<?php echo getLang() ?>"> <?php echo __('Review') ?> </a>
                    <a href="Specifications.php?lang=<?php echo getLang() ?>"> <?php echo __('Specification') ?> </a>
                </div>
            <li><a href="News.php?lang=<?php echo getLang() ?>"> <?php echo __('News') ?> </a></li>
            <li class="dropdown">
            <a href="javascript:void(0)" class="dropbtn"> <?php echo __('Media') ?> </a>
                <div class="dropdown-content">
                    <a href="Photos.php?lang=<?php echo getLang() ?>"> <?php echo __('Photos and GIFs') ?></a>
                    <a href="Videos.php?lang=<?php echo getLang() ?>"> Audio & Video </a>
                </div>
            <li><a href="CompareiPhones.php?lang=<?php echo getLang() ?>"> <?php echo __('Compare') ?> </a></li>
            <li><a href="Contact.php?lang=<?php echo getLang() ?>"> <?php echo __('Contact') ?> </a></li>
            <li><a href="index.php?lang=EN"><img class="iconclass" src="Icons/ukicon.ico" alt="ENG"></a></li>
            <li><a class="current"href="index.php?lang=SK"><img class="iconclass" src="Icons/slovakicon.ico" alt="SK"></a></li>
        </ul>
    </div>
 </header>