Introduction to GetText

zhaozj2021-02-16  43

Removal from fanyamin in PHP.NET and

http://www.onlamp.com/pub/a/php/2002/06/13/php.html

The GetText function implements the application interface supported by localized language, which can be used for the internationalization of the PHP application, detailed explanation documents, please see

http://www.gnu.org/software/gettext/manual/index.html

demand:

To use these functions, you must

http://www.gnu.org/software/gettext/gettext.html Download Install GNU GetText Package

To add GNU GetText support in your PHP installation, you must add -with-gettext [= dir] to select, where DIR is the installation directory of GetText, default is / usr / local.

Note For Win32 users, you must copy GNU_Gettext.dll to the System32 directory under Windows, renamed libintl-1.dll from PHP 4.2.3, and simultaneously install iconv.dll

Function list

Bind_textdomain_codset - Specify The Character Encoding in Which The Messages from The Domain Message Catalog Will Be Returned

BindtextDomain - Sets The Path for A Domain

DcgetText - Overrides The Domain for a Single Lookup

DCNGETTEXT - PLURAL VERSION OF DCGETTEXT

DgetText - Override The Current Domain

DNGETTEXT - PLURAL VERSION OF DGETTEXT

GetText - Lookup a message in the current domain

NgetText - Plural Version of getText

TextDomain - Sets The Default Domain

For the sake of simplicity, GetText PHP Extension allows the use of getText () functions to dynamically translate the source string as a suitable destination string in your PHP code.

If the string is still not translated, the source string is retained.

E.g

// i18n support information here

$ Language = 'en';

Putenv ("Lang = $ LANGUAGE");

SetLocale (lc_all, $ tanguage);

// set the text domain as 'Messages'

$ domain = 'messages';

BindtextDomain ($ Domain, "/www/htdocs/site.com/locale");

TextDomain ($ domain);

Echo GetText ("A String to Be Translated Would Go Here";

?>

Install a getText file

GetText needs a local directory with all translation strings, the basic structure is as follows

/ locale

/ EN

/ LC_MESSAGES

Messages.po

Messages.mo

Create a subdirectory for each language you want to support in the locale directory

// the language code goes here

$ lGuage = 'zh_cn';

Putenv ("Lang = $ LANGUAGE"); setLocale (lc_all, $ tanguage);

// ....

?>

E.g:

/ locale

/ EN

/ LC_MESSAGES

Messages.po

Messages.mo

/ zh_cn

/ LC_MESSAGES

Messages.po

Messages.mo

After the zh_cn directory creation, use the GetText function in the PHP file to label the need to translate and text, and then you can use the command.

$ XGettext -n * .php

To generate a translation string file Messages.po, for example

# Some Descriptive Title.

# Copyright (c) Year Free Software Foundation, Inc.

# First author , year.

#

#, Fuzzy

"Msgid" "

"Msgstr" "

"Project-id-version: package version / n"

"Pot-Creation-Date: 2002-04-06 21: 44-0500 / 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"

#: GetText_example.php: 12

Msgid "a string to be translated would go here"

"Msgstr" "

This file includes all strings called getText (), providing translators to translate into different languages

Then, you can convert these POT files * .po to the GetText function that really understands the binary * .mo

The following command can create a Message.MO file and must be placed in the correct directory locale / / lc_messages /

$ Msgfmt messages.po

Note If a new file containing new strings is added, how to maintain a constantly updated message.po file including all strings translation

At this time, you can use the MsgMerge functionality of GetText.

$ Ls

EXAMPLE.PHP

$ XGettext -n * .php

$ Ls

Example.php messages.po

// ...

// Translates the messages.po file now

// ...

$ Msgfmt messages.po

$ Ls

Example.php messages.po messages.mo

// ...

// changes the example.php file

// ...

$ Mv messages.po ild.po

$ XGettext -n * .php

$ Ls

Example.php messages.po messages.mo ild.po

$ Msgmerge old.po messages.po --output-file = new.po

$ Ls

Example.php messages.po message.mo new.po ild.po // ...

// translates the new.po file

// ...

$ Msgfmt new.po

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

New Post(0)