Php program internationalization implementation method (using gettext)

Source: Internet
Author: User

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:
Copy codeThe Code is as follows:
<? Php
$ 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:
Copy codeThe 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 <EMAIL @ ADDRESS>, 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 <EMAIL @ ADDRESS> \ n"
"Language-Team: LANGUAGE <LL@li.org> \ 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,

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.