Home > Backend Development > PHP Tutorial > phpmaster | Localizing PHP Applications Part 1: Oct 2011 - Sitepoint

phpmaster | Localizing PHP Applications Part 1: Oct 2011 - Sitepoint

Lisa Kudrow
Release: 2025-03-02 08:37:09
Original
437 people have browsed it

Internationalize your PHP application: Easily implement multilingual support using gettext

The global Internet users continue to grow, and many users want to browse your content in their native language. You may think that only an excellent translation is needed to complete the user interface translation of a website, but the bigger challenge lies in writing background code. The traditional software localization process is cumbersome and error-prone, resulting in confusion in code. Some developers even use different versions of code for different regions, which makes codebase management nearly impossible.

gettext is an excellent open source tool that simplifies the process and allows you to focus on code writing, while the translator is responsible for handling individually translated files in the target language. Mastering PHP gettext, expanding the global influence of applications will become a breeze! This series of articles will teach you how to localize your PHP application using gettext. The first part will introduce gettext quickly. On this basis, the subsequent parts will explain how to deal with multiple translation domains, plural forms, and even how to automate part of the localization process.

Preparation environment

Before starting learning, make sure the environment is set up correctly, which helps to test the learning content step by step. Here is the installation list:

  • PHP 5.x on Apache (or any web server you like), enable gettext extension. You can get the latest version of PHP at www.php.net.
  • Poedit, a cross-platform gettext directory editor. This is a great tool that allows you to separate translation from your application code. You can get a copy at www.poedit.net.

Whatever your platform is, to use gettext from PHP, you need to install the gettext library and the PHP extension connected to the library. Ubuntu/Debian users can use apt-get, and Fedora/CentOS/Redhat users can use yum to install the gettext library. If you are using another Unix-like system, please visit www.gnu.org/s/gettext to get a copy of the gettext that is compatible with your platform. Windows users can download the latest executable file from gnuwin32.sourceforge.net/packages/gettext.htm and install it. After installing the library, you need to enable the PHP extension of gettext by editing php.ini and adding a line:

<code># Windows用户
extension=php_gettext.dll
# *nix用户
extension=gettext.so</code>
Copy after login
Copy after login

Then, install the Poedit that will be used later. You can download a copy at www.poedit.net.

"Hello World" of gettext

In your web root directory, create a sandbox directory called TestI18N for gettext exercises (I18N stands for internationalization). In the TestI18N directory, create the following hierarchy:

phpmaster | Localizing PHP Applications Part 1: Oct 2011 - Sitepoint

In future projects, you can name the parent and Locale directories as you like, but en_US and LC_MESSAGES are the standard names used by gettext. en_US represents the name of the locale and consists of two parts. The first part is the two lowercase abbreviation of the language according to the ISO 639-1 specification. The second part after the underscore is a two-digit capital country code according to the ISO 3166-1 alpha-2 specification. Therefore, the en_US means that the language is American English.

Next, make sure Poedit works properly on your platform. Start the program and select "File" > "New Directory" from the top menu bar. In the settings window, fill in the following information and temporarily skip the plural form fields:

phpmaster | Localizing PHP Applications Part 1: Oct 2011 - Sitepoint

Click "OK" and save the file as messages.po created in the LC_MESSAGES directory. Now close Poedit and open messages.po with your favorite text editor... Yes, it's a normal text file! You can edit it manually, but to save trouble, we let Poedit create the main definition for us. Leave a blank line after the existing line in the file, and add the following:

<code># Windows用户
extension=php_gettext.dll
# *nix用户
extension=gettext.so</code>
Copy after login
Copy after login

Save messages.po and close it, then reopen it in Poedit. In Poedit, select File > Save or click the "Save Directory" entry in the icon bar. The reason we save PO files in Poedit is that they need to be compiled into a special format available to gettext. After saving in Poedit, you will see that a new file with the same name was created in the same directory, but with the extension .mo. If you want to continue modifying and saving PO files using a regular text editor, you need to compile with the pocompile command. If you use Poedit, no additional compilation steps are required as it will automatically compile the file when you save it.

