Creating a simple multi-lingual website

Daniel Pataki
Mar 19, 2009
Updated • May 29, 2017
Development
|
2

If you want to create a simple webpage for yourself, listing who you are, how you can be contected and what you do, you might want to add a few languages in there. You could use Google Translate, but that does not yield the best (and professional) results, so it would be best to translate the page yourself, or have someone do it for you.

Now, you could have different pages like "about_english.php" and "about_spanish.php", but this would make updating a pain, and very inflexible, plus if you have 20 languages, it means 20 files per page. So how do we get around this? Let me introduce you guys, to PHP constants.

A constant is defined just like a variable, but as its name suggests, it is in fact constant. It can be echoed just like a variable, and is great for defining set pieces of text. Let me show you my method of using constants to easily keep multiple language versions of a webpage, without having to have multiple files for each page.

Constants in PHP

php define constant

First of all, let's take a look at how to define constants. Constant names are uppercase strings, but must not begin with a number and should not start with a special character. For example, let's define a constant:

define("NAME", "Daniel Pataki");

In this example we have defined a constant called "NAME", and gave it a value of "Daniel Pataki". I gave the constant a describing name, so I know that this constant holds my name.

The way I create multiple languages is that I create 1 language file for each language. I name them according to the standard 2 letter convention. The English file is named "en.php", the French would be "fr.php" and so on. I include the language file the user needs at the beginning of all my other files containing text, so they will automatically be defined. There are many way to approach this, but for now, let's say that the page is always in English, unless a user clicks one of the language links. So at the beginning of the code I would write:

<?php
if (isset($_GET['lang']))
include($_GET['lang'].".php");
else
include("en.php")
?>

This way the relevant file will be included only. Each file contains the list of definitions I need in the same structure. My name in the English version is "Daniel Pataki", but in Hungarian we put our family names in the front, so in "hu.php" I would define it as:

define("NAME", "Pataki Dániel");

In the page's code, I can then simply type the following to display my name:

<h2><?php echo NAME ?></h2>

This would show my name as a level 2 heading. If the user is viewing in English, the output would be "Daniel Pataki", if the user is viewing in Hungarian it would display "Pataki Dániel", since this time "hu.php" is included, and not "en.php". Notice that when displaying constants you do no need to put any special characters before or after the constant name, just type the constant itself as you defined it.

You can use this method to create languages for larger sites too. The reason I especially like this is that it is quite easy to translate the site, you just need to send the file over to someone and he will be able to do it easily, without any training. If you have a larger site it might be a good idea to indicate where the constant will be used. You can do this by defining a constant name like "SIDEBAR_COMMENTS', or "CONTACT_NAME". This way you and your translators will have an easier time, especially if you also use PHP comments in the language file for further pointers.

Summary
Creating a simple multi-lingual website
Article Name
Creating a simple multi-lingual website
Description
Daniel walks you through the steps of creating a simple but effective multi-lingual website using the PHP language and nothing more.
Author
Publisher
Ghacks Technology News
Logo
Advertisement

Previous Post: «
Next Post: «

Comments

  1. eRIZ said on March 20, 2009 at 7:00 pm
    Reply

    How about Gettext? It’s more convenient than using constants.

  2. Tobey said on March 19, 2009 at 10:55 pm
    Reply

    Appreciated, thanks.

Leave a Reply

Check the box to consent to your data being stored in line with the guidelines set out in our privacy policy

We love comments and welcome thoughtful and civilized discussion. Rudeness and personal attacks will not be tolerated. Please stay on-topic.
Please note that your comment may not appear immediately after you post it.