I have the following code:
<html>
<head>
<title><?php echo $GLOBALS['L']['title']; ?></title>
</head>
<body>
<ul id="language-selection">
<li><a href="index.php?lang=english">English</a></li>
<li><a href="index.php?lang=french">French</a></li>
</ul>
<h1><?php echo $GLOBALS['L']['h1']; ?></h1>
<p><?php echo $GLOBALS['L']['p1']; ?></p>
<ul id="language-selection">
<li><a href="about.php">About Page</a></li>
<li><a href="contact.php">Contact Page</a></li>
</ul>
</body>
</html>
set_locale.php:
<?php
/*
* File: set_locale.php
*/
// Get the language from the query string, or set a default.
($language = @$_GET['lang']) or $language = 'english';
// Set up a list of possible values, and make sure the
// selected language is valid.
$allowed_locales = array('english', 'french');
if(!in_array($language, $allowed_locales))
$language = 'english'; // Set default if it is invalid.
// Inlclude the selected language
include "locale/$language.php";
// Make it global, so it is accessible everywhere in the code.
$GLOBALS['L'] = $locale;
?>
It works OK, but if I click the about.php and contact.php link.
The page returns to the default language: English.
What can I do so that when I click about.php or contact.php ends up like this:
about.php?lang=english
contact.php?lang=french
respectively, in other words I want the URL to remember the ?lang= ending.
What’s the best way of doing it?
You’ll have to append it to every outgoing link:
a nice way of dealing with multi-language sites in general is, if your server supports it,
mod_rewriteto rewrite “virtual” URLs likeand map them internally to
there’s a beginner’s guide on that here and official documentation here.
I’m no mod_rewrite guru but this works for me:
it maps
www.domain.com/en/about.phpto/about.php?lang=enwww.domain.com/fr/about.phpto/about.php?lang=frwww.domain.com/es/to/?lang=es= usually index.phpIt maps any occurrence of a two-letter, lowercase
www.example.com/xy, so you shouldn’t have any directories with two letters on your root level to work with this.