Wouldn’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. Show 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. InstallationNote: 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: To test whether gettext is installed properly or not, create a new file named test.php and add the following code to it: 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: Completely new to PHP? Learn the basics of the language in this PHP 101 course. Setting Up gettext EnvironmentThe 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: 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 FilesAll 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. You’ll be asked to choose a language in the next window. Pick “English (United States)” for now. Click on the ‘Save’ icon to save the file. Name it ”messages”. Make sure to save it in the LC_MESSAGES folder. Now open the messages.po file in the en_US folder. It should look something like this: Add the following code on a separate line below the above code: 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 FileCreate a new PHP file and name it “test_project.php”. Add the following code to it: 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: 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. |