Einfacher Sprachumschalter für kleine indivduelle Websites.

Ein bischen 'include' gepaart mit einem Session-basiertem Switcher.
Es werden hierbei keine URL-Anpassungen ( wie: ?lang=en ) vorgenommen.

Beispiel:

Ordner-/Dateistruktur:

  • ./html
    • /content
      • /de.php
      • /en.php
      • /zh.php
    • /include
      • /config.php
    • /index.php

./include/config.php

<?php
session_start();
header('Cache-control: private');

if($_GET['lang']){
$_SESSION['lang'] = $_GET['lang'];
header('Location:'.$_SERVER['PHP_SELF']);
exit();
}

switch($_SESSION['lang']){

case "de":
require('content/de.php');
break;
case "en":
require('content/en.php');
break;
case "zh":
require('content/zh.php');
break;
...
...
...
default:
require('content/de.php');

}

index.php

<?php
require_once 'include/config.php';
?>
<!DOCTYPE html>
<html lang="<?php echo $lang['html_lang']; ?>">

<?php include_once('include/head.php'); ?>

</head>

<body>
<header>
<h1>... CompanyName ...</div>
</header>

<?php include_once('include/nav.php'); ?>

<main>

<article>
<header>
<h2<?= $char; ?><?php echo $lang['headline']; ?></h2>
</header>
<p<?= $char; ?><?php echo $char; ?>><?= $lang['post']; ?> ...</p>
</article>

</main>

<footer>
<p>... FooterInfo ...</p>
</footer

</body>
</html>

./include/nav.php

<nav>
<ul>
<li><span>Languages</span>
<ul>
<li><a title="<?= $lang['lang-de']; ?>" href="/?lang=de">Deutsch</a></li>
<li><a title="<?= $lang['lang-en']; ?>" href="/?lang=en">English</a></li>
<li><a title="<?= $lang['lang-zh']; ?>" href="/?lang=zh">中国</a></li>
</ul>
</li>
</ul>
</nav>

./content/de.php

<?php
/**
* page lang GERMAN
*/
$lang['html_lang'] = "de";
$lang['headline'] = "Deutsch";
$lang['post'] = "Deutsch";
...
...
...

./content/en.php

<?php
/**
* page lang ENGLISH
*/
$lang['html_lang'] = "en";
$lang['headline'] = "English";
$lang['post'] = "English";
...
...
...

./content/zh.php

<?php
/**
* page lang CHINESE
*/
$lang['html_lang'] = "zh";
$lang['headline'] = "中文";
$lang['post'] = "中文";
...
...
...