Using GetText in PHP to support multilingual methods _php Tips

Source: Internet
Author: User
Tags gettext locale
We use a simple example today to illustrate the use of GetText in PHP (GetText is a series of tools and library functions that help programmers and translators develop multi-language software) to implement PHP's i18n.
Now, let's assume that you want to display a link that returns to the home page:
Copy Code code as follows:

home.php:
$str = ' home ';
Print <<<a href= "#" >{$str}</a>
HTML;

Here's how to start our Multilingual development tour:
Create the pot file, pot is the Portable object template initials, and the PO corresponds to the MO,MO is the initials of machine object. The former means the original string file, generally used for translators to modify, the latter is machine-related, generally for the program read. You can create pot files manually, or you can extract strings from your code by Xgettext. This is produced using Xgettext:
Xgettext-a Home.php-o Home.pot
After running the command, we found that, in the current directory, a file with a name Home.pot was created, and the file was opened, and you can see:
Copy Code code 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.
# 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 "

According to pot production of different language PO files, here we first produce a simplified Chinese po file:
Export lang=zh_cn.gb2312
Msginit-l zh_cn.gb2312-i Home.pot
After running the command, we found that, in the current directory, a file with a name Zh_cn.po was created, and the file was opened, and you can see:
Copy Code code as follows:

# Chinese translations for PACKAGE PACKAGE
# PACKAGE Simplified Chinese translation of software packages.
# Copyright (C) 2009 the PACKAGE ' S copyright HOLDER
# This file is distributed under the same license as the PACKAGE PACKAGE.
# #
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 translation Zh_cn.po is Chinese:
Copy Code code as follows:

# Chinese translations for PACKAGE PACKAGE
# PACKAGE Simplified Chinese translation of software packages.
# Copyright (C) 2009 the PACKAGE ' S copyright HOLDER
# This file is distributed under the same license as the PACKAGE PACKAGE.
# #
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: "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 "Home

Generate Mo File according to PO file.
MSGFMT Zh_cn.po-o Zh_cn.mo
After running the command, we found that a file named Zh_cn.mo was produced 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.
Copy Code code as follows:

SetLocale (Lc_all, ' zh_cn ');
Specify location of translation tables
Bindtextdomain ("Home", ".");
Choose Domain
Textdomain ("Home");
Translation is looking to./locale/zh_cn/lc_messages/home.mo now
$str = GetText (' home '); You can also use _ (' home ')
Print <<<a href= "#" >{$str}</a>
HTML;

Run this script to see if the output is correct in Chinese?
Adding other languages is also very easy, do not need to modify the program, just like the Chinese, to generate a Mo file, and installed to the corresponding directory in the system. Switching to a different language is just to modify the current locale.

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.