我可以做些什么来使用PHP制作多语言>

In HTML I have two input as :

    <input type="submit" value="FR"  <?php include "Lang/fr.php" ?>>
    <input type="submit" value="EN"  <?php include "Lang/en." ?>>

My problem :

I want when I click button with value EN , my website will change language to EN and the same with `FR'

Can you give me some solution to implement it ? Thank for your consideration.

Here is some starter code for you (not guaranteed to work)

<?php
session_start(); // ensure sessions are enabled on each page
//---set default value for language
if( !isset($_SESSION['lang']) ) $_SESSION['lang'] = 'en';
if( $_POST['submit'] ) {
   $_SESSION['lang'] = $_POST['<your input name here>'];
}
?>

then you can use something like the following in your html

<html lang='<php echo $_SESSION['lang']'?>'>

Don't use PHP to do that stuff. PHP is a server-side language. So everytime your user sends request to change the language, it involves all server side works, hence make your server bulky.

I would suggest you to use the Translator API by Google by using some little bit JavaScript code and just bind the API with the data.

The benefits will be-

  • Your Server will be less bulks

  • You don't have to use those extra files.

  • You will be less depend on your code. So you will have to give less time to find bugs.

As already written you could use sessions to display the choosen language. Everything you´ve to do using this solution is to create "locals", one for each language.

en = en.php - fr = fr.php

To save time und work you can create strings which are declared in both "local files":

 $home = "Welcome to this page"; // En
 $home = "Bienvenue sur cette page"; // Fr

Since you have a session you can use a simple if statement:

<?php
session_start(); // ensure sessions are enabled on each page
//---set default value for language
if( !isset($_SESSION['lang']) ) $_SESSION['lang'] = 'en';
if( $_POST['submit'] ) {
$_SESSION['lang'] = $_POST['<your input name here>'];
}
if ($_SESSION['lang'] == "") { include("en.php");}
elseif ($_SESSION['lang'] == "en") { include("en.php");}
elseif ($_SESSION['lang'] == "fr") { include("fr.php");}
else {echo "Error";}
?>

This code is not tested only an addition to jeff´s post.

With this option you can give out different languages in one file:

echo "$home";

But! There is no need to use javascript if you want to use googles api.

There is a quick and simple solution:

    <?php
$canonical = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
echo '
<form method="post" action="/language.php">
<select name="language">
<option value="en" selected="selected">English</option>
<option value="fr">French</option>
<input type="hidden" name="page" value="'.$canonical.'" />
<input type="submit" value="Translate" />
</form>
';
?>

This will detect the url of a site and submit it to the "language.php":

    <?php
$lang = $_POST["language"];
$link = $_POST["page"];
header ("Location: https://translate.google.com/translate?hl=en&sl=en&tl=$lang&u=$link");
exit;
?>

This will put every information (url + language) together an redirect to the translated version.