Php program internationalization (using gettext) _ PHP Tutorial

Source: Internet
Author: User
Php program internationalization implementation method (using gettext ). Step 1: Set up environment 1. first check whether the php_gettext.dll file exists in your php extension directory. if not, you need to download one or copy one from other places, Step 1: build the environment
1. first check whether the php_gettext.dll file exists in your php extension directory. if not, you need
Download one or copy one from other places and put it to the php extension directory.
2. open php. ini, find "; extension = php_gettext.dll", remove comments, and restart apache.
Step 2: Principles
If you do not have an international program with such code, echo "Hello"; while an international program needs to be written
Echo gettext ("Hello");, and then add the corresponding English "Hi" in the configuration file ".
In this case, browsing in China will output "Hello" on the screen, while browsing in the United States will output on the screen.
"Hi ". That is to say, what is displayed depends on your configuration file. if the configuration file cannot be found,
To output the content in the program.
Step 3: coding test
1. create the file hi. php under d: \ www. the detailed code is as follows:

The code is as follows:


$ Domain = 'test ';
Bindtextdomain ($ domain, "locale/"); // you can specify the mo file path for a domain.
Textdomain ($ domain); // specifies the domain from which the gettext () function locates the mo file.
Echo gettext ("Hi! "); // _ () Is the abbreviated form of the gettext () function.
?>


Then you run the program change and only output "Hi ". But we are Chinese, and we do not know "Hi ",
We only know "Hello", and the configuration file will be launched immediately. A tool is usually used to generate configuration files.
: Http://nchc.dl.sourceforge.net/sourceforge/gnuwin32/gettext-0.14.4.exe
After installation, you need to add "installation path/bin" to the system environment variable to use it in any directory.
Step 4: Create a configuration file
1. assume that your tool has been installed and can be used in any directory. Run cmd now and
Switch the path to d: \ www, that is, the directory where hi. php is located.
Type xgettext-d hi. php -- from-code = gb2312, and then execute the command. then, you can see a new
Hi. po file. Note: -- from-code = gb2312, where gb2312 can also be UTF-8.
2. open the hi. po file and display the following:

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 version \ n"
"Report-Msgid-Bugs-To: \ n"
"POT-Creation-Date: + 0800 \ n"
"PO-Revision-Date: YEAR-MO-DA HO: MI + ZONE \ n"
"Last-Translator: FULL NAME \ N"
"Language-Team: LANGUAGE \ N"
& Quot; MIME-Version: 1.0 \ n & quot"
"Content-Type: text/plain; charset = CHARSET \ n"
"Content-Transfer-Encoding: 8bit \ n"
#: Hi. php: 6
Msgid "Hi! "
Msgstr ""


Now there are two places to modify,
1: "Content-Type: text/plain; charset = CHARSET \ n"
2: msgstr ""
Change CHARSET in 1 to gb2312, and then change 2 to msgstr "Hello ".
3. type msgfmt-o hi.mo hi. po and run the command to generate the hi.mo file.
Create the locale \ zh_Cn \ LC_MESSAGES directory under d: \ www and copy hi.mo here.
4. restart apache and run it again. "Hello" can be output on the screen ".
Others:
If you use utft-8 encoding, you need to use
Bind_textdomain_codeset ($ domain, 'utf-8 ');
Replace CHARSET in hi. po with UTF-8, and save hi. po as UTF-8,
Generate hi.mo again.
Summary:
We all hope that the programs we write can be widely used by the masses or even internationally, such as the well-known wordpress
It is also used internationally. Gettext is quite good and easy to use,

Example 1: first check whether the php_gettext.dll file exists in your php extension directory. if not, you need to download one or copy one from another location ,...

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.