Switching language while staying on the same page in PHP

Solution:
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>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to a member function format() on string
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.