How to create multi language main menu in html/php script now i have
{t t=\"Let
Multi-language management and support for the PHP.
Sample :
<?php
include "LMPHP.php";
/////////////////////////////
$langs = new LMPHP();
$langs -> language_add("en","English");
$langs -> language_add("fr","Germany");
$langs -> language_active("en");
$langs -> word_add("Bye","Good Bye!");
$langs -> word_add("HowAre");
$langs -> language_active("fr");
$langs -> word_add("Bye","Au revoir!");
print_r( $langs -> words );
echo $langs -> word_get("Bye");
Code Repository : https://github.com/BaseMax/LMPHP
Full Sample Code : https://github.com/BaseMax/LMPHP/blob/master/Sample.php
Used the above code, but session is overwritten every load to EN, so changed it to
<?php
// Start a Session, You might start this somewhere else already.
session_start();
// What languages do we support
$available_langs = array('en','zh-cn','es');
if(isset($_GET['lang']) && $_GET['lang'] != ''){
// check if the language is one we support
if(in_array($_GET['lang'], $available_langs))
{
$_SESSION['lang'] = $_GET['lang']; // Set session
}
}
// Set our default language session ONLY if we've got nothing
if ($_SESSION['lang']=='') {
$_SESSION['lang'] = 'en';
}
// Include active language
include('languages/'.$_SESSION['lang'].'/lang.'.$_SESSION['lang'].'.php');
?>
My Approach would be to do the following:
Step 1: Setup a folder tree structure like this:
Languages
-en
-lang.en.php
-fr
-lang.fr.php
-de
-lang.de.php
keep making new folders with all the other languages you want to support
Step 2: Create our language files, i will start with languages/en/lang.en.php
<?php
$lang['label'] = 'Value for this label';
$lang['firstname'] = 'First Name';
$lang['lastname'] = 'Last Name';
$lang['phone'] = 'Phone';
// ETC
?>
you would repeat this for every other language, ill do fr for example languages/fr/lang.fr.php
. NOTE how the labels stay the same in english
<?php
$lang['label'] = 'Valeur pour ce label';
$lang['firstname'] = 'Prénom';
$lang['lastname'] = 'Nom de famille';
$lang['phone'] = 'Téléphone';
// ETC
?>
Step 3: Check if the user has requested a language change, via a url variable
<?php
// Start a Session, You might start this somewhere else already.
session_start();
// What languages do we support
$available_langs = array('en','fr','de');
// Set our default language session
$_SESSION['lang'] = 'en';
if(isset($_GET['lang']) && $_GET['lang'] != ''){
// check if the language is one we support
if(in_array($_GET['lang'], $available_langs))
{
$_SESSION['lang'] = $_GET['lang']; // Set session
}
}
// Include active language
include('languages/'.$_SESSION['lang'].'/lang.'.$_SESSION['lang'].'.php');
?>
Step 4: you can access your language parts like so and it would change based on what language file is loaded.
<?php
echo $lang['firstname'];
?>
hope this helps get you started as an idea
If your language file is really long, you could break it up by page by adding this above the language include:
// for login page
$textpart = 'login';
and have the language page split up the array thusly
<?php
switch ($textpart) {
//login page
case 'login':
$lang['label'] = 'Value for this label';
$lang['firstname'] = 'First Name';
$lang['lastname'] = 'Last Name';
$lang['phone'] = 'Phone';
break;
//home page
case 'home':
// ETC
}
// All pages
$lang['title'] = 'Title';
// ETC
?>