PHP Smarty's summary of enhanced internationalization through gettext

Source: Internet
Author: User
Tags i18n

1. Use gettext. Because Smarty has the corresponding gettext plug-in, you can directly use this plug-in.

The procedure is as follows:

1.1 Add the following code to a public include file:

// $ Domain_info ['lang '] indicates the language value passed in by the cookie. $ language_code = $ domain_info ['lang']; // set the interface language to Chinese if ($ your age_code = 'zh _ cn') {// set the target language putenv ("LANG = $ your age_code"); setlocale (LC_ALL, $ language_code); // $ package is the mo file name $ package = 'i18n _ zh '; // bindtextdomain ($ package, '/var/locale'); // you can specify the textdomain ($ package) Name of the mo file to be searched );
// Specify the bind_textdomain_codeset ($ package, 'utf-8');} elseif ($ response age_code = 'big5') encoding returned by the mo File ') {// The interface language is set to traditional: $ export age_code = 'zh _ TW '; putenv ("LANG = $ export age_code"); setlocale (LC_ALL, $ export age_code ); $ package = 'i18n _ tw '; bindtextdomain ($ package,'/var/locale '); textdomain ($ package); bind_textdomain_codeset ($ package, 'utf-8');} else {// The interface language is English // set the target language putenv ("LANG = $ language_code"); setlocale (LC_ALL, $ language_code ); // $ package is the mo file name $ package = 'i18n _ en'; // bindtextdomain ($ package, '/var/locale '); // set the file name textdomain ($ package) of the mo file to be searched; // specify the bind_textdomain_codeset ($ package, 'utf-8') encoded for gettext returned from the mo file ');} ***************/

1.2 Add the t tag required by the smarty plug-in to the corresponding template.

1.3 use the tool provided by the smarty plug-in to generate a c file

This c file extracts the tag strings from all the templates.

php -q ./tsmarty2c.php  *.html  $package.c

Note: The default c file name is the best defined in 1.1.

Note that the open tag and close tag defined in tsmarty2c. php must be consistent with the settings in the smarty configuration file.

1.4 calling linux requires xgettext to generate a file suffixed with po for the c file in 1.3

xgettext -d $package    $package.c

Note: If the file is not Asc encoded, it must be specified in the above command

-- File-code = file encoding

1.5 edit the $ package. po generated in 1.4 and add the corresponding translation string

In several languages, you can edit and generate several po files respectively.

1.6 call the linux system and obtain msgfmt to generate a binary file suffixed with mo from the po file 1.3 in 1.5.

msgfmt -o $package.mo $package.po

1.7 create a locale Directory, which must be the same as bindtextdomain ($ package, '/var/locale') in 1.1.

Take Section 1.1 as an example. First, create the locale directory under the/var/(existing) directory. the locale structure is

|-- en_US
|   `-- LC_MESSAGES
|
|-- $package.mo
|
|-- zh_CN
|   `-- LC_MESSAGES
|
|-- $package.mo
|
`-- zh_TW
`-- LC_MESSAGES
|-- $package.mo

The first-level directory is set according to the definition of $ language_code in 1.1. This is required. Otherwise, the second-level directory LC_MESSAGES is fixed and contains the mo file generated in 1.6.

2. Because the website uses Smaty, we can extract all the text in the template and put it in the language file.
It is loaded through the config_load of smarty, so that the language file is put in the directory set by $ smarty-> config_dir.
The following code is included in php,

$ Smarty-> config_load ('chs. lang ');

// $ LangTo passCookieOrSessionPage language value obtained

Switch ($ lang ){

Case 'zh-cn ':

$ Smarty-> config_load ('chs. lang ');

Break;

Case 'zh-tw ':

Header ('cht. lang ');

Break;

Default:

Header ('cht. lang ');

Break;

}


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.