Use GetText to implement PHP's international programming

xiaoxiao2021-03-06  70

Usually people write the program, it is written in the program, such as: echo "Hello World!"; If you want to change into its language, write an internationalization process, you must open it one by one, and the program is short. OK, if the program has 10,000 or even more, it is not so easy to change it. Recently, with the gradual standardization of I18n, I also talk about how to achieve international support in PHP. Like other programs, PHP can also use the GetText Suite Writing I18N program to implement NLS (Native Language Support) International Support, please refer to the official document (http://www.gnu.org/Manual/gettext/index. HTML). Realization: The program designer writes the information to be displayed in the program code. When the program does not directly display the information written by the programming designer, it will look for an information file that set up the language. If not found, the information in the display code will be displayed. First, install setting GetText Suite: 1) * NIX System: 1, from http://www.gnu.org/software/gettext/gettext.html to download GetText Package, install 2, compile PHP, plus " With-gettext [= dir] ", where DIR is the directory installed with the GetText, the default is: / usr / local 3, saved, then Restart Server. 2) Win32 system: 1, you need to copy the GNU_Gettext.dll file to the system directory (EX: C: / Winnt / System32 or C: / Windows / System32), the file named libintl-1.DLL after PHP 4.2.3, It can be obtained under PHP4 / DLLs. 2, open the php.ini file, look up extension = php_gettext.dll, remove the front ";" 3, save, then restart Server. If everything goes well, you can see GetText words in phpinfo (), which has been set up. Second, there are several functions in the php_gettext.dll kit, please see the relevant manual. Here we only remember 3 functions, as follows:

String BindtextDomain (String Domain, String Directory) String Text_Domain String GetText (String Message) STRING GETTEXT (STRING MESSAG)

Third, writing I18N program:

Below is the I18N program: hello.php

>

Enter: http://localhost/hello.php, the output is: "Hello World!" NOTE: Follow the habits of GNU Package, you can use _ (...) instead of getText (...), This will make a lot of GetText. Fourth, then set the GetText PO file: 1. Create a directory structure, as shown below: bindtextdomain's dir / language / lc_messages domain.po domain.mo where the directory used by bindtextDomain (), Langguage is the language, Domain For the Package name. Take the above as an example: / local / zh_tw / lc_messages Hello.po Hello.mo 2, create a PO file XGettext -d [What is the defined package name] [Program File Name] The following XGettext below, the MSGFMT file file can be (http: //switch.dl.sourceForge.Net/SourceForge/gnuwin32/gettext-0.10.40-bin.zip) Download, need libiconv.dll, libintl.dll support. Take the above hello.php file as an example, $ XGettext -d Hello Hello.php will generate a Hello.po file, the content is as follows: # Some Descriptive Title. # Copyright © Year Free Software Foundation, Inc. # Firsthor , year. ##, fuzzymsgid "" "" Project-id-version: package version / n "" Pot-Creation-Date: 2003-04-21 22: 31 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 "#: Hello.php: 14msgid" Hello World! "Msgstr" In the Hello.php file, all strings in the GetText function, you only need to translate the MSGID value when translating, if you translate into Chinese.

转载请注明原文地址:https://www.9cbs.com/read-109560.html

New Post(0)