Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi language php script

How to create multi language main menu in html/php script now i have

<li>
<a href="{url p='poceni-letalske-karte.html'}">
<span>{t t="Letalske Karte"}</span>
</a>
</li>

and

<option value='EN'>English</option> which it go to mysite.com/EN i want when user select English language EN code it also change main menu text how to do that ? This is website Letalske karte

I found this script http://www.bitrepository.com/php-how-to-add-multi-language-support-to-a-website.html But i don't know how to set to /EN/ as now in this script is set to index.php?lang=en

like image 246
Luka Boltes Avatar asked Mar 21 '26 18:03

Luka Boltes


1 Answers

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

like image 147
Dave Avatar answered Mar 24 '26 09:03

Dave