Make PHP multi-language

Source: Internet
Author: User
Let PHP multi-language [turn] original address: http://www.laruence.com/2009/07/19/1003.html

It is very difficult to develop multi-language Web applications. the encoding methods, currency symbols, date formats, numeric formats, and text performances of character sets in various countries are different.

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:

// Home. php: $ str = 'home'; print <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:

# 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-: \ 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 "" MIME-Version: 1.0 \ n "" 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:

# 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 "" MIME-Version: 1.0 \ n "" 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:

# 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 "" MIME-Version: 1.0 \ n "" 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.

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 <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.

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.