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.

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:

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.

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.

Top courses in PHP

APIs in PHP: from Basic to Advanced
Dave Hollingworth
4.6 (802)
Laravel 10 - Build Real Estate Property Listing Project A-Z
Kazi Ariyan, easy Learning
4.8 (137)
Highest Rated
PHP for Beginners - Become a PHP Master - CMS Project
Edwin Diaz, Coding Faculty Solutions
4.3 (24,122)
Bestseller
PHP with Laravel for beginners - Become a Master in Laravel
Edwin Diaz, Coding Faculty Solutions
4.4 (12,515)
Object Oriented PHP & MVC
Brad Traversy
4.5 (4,831)
PHP for Beginners
Tim Buchalka's Learn Programming Academy, Dave Hollingworth
4.5 (3,485)
PHP Unit Testing with PHPUnit
Dave Hollingworth
4.5 (2,088)
Bestseller
Learn PHP Programming From Scratch
Stone River eLearning
4.8 (1,057)

More PHP Courses

PHP students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.

Request a demo