Home  >  Article  >  Backend Development  >  How to use gettext in PHP to support multi-language methods_PHP tutorial

How to use gettext in PHP to support multi-language methods_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:30:32930browse

Today we use a simple example to illustrate the usage of getText in PHP (getText is a series of tools and library functions to help programmers and translators develop multi-language software), thereby realizing PHP's i18n.
Now , we assume that we want to display a link back to the homepage:

Copy code The code is as follows:

//home.php:
$str = 'home';
print <<{$str}
HTML;

Let’s start our multi-language development journey:
Create a pot file. Pot is the abbreviation of Portable Object Template. The corresponding po is mo, which is the abbreviation of Machine Object. The former refers to the original string file, which is generally used for translators to modify, while the latter is machine-related and is generally read by programs. The pot file can be created manually, or it can be generated by extracting strings from the code through xgettext. This is generated using xgettext:
xgettext -a home.php -o home.pot
After running this command, we found that a file named home.pot was generated in the current directory. Open the file file, you can see:
Copy code The code is as follows:

# 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 , YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSIONn"
"Report-Msgid-Bugs-To: n"
"POT-Creation-Date: 2009-07 -23 20:56+0800n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONEn"
"Last-Translator: FULL NAME n"
"Language-Team: LANGUAGE n"
"MIME-Version: 1.0n"
"Content-Type: text/plain; charset=CHARSETn"
"Content-Transfer -Encoding: 8bitn"
#: home.php:2
msgid "home"
msgstr "

Generate po files in different languages ​​according to pot. Here we first generate one Simplified Chinese po file:
export LANG=zh_CN.gb2312
msginit -l zh_CN.gb2312 -i home.pot
After running this command, we found that a file named zh_CN was generated in the current directory .po file, open the file, you can see:
Copy code The code is as follows:

# Chinese translations for PACKAGE package
# Simplified Chinese translation of PACKAGE software package.
# Copyright (C) 2009 THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# , 2009.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSIONn"
"Report-Msgid-Bugs-To: n "
"POT-Creation-Date: 2009-07-23 20:56+0800n"
"PO-Revision-Date: 2009-07-23 21:00+0800n"
"Last-Translator : FULL NAME n"
"Language-Team: Chinese"
"MIME-Version: 1.0n"
"Content-Type: text/plain; charset=GB2312n"
"Content-Transfer-Encoding: 8bitn"
#: test.php:2
msgid "home"
msgstr "

Translate the corresponding string in zh_CN.po For Chinese:
Copy code The code is as follows:

# Chinese translations for PACKAGE package
# Simplified version of PACKAGE software package Chinese translation.
# Copyright (C) 2009 THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# , 2009.
# >#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSIONn"
"Report-Msgid-Bugs-To: n"
"POT-Creation-Date : 2009-07-23 20:56+0800n"
"PO-Revision-Date: 2009-07-23 21:00+0800n"
"Last-Translator: n "
"Language-Team: Chinese"
"MIME-Version: 1.0n"
"Content-Type: text/plain; charset=GB2312n"
"Content-Transfer-Encoding: 8bitn"
#: test.php:2
msgid "home"
msgstr "Homepage

Generate mo file based on po file.
msgfmt zh_CN.po -o zh_CN.mo
After running this command, we found that a file named zh_CN.mo was generated in the current directory. It is binary and cannot be opened with a text editor.
Install the mo file into a specific directory:
cp -f zh_CN.mo .local/LC_MESSAGES/home.mo
Modify the program.
Copy code The code is as follows:

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'); //You can also use_('home ')
print <<{$str}
HTML;

Run Let’s see if this script outputs correct Chinese?
It’s also easy to add other languages. You don’t need to modify the program. You just need to generate a mo file like Chinese and install it in the corresponding directory in the system. That’s it. Switching between different languages ​​only requires modifying the current locale.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323229.htmlTechArticleToday we use a simple example to illustrate the usage of getText in PHP (getText is a series of tools and Library functions to help programmers and translators develop multi-language software), thus...
Statement:
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