Go back to the TestI18N directory and create a file named test-locale.php with the following code:

<code># 测试标记1
msgid "HELLO_WORLD"
msgstr "Hello World!"

# 测试标记2
msgid "TEST_TRANSLATION"
msgstr "Testing translation..."</code>
Copy after login

Open TestI18N/test-locale.php in your browser. If everything is installed correctly and works well, you will see "Hello World" on the page.

Summary

You have many options when localizing your PHP application. We chose to use the GNU gettext library and its PHP extension, a powerful and simple way to "correctly" localize your application! In this section, you see what you need to install gettext and PHP extensions, briefly use Poedit, and spark your interest with a simple Hello World script. The second part will be based on this to learn more about gettext; I will explain each function introduced in the Hello World script and how the gettext library works.

Pictures from sgame / Shutterstock

(The following is the FAQ part, adjusted according to the original content, and pseudo-original creations were made)

Frequently Asked Questions about PHP Application Localization

What is the importance of localizing PHP applications?

Localized PHP applications are essential for businesses that want to reach users around the world. It involves translating the user interface and other visible elements of a software application into different languages. This process not only enhances the user experience, but also expands the market coverage of the application. By localizing your PHP application, you can meet the needs of different users, thereby increasing your user base and potential revenue.

How does PHP localization work?

PHP localization involves using specific functions and libraries to translate text in an application. The most common method is to use gettext, a powerful library that allows you to wrap strings in a function that will translate them according to the user's locale settings. This involves creating a .po file for each language that contains all translations.

What are the steps to implement PHP localization?

Implementing PHP localization involves several steps. First, you need to install and configure the gettext library in your PHP environment. Next, you need to wrap all the strings in the application that you want to translate using the gettext function. Then you need to create .po files for each language you want to support and translate all the strings in those files. Finally, you need to compile these .po files into .mo files, which are binary files and gettext can read them.

What are the challenges of localizing PHP applications?

Localizing PHP applications can be challenging due to several reasons. First, translating all strings in an application can be time-consuming, especially when the application is huge and complex. Second, maintaining translation can be difficult, especially when updating or adding new features to your application. Third, dealing with plural, gender, and other grammatical subtleties in different languages ​​can be tricky.

How to handle plural numbers in PHP localization?

The ngette function can be used to process plural numbers in PHP localization. This function accepts three parameters: the singular form, the plural form, and the number of the string. It then returns the correct form based on the number. For example, ngettetext ("one apple", "many apples", $number) returns "one apples" if $number is 1, otherwise "many apples".

How to test my localized PHP application?

You can test your localized PHP application by changing the locale on the server or in the PHP code and then checking if the string is translated correctly. You should also test the layout and design of applications in different languages, as some languages ​​may require more space or have different reading directions.

Can I use machine translation for PHP localization?

While machine translation can translate your strings quickly and easily, it is not recommended for PHP localization. Machine translation often lacks the accuracy and context-awareness required for software localization. It is best to use a professional translator or localized service to ensure the quality of the translation.

How to manage translation of my PHP application?

You can use a localized platform or translation management system to manage translation of PHP applications. These tools allow you to store, manage and update translations in a central location. They also provide features such as translation memory, glossary and collaboration tools to help you simplify the localization process.

What is the role of the.po and .mo files in PHP localization?

.po and .mo files play a crucial role in PHP localization. A .po file is a text file containing the original string and its translation. They are human readable and can be edited using a text editor or a .po file editor. On the other hand, the .mo file is a machine-readable binary file. They are generated by .po files and are used by gettext to translate strings in the application.

Can I localize my PHP application to any language?

Yes, you can localize your PHP application to any language that gettext supports. However, you need to have the translation of that language for all strings in the application. In addition, you need to consider the cultural and linguistic nuances of each language to ensure the quality of localization.

The above is the detailed content of phpmaster | Localizing PHP Applications Part 1: Oct 2011 - Sitepoint. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template