Skip to content
Explain to Dev
Explain to Dev

Empowering developers with the knowledge to build, create, and innovate in the software world.

  • Home
  • About
  • Java
  • Python
  • PHP
  • .NET
  • Node.js
  • SQL
  • Privacy Policy
Explain to Dev

Empowering developers with the knowledge to build, create, and innovate in the software world.

How to Build a Multi-Language Website Using PHP

etd_admin, May 13, 2025May 13, 2025

Creating a website that supports multiple languages is essential if you’re targeting a global audience. PHP, being one of the most popular server-side scripting languages, provides several ways to handle multiple languages on a website. In this article, you’ll learn how to build a multi-language website using PHP with simple steps and clear code samples.

Choose a Language Management Method

There are a few ways to manage language content:

  1. Language Files (Preferred for Simplicity)
  2. Database-Driven Content (More complex)
  3. PHP Framework Localization (e.g., Laravel, Symfony)

For this tutorial, we’ll use language files because they are easy to manage and good for most small to medium-sized websites.

Create the Folder Structure

You can organize language files like this:

/lang
   en.php
   fr.php
   es.php
index.php

Each language file will return an array of translated strings.

Example of lang/en.php:

<?php
return [
    'title' => 'Welcome to My Website',
    'greeting' => 'Hello, how are you?',
];

Example of lang/fr.php:

<?php
return [
    'title' => 'Bienvenue sur mon site Web',
    'greeting' => 'Bonjour, comment ça va?',
];

Detect or Choose the Language

You can let users pick a language using a query string like ?lang=en, or store their preference in a session or cookie.

Here’s how to handle it in index.php:

<?php
session_start();

// Set default language
$lang = 'en';

if (isset($_GET['lang'])) {
    $lang = $_GET['lang'];
    $_SESSION['lang'] = $lang;
} elseif (isset($_SESSION['lang'])) {
    $lang = $_SESSION['lang'];
}

// Load language file
$langFile = "lang/$lang.php";
if (file_exists($langFile)) {
    $translations = include($langFile);
} else {
    $translations = include("lang/en.php");
}
?>

<!DOCTYPE html>
<html>
<head>
    <title><?= $translations['title']; ?></title>
</head>
<body>
    <h1><?= $translations['title']; ?></h1>
    <p><?= $translations['greeting']; ?></p>

    <p>Select Language:</p>
    <a href="?lang=en">English</a> |
    <a href="?lang=fr">Français</a>
</body>
</html>

With this setup, when users click on a language link, the content updates accordingly.

Expand as Needed

You can continue adding more keys to each language file like this:

return [
    'title' => '...',
    'greeting' => '...',
    'contact' => 'Contact Us',
    'about' => 'About Us',
    // etc.
];

Then in your HTML/PHP, just call <?= $translations['about']; ?> wherever needed.

Here are some best practices:

  • Avoid hardcoding text — always load from your language files.
  • Use consistent keys across all languages to avoid errors.
  • Set a fallback language (usually English) in case the file or key doesn’t exist.
  • Keep language files clean and organized, especially as your site grows.

That’s it! You now know how to build a multi-language website using PHP using language files and simple logic. This method is scalable, easy to manage, and works for most websites that don’t require dynamic translations from a database or advanced frameworks.

PHP LanguagePHP

Post navigation

Previous post
Next post

Leave a Reply Cancel reply

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

©2025 Explain to Dev | WordPress Theme by SuperbThemes