Using GetText to implement the internationalization programming of PHP

Source: Internet
Author: User
Tags gettext i18n locale win32
Usually when people write programs, they write words that die in the program, such as: echo "Hello world!"; , if you want to change to the language of the country, write the internationalization of the program, it is necessary to open one by one to modify, the procedure is relatively short, if the program has tens of thousands or more, it is not so easy to change. Recently, with the gradual standardization of i18n, I also talk about how to achieve internationalization support in PHP. As with other programming languages, you can also use the GetText suite to write i18n programs in PHP to implement the NLS (Native Language Support) internationalization support, please refer to the official documentation (Http://www.gnu.org/manual/get text/index.html).

Implementation process: The Program designer writes the information to be displayed in the code, does not directly display the information that the programmer writes when running the program, but first goes to find a set of information file of the language family. If it is not found, the information in the code is displayed.

Installation Set GetText Kit:
1) *nix System:
1, from the http://www.gnu.org/software/gettext/gettext.html download GetText package, to install
2, compile PHP when adding "--with-gettext[=dir]", where DIR for GetText installed
Directory, defaults to:/usr/local
3, save, and then restart server.

2) WIN32 System:
1, need to copy the Gnu_gettext.dll file to the system directory (Ex:c:/winnt/system32 or c:/windows/system32), PHP 4.2.3 file name after Libintl-1.dll, can be in the php4/ DLLs under the access.
2, open php.ini file, find Extension=php_gettext.dll, remove the front of the ";"
3, save, and then restart server.

If all goes well, you can see the word gettext in Phpinfo (), which is now set.

Second, the Php_gettext.dll kit has several functions, specifically see the relevant manual. Here we'll just memorize 3 functions, as follows: String Bindtextdomain (string domain, string directory) string Textdomain (string text_domain) Stri Ng GetText (String message)



Third, the writing i18n procedure: <?php//General procedure echo "Hello world!"); ?>

Here is the i18n program:hello.php <?php//i18n Program example start define (' PACKAGE ', ' hello '); Define the name of the mo file you want to use, as a general rule, we all have the same name definition and program name for package. Putenv (' lang=zh_tw '); SetLocale (Lc_all, ' zh_tw '); Specify the language families to use, such as: en_US, ZH_CN, Zh_tw bindtextdomain (PACKAGE, '/apache/htdocs/locale '); Textdomain (PACKAGE); the. mo file searched is:///apache/htdocs/locale/en/lc_messages/hello.mo echo gettext ("Hello world!"); ?>


In IE input: http://localhost/hello.php, the output result is: "Hello world!"
   Note:According to the custom in the GNU package, you can use _ (...) instead of gettext (...) so that you can play a lot less gettext.

Iv. Next set up gettext PO file:
1, create the directory structure, as follows:
Bindtextdomain ' s dir
/language
/lc_messages
Domain.po
Domain.mo
Where Bindtextdomain ' s dir is the directory used by Bindtextdomain (), Language for
Language, domain for PACKAGE name.

The above surface for example:
/locale
/zh_tw
/lc_messages
Hello.po
Hello.mo

2. Create PO File
xgettext-d [Package name you define] [program file name]
The Xgettext, msgfmt program files below WIN32 can be downloaded from (http://switch.dl.sourceforge.net/sourceforge/gnuwin32/gettext-0.10.40-bin.zip) , the need for Libiconv.dll, Libintl.dll support.

Take the above hello.php file for example,
$ xgettext-d Hello hello.php
After running, it will produce a HELLO.PO file, which reads as follows:
# SOME Descriptive TITLE.
# copyright©year Free Software Foundation, Inc.
# AUTHOR <email@address&gt, year.
#
#, Fuzzy
MsgId ""
Msgstr ""
"Project-id-version:package version/n"
"Pot-creation-date:2003-04-21 22:31+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"

#: Hello.php:14
MsgId "Hello world!"
Msgstr ""

It lists all the strings in the hello.php file that call the GetText function, and the translation takes only the
MsgId value translation can be filled in msgstr, such as translated into Chinese.
# SOME Descriptive TITLE.
# copyright©2003 Ptker All right Reserved.
# AUTHOR <ptker@hotmail.com&gt, 2003.
#
#, Fuzzy
MsgId ""
Msgstr ""
"Project-id-version:0.1/n"
"Pot-creation-date:2003-04-21 22:31+0800/n"
"Po-revision-date:year-mo-da ho:mi+zone/n"
"Last-translator:ptker <ptker@hotmail.com>/n"
"Language-team:zh_tw <ptker@hotmail.com>/n"
"Mime-version:1.0/n"
"Content-type:text/plain; charset=big5/n "
"Content-transfer-encoding:8bit/n"

#: Hello.php:14
MsgId "Hello world!"
Msgstr "World, hello." "

3. Create Mo File
Msgfmt-o Hello.mo Hello.po
A hello.mo binary file will be generated after running.

4, finally will Hello.po, Hello.mo copy to the relevant family directory can be.

Five, in IE Input: http://localhost/hello.php, now the result is: "The world, Hello." ”

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.