Saving the User's Language Preference
Many web pages are multilingual. In addition, they are often organized so that every localized section resides in its own directory, similar to this approach:
You can detect the language of the user in several different ways:
Although all of these methods work somehow, the final one (or a combination of several of them) is considered to be most user-friendly. So, you do need a home page that offers links to all three versions. That's simple HTML, as is shown here: Home Page Linking to the Various Language Versions (multilingual.php; excerpt)<a href="en/index.php">English version</a><br /> <a href="es/index.php">Versión español</a><br /> <a href="fr/index.php">Versione française</a> Now, every language directory has an index.php file in the specific language. In this file, the code is included from the listing at the beginning of this phrase. This checks whether there is already a language cookie. If not, it tries to set a cookie with the current path (retrieved from $_SERVER['PHP_SELF']). Saving the Current Path in a Cookie (saveLanguage.inc.php)<?php if (!isset($_COOKIE['lang']) || $_COOKIE['lang'] != $_SERVER['PHP_SELF']) { setcookie('lang', $_SERVER['PHP_SELF'], time() + 30*24*60*60, '/'); } ?>
Finally, you have to check on the home page to determine whether the cookie is present, and, if so, redirect the user to the appropriate page. To do so, the following code must be added at the top of the multilingual.php page. <?php if (isset($_COOKIE['lang']) && $_COOKIE['lang'] != '') { header("Location: {$_COOKIE['lang']}"); } ?> |