3 Steps Very Simple Multi-Language Website With PHP

Welcome to a quick tutorial on how to create a multi-language website in PHP. I know, there are already a ton of these “multi-language” tutorials all over the Internet. So this one is going to be a little different from the rest, I am going to keep it as simple as possible – Read on for the example!

 

 

TLDR – QUICK SLIDES

Fullscreen Mode – Click Here

 

TABLE OF CONTENTS

 

 

PHP MULTI-LANGUAGE WEBSITE

All right, let us now get into the example of creating a simple multi-language website with PHP.

 

STEP 1) HTML SITE LAYOUT

lang-demo.php
<!DOCTYPE html>
<html>
  <head>
    <title><?=$_TXT[0]?></title>
    <meta charset="utf-8">
  </head>
  <body lang="<?=$_SESSION["lang"]?>">
    <!-- (A) SET LANGUAGE -->
    <form method="post">
      <input type="submit" name="lang" value="en">
      <input type="submit" name="lang" value="zh">
    </form>
 
    <!-- (B) SITE CONTENTS -->
    <header><?=$_TXT[1]?></header>
    <main><?=$_TXT[2]?></main>
    <footer><?=$_TXT[3]?></footer>
  </body>
</html>

That’s right, this is just your “regular HTML layout”.

  1. A language switcher form. I went “full lazy and full simple” with this one, you will want to create your own “nice-looking” form here.
  2. Take note that all the text and contents are replaced with PHP variables instead – <?=$_TXT[N]?>.

 

STEP 2) PHP LANGUAGE MANAGER

lang-demo.php
<?php
// (C) SET LANGUAGE INTO SESSION
session_start();
if (!isset($_SESSION["lang"])) { $_SESSION["lang"] = "en"; }
if (isset($_POST["lang"])) { $_SESSION["lang"] = $_POST["lang"]; }
 
// (D) LOAD LANGUAGE FILE
require "lang-" . $_SESSION["lang"] . ".php"; ?>
<!DOCTYPE html>
...
  • To register the chosen language, we have to start the session first – session_start().
  • If the language is not chosen, we will set it to English by default – if (!isset($_SESSION["lang"])) { $_SESSION["lang"] = "en"; }
  • When the “switch language” form is submitted, we change to the selected language – if (isset($_POST["lang"])) { $_SESSION["lang"] = $_POST["lang"]; }
  • Lastly, load the language file – require "lang-" . $_SESSION["lang"] . ".php";

 

 

STEP 3) LANGUAGE PACKS

3A) ENGLISH LANGUAGE PACK

lang-en.php
<?php
$_TXT = [
  "Test Website",
  "My Website",
  "Welcome To My Site",
  "Copyright My Site"
];

Remember that all the page content is a “placeholder variable” <?=$_TXT[N]?>? All we need to do in the language pack, is to create an array and “map” the text accordingly.

 

3B) CHINESE LANGUAGE PACK

lang-zh.php
<?php
$_TXT = [
  "测试网站",
  "我的网站",
  "欢迎来到我的网站",
  "版权所有: 我的网站"
];

This is the Chinese translation. Create as many of such “language packs” as required in your own project.

 

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

SUPPORT

600+ free tutorials & projects on Code Boxx and still growing. I insist on not turning Code Boxx into a "paid scripts and courses" business, so every little bit of support helps.

Buy Me A Meal Code Boxx eBooks

 

EXAMPLE CODE DOWNLOAD

Click here for the source code on GitHub gist, just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

EXTRA BITS & LINKS

That’s all for this project, and here is a small section on some extras that may be useful to you.

 

ALTERNATIVE METHODS

Of course, the above example is not the only way to create a multi-language website. The other common methods are:

  • Create separate websites for each language. For example, http://site.com for English, http://fr.site.com for French, http://jp.site.com for Japanese, etc…
  • Use a translation API – Check out Google Translate.
  • Keep the language packs in the database instead of an array.

 

REFERENCES & LINKS

 

 

INFOGRAPHIC CHEAT SHEET

Multi-Language Websites With PHP (Click To Enlarge)

 

THE END

Thank you for reading, and we have come to the end of this guide. I hope that it has helped you with your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

3 thoughts on “3 Steps Very Simple Multi-Language Website With PHP”

Leave a Comment

Your email address will not be published. Required fields are marked *