Reputation: 75
I'm new to PHP and I'm struggling to find solution to my problem. Basically, as the title says what I want to do is change language, but it redirects me to index.php as it is in the code below. The thing that there's on more if () {echo ...} statement complicates it even more. I've read some topics but i can't find the right answer. So here's my 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>';
}
?>
Upvotes: 0
Views: 750
Reputation: 12505
What you want to maybe do is:
1) Have a function that will translate words based on current language (there may also be a library/server-implementation to do this for you, but for limited words this method may work fine) 2) Store the language in a session 3) As others have suggested, don't duplicate whole blocks of code
/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'
];
header:
<?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>
Upvotes: 1