How to make something to let the user change the language in a multilingual website that uses php arrays?
I followed the following tutorial to build a multilingual webpage using php arrays: http://bytes.com/topic/php/answers/880915-how-develop-multi-lingual-websites-php
the code:
files tree:
locale/
english.php
french.php
set_locale.php
index.php
locale/english.php:
<?php
$locale = array(
'title' => 'Title in English',
'h1' => 'The following in in English:',
'p1' => 'This is a sample text, in English'
);
?>
locale/french.php:
<?php
$locale = array(
'title' => 'Titre en français',
'h1' => 'Le texte suivant en en français:',
'p1' => 'Il s\'agit d\'un échantillon de texte, en français.'
);
?>
set_locale.php:
<?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 = 'eng开发者_运维问答lish'; // 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;
?>
index.php:
<?php
// Include the "set_locale" script to fetch the locale
// that should be used.
include_once "set_locale.php"
// Print the HTML, using the selected locale.
?>
<html>
<head>
<title><?php echo $GLOBALS['L']['title']; ?></title>
</head>
<body>
<h1><?php echo $GLOBALS['L']['h1']; ?></h1>
<p><?php echo $GLOBALS['L']['p1']; ?></p>
</body>
</html>
Now, I want to make something to let the user click on a link and change the language
Something like this:
<ul id="language-selection">
<li><a href="something should go here but I'm not sure what">English</a></li>
<li><a href="ssomething should go here but I'm not sure what">English</a></li>
</ul>
What's the best way of doing it?
Pass language as part of the query string:
<ul id="language-selection">
<li><a href="index.php?lang=english">English</a></li>
<li><a href="index.php?lang=french">English</a></li>
</ul>
In your code, you are using $_GET['lang']
, so this should catch it. You may want to mofify the code if the need be.
index.php?lang=english
OR
index.php?lang=french
精彩评论