Using gettext in PHP to support multiple languages _ PHP Tutorial

Source: Internet
Author: User
Use gettext in PHP to support multi-language methods. Today, we use a simple example to describe the usage of getText in PHP (getText is a series of tools and library functions that help programmers and translators develop multilingual software ), so today, we use a simple example to describe the usage of getText in PHP (getText is a series of tools and library functions that help programmers and translators develop multilingual software ), so as to implement PHP i18n.
Now, let's assume that we want to display a link that returns to the home page:

The code is as follows:


// Home. php:
$ Str = 'home ';
Print <{$ Str}
HTML;


Next we will start our multi-language development journey:
Create a pot file. pot is the abbreviation of Portable Object Template. po corresponds to mo and mo is the abbreviation of Machine Object. The former refers to the original string file, which is generally used for modification by the translator. The latter is machine-related and generally for the program to read. You can create a pot file manually or use xgettext to extract strings from the code. Xgettext is used here:
Xgettext-a home. php-o home. pot
After running this command, we found that a file named home. pot is generated in the current directory. open this file and we can see that:

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"
#: Home. php: 2
Msgid "home"
Msgstr"


Po files in different languages are generated based on the pot. here we first generate a po file in simplified Chinese:
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.po is generated in the current directory. open this file and we can see that:

The code is as follows:


# Chinese translations for PACKAGE package
# Simplified Chinese translation of the 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 version \ n"
"Report-Msgid-Bugs-To: \ n"
"POT-Creation-Date: + 0800 \ n"
"PO-Revision-Date: + 0800 \ n"
"Last-Translator: FULL NAME \ N"
"Language-Team: Chinese \ n"
& Quot; MIME-Version: 1.0 \ n & quot"
"Content-Type: text/plain; charset = GB2312 \ n"
"Content-Transfer-Encoding: 8bit \ n"
#: Test. php: 2
Msgid "home"
Msgstr"


Translate the string corresponding to zh_CN.po into Chinese:

The code is as follows:


# Chinese translations for PACKAGE package
# Simplified Chinese translation of the 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 version \ n"
"Report-Msgid-Bugs-To: \ n"
"POT-Creation-Date: + 0800 \ n"
"PO-Revision-Date: + 0800 \ n"
"Last-Translator: \ N"
"Language-Team: Chinese \ n"
& Quot; MIME-Version: 1.0 \ n & quot"
"Content-Type: text/plain; charset = GB2312 \ n"
"Content-Transfer-Encoding: 8bit \ n"
#: Test. php: 2
Msgid "home"
Msgstr "homepage


Generate a mo file based on the po file.
Msgfmt zh_CN.po-o zh_CN.mo
After running this command, we found that a file named zh_CN.mo is generated in the current directory. It is binary and cannot be opened in a text editor.
Install the mo file to a specific directory:
Cp-f zh_CN.mo. local/LC_MESSAGES/home.mo
Modify the program.

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 this script to see if the correct Chinese characters are output?
It is easy to add other languages. without modifying the program, you only need to generate a mo file and install it to the corresponding directory in the system just like treating Chinese. To switch between different languages, you only need to modify the current locale.

GetText is a series of tools and library functions that help programmers and translators develop multilingual software...

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.