HTML page with two languages

Asked

Viewed 1,466 times

1

Hello, I am beginner in the world of web development and would like help to realize the design of a small project 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 this case the default page would be in English, but it would have a button or something so that the user could place the site in Portuguese. You need to use another language like PHP or Javascript, or with pure HTML and CSS you can do?

The link below leads to the code page, I’m also using bootstrap:

https://codepen.io/mrfelps/pen/mByWKG

  • Try to give more details so that we can help you, example code you have already done or will create the site from scratch.. whether to use wordpress?

  • Have you created any code? Don’t expect anyone to code for you. Be more specific. See [tour] for more details.

  • Yes, I just edited the question rsrs, I’m learning to use the tool still, little by little. In editing I included the link with the code. Abs.

  • @Felipebraz Put the code right here, imagine if the site you passed the link with the code is out of the air or is deleted? Ideally you put the code right here, so the site is more organized and easier for others who face the same problem.

  • @RORSCHACH I understand, I apologize because I couldn’t put the link here. I haven’t had time to understand all the features of Stack, but the idea is to learn and go beyond (-:

1 answer

3


Has a topical 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 to 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: Check if the user has requested a language change, using 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 so and would change based on the uploaded language file.

<?php
  echo $lang['firstname'];
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.