HTML page with two languages

1

Hello, I am a beginner in the web development world and would like help in designing a small project that I am studying.     The project should contain only one page and I would like to know the simplest way to put two languages for the user to choose. In case the default page would be in English, but would have a button or something of the type for the user to put the site in Portuguese.     Is it necessary to use another language like PHP or JavaScript, or with pure HTML and CSS?

The link below takes you to the code page, I'm also using bootstrap:

link

    
asked by anonymous 14.09.2017 / 17:57

1 answer

1

You have a topic that was answered using PHP:

Step 1: Set up a folder tree structure like this:

Linguagens
 -en
   -lang.en.php
 -fr
   -lang.fr.php
 -de
   -lang.de.php

Continue making new folders with all the other languages you want.

Step 2: Create a folder with the language files, example: linguagens/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 any other language, for example, linguagens/fr/lang.fr.php . Note Always after the $lang the names remain 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: Verify that the user has requested a language change through 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('linguagens/'.$_SESSION['lang'].'/lang.'.$_SESSION['lang'].'.php');

?>

Step 4: You can access your language parts as such and change based on the language file you uploaded.

<?php
  echo $lang['firstname'];
?>
    
14.09.2017 / 18:15