PHP Gettext: Quick Start Guide to Creating Translations Using gettext in PHP

php gettextWouldn’t it be nice if you could create local language versions of your web app for all your international users? gettext, an open source internationalization and localization system makes it possible. It works with multiple languages, including PHP, and makes the entire translation process easy to manage and maintain.

In this tutorial, we will learn how to install, set up and create simple translations using PHP gettext. Check out this course for a more in-depth tutorial on internationalization (i18n) with PHP and gettext.

What is gettext?

In the early 1990s, as computing became truly global, programmers realized the importance of creating multi-lingual versions of their software. This was more than a translation problem; it was a coding nightmare. Programmers often had to create multiple copies of the same code to serve up different versions of the website in local languages.

To remedy this problem, Sun Microsystems developed a system called gettext in the 1990s. A few years later, the GNU Project released GNU gettext, which was an open source implementation of Sun’s gettext system.

Using gettextis relatively simple in principle. It involves modifying the original English source code to include the gettext(string) function. gettext then returns a local language of the specified string stored in an external file.

Over the years, gettext has been implemented in several languages, including C++, C#, ASP.net, Perl, Ruby, and of course, PHP. It is now the standard internationalization (shortened to i18n – i-18 letters-n) system across languages.

Using gettext in PHP

Installation

Note: This tutorial assumes you already have PHP and Apache web server installed on your local machine. If you have not, grab a copy of XAMPP. Learn how to install and enable XAMPP in this tutorial.

To use gettext in PHP, you first need to download and install a copy of GNU gettext. Windows users can download a copy from the official GNU Windows website. Mac and Linux users can get a copy here.

Once downloaded and install, you need to enable gettext in PHP. To do this, open your PHP.ini file (located in the root PHP folder) and search for “gettext”.

You should find a line that looks like this:

;extension=php_gettext.dll

Remove the semicolon (;) from the line. This is what your PHP.ini file should look like now:

a

To test whether gettext is installed properly or not, create a new file named test.php and add the following code to it:

php gettext

Save this file in the htdocsfolder in your main xamppdirectory (usually c:\xampp\htdocs). Now make sure Apache web server is turned on, and then navigate to localhost\test.php in your browser.

If gettext is properly installed, you should see this message:

 

php

Completely new to PHP? Learn the basics of the language in this PHP 101 course.

Setting Up gettext Environment

The gettext function requires a specific file/folder structure to work.

In your root directory (htdocs\) create a new folder named Test_Project. Create another folder named Locale inside it. In the Locale folder, create a sub-directory named en_US. This sub-directory should hold another folder named LC_MESSAGES, as shown below:

d

In the above structure:

  • Test_Project is just the name of our example project. You can name this anything.
  • The Locale folder holds all translation files for different languages. You can name this anything as well, though Locale is the standard name.
  • The en_US holds language files for the English (United States) language. en_US is a standard two-part abbreviation for the English (US) language. The same for English (UK) is en_GB. You must name this folder exactly as the language you wish to translate the code in.
    For example, if you wanted to hold translation for Colombian Spanish, you would name the folder es_CO. You can see a complete list of language codes here.
  • The LC_MESSAGES folder holds the actual translated messages. You must keep this folder name exactly the same for gettext to work.

Want to create your own translations for Spanish language audiences? Learn how with this course on Spanish language for beginners, intermediate and advanced users.

Creating the Translation Files

All translations in gettext are stored in a PO (Portable Object) file. You would have different *.po files for different language translations.

Before you can create the PHP code for the translation, you have to create the appropriate .po file. You can use a program like POEdit to create .po files. You can download a copy of POEdit here.

In POEdit, create a new translation file by hitting CTRL + N or selecting File -> New.

php

You’ll be asked to choose a language in the next window. Pick “English (United States)” for now.

php

Click on the ‘Save’ icon to save the file. Name it ”messages”. Make sure to save it in the LC_MESSAGES folder.

php

Now open the messages.po file in the en_US folder. It should look something like this:

php

Add the following code on a separate line below the above code:

php

This is our actual translation.

  • The first line, beginning with (#) is just a comment.
  • msgid identifies the text string that needs to be translated.
  • msgstr is the translated text that will replace the msgid.

Now save the .po file and re-open it in POEdit. Hit ‘Save’. This will re-compile the translation and make it ready for delivery.

Creating the PHP File

Create a new PHP file and name it “test_project.php”. Add the following code to it:

php

Save the file and open it in your web browser by going to localhost/Test_Project/test_project.php.

You should see the translated string (“This is a translated string”), like this:

php

Congratulations, you’ve just used PHP gettext to create a web page translation!

PHP is a powerful, yet easy to learn language. Learn how to create robust web apps using PHP and MySQL in this course.