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.
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:
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.Advertisement
Ghacks is a technology news blog that was founded in 2005 by Martin Brinkmann. It has since then become one of the most popular tech news sites on the Internet with five authors and regular contributions from freelance writers.