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!

ⓘ I have included a zip file with all the example source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Multi-Language Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

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>
  </head>
  <body>
    <!-- (A) 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”. But take note that all the text and contents are replaced with PHP variables instead – <?=$_TXT[N]?>.

 

STEP 2) LANGUAGE SWITCHER

HTML LANGUAGE FORM

lang-demo.php
<body lang="<?=$_SESSION["lang"]?>">
  <!-- (B) SET LANGUAGE -->
  <form method="post">
    <input type="submit" name="lang" value="en"/>
    <input type="submit" name="lang" value="zh"/>
  </form>
</body>

Next, we put a “switch language” HTML form into the page. I went “full lazy and full simple” with this one, you will want to create your own “nice-looking” form here.

 

 

PHP REGISTER & LOAD LANGUAGE PACK

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";
  • 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

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.

 

 

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.

 

USEFUL 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!

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

Leave a Comment

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