gettext is used in PHP to support multilingual methods

  • 2020-05-05 11:03:24
  • OfStack

Today we'll use a simple example of getText in PHP (getText is a series of tools and library functions that help programmers and translators develop multilingual software) to implement i18n.
Now, let's say we want to display an link:
that returns to the home page
 
//home.php: 
$str = 'home'; 
print <<<HTML 
<a href="#">{$str}</a> 
HTML; 

Here's our multilingual development journey:
Create the pot file, pot is the acronym for Portable Object Template, mo for po, mo for Machine Object. The former refers to the original string file, which is usually used for a translator to modify, while the latter is machine-related and is usually read by a program. You can create the pot file by hand, or you can generate it by xgettext extracting strings from the code. This is produced using xgettext:
xgettext -a home.php -o home.pot
After running this command, we found that in the current directory, a file with the name home.pot was generated
 
# SOME DESCRIPTIVE TITLE. 
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER 
# This file is distributed under the same license as the PACKAGE package. 
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR. 
# 
#, fuzzy 
msgid "" 
msgstr "" 
"Project-Id-Version: PACKAGE VERSION\n" 
"Report-Msgid-Bugs-To: \n" 
"POT-Creation-Date: 2009-07-23 20:56+0800\n" 
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" 
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" 
"Language-Team: LANGUAGE <LL@li.org>\n" 
"MIME-Version: 1.0\n" 
"Content-Type: text/plain; charset=CHARSET\n" 
"Content-Transfer-Encoding: 8bit\n" 
#: home.php:2 
msgid "home" 
msgstr " 

To generate po files in different languages based on pot, here we first produce a simplified Chinese po file:
export LANG=zh_CN.gb2312
msginit -l zh_CN.gb2312 -i home.pot
After running this command, we found that in the current directory, a file with the name zh_CN.po was generated
 
# Chinese translations for PACKAGE package 
# PACKAGE  Simplified Chinese translation of software package . 
# Copyright (C) 2009 THE PACKAGE'S COPYRIGHT HOLDER 
# This file is distributed under the same license as the PACKAGE package. 
# <huixinchen@localhost.localdomain>, 2009. 
# 
msgid "" 
msgstr "" 
"Project-Id-Version: PACKAGE VERSION\n" 
"Report-Msgid-Bugs-To: \n" 
"POT-Creation-Date: 2009-07-23 20:56+0800\n" 
"PO-Revision-Date: 2009-07-23 21:00+0800\n" 
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" 
"Language-Team: Chinese\n" 
"MIME-Version: 1.0\n" 
"Content-Type: text/plain; charset=GB2312\n" 
"Content-Transfer-Encoding: 8bit\n" 
#: test.php:2 
msgid "home" 
msgstr " 

The corresponding string in zh_CN.po is
 
# Chinese translations for PACKAGE package 
# PACKAGE  Simplified Chinese translation of software package . 
# Copyright (C) 2009 THE PACKAGE'S COPYRIGHT HOLDER 
# This file is distributed under the same license as the PACKAGE package. 
# <huixinchen@localhost.localdomain>, 2009. 
# 
msgid "" 
msgstr "" 
"Project-Id-Version: PACKAGE VERSION\n" 
"Report-Msgid-Bugs-To: \n" 
"POT-Creation-Date: 2009-07-23 20:56+0800\n" 
"PO-Revision-Date: 2009-07-23 21:00+0800\n" 
"Last-Translator: <huixinchen@localhost.localdomain>\n" 
"Language-Team: Chinese\n" 
"MIME-Version: 1.0\n" 
"Content-Type: text/plain; charset=GB2312\n" 
"Content-Transfer-Encoding: 8bit\n" 
#: test.php:2 
msgid "home" 
msgstr " The home page  

Generate the mo file from the po file.
msgfmt zh_CN.po -o zh_CN.mo
After running this command, we found that a file with the name zh_CN.mo was generated in the current directory. It is binary and cannot be opened with a text editor.
Install mo files into a specific directory:
cp -f zh_CN.mo .local/LC_MESSAGES/home.mo
Modify the program.
 
setlocale(LC_ALL, 'zh_CN'); 
// Specify location of translation tables 
bindtextdomain("home", "."); 
// Choose domain 
textdomain("home"); 
// Translation is looking for in ./locale/zh_CN/LC_MESSAGES/home.mo now 
$str = gettext('home'); // Can also be used _('home') 
print <<<HTML 
<a href="#">{$str}</a> 
HTML; 

Run the script and see if it prints the correct Chinese.
It's also easy to add other languages. You don't need to modify the program, you just need to generate an mo file and install it in the corresponding directory on the system, just like you do in Chinese. Switching languages is simply a matter of modifying the current locale.

Related articles: