Home php教程 php手册 php程序的国际化实现方法(利用gettext)

php程序的国际化实现方法(利用gettext)

Jun 13, 2016 pm 12:06 PM
gettext php one use globalization accomplish Expand build method Check step environment of program first

步骤一:搭建环境
1,首先查看你的php扩展目录下是否有php_gettext.dll这个文件,如果没有,这就需要你
下载一个或是从其他地方拷贝一个,然后放到php扩展目录。
2,打开php.ini,查找”;extension=php_gettext.dll“ ,然后去除注释,重启apache。
步骤二:原理讲解
假如你的没有国际化的程序里有这样的代码,echo "你好";,而国际化的程序你要写成
echo gettext("你好");,然后再在配置文件里添加“你好”相对应的英文“Hi”。
这时,中国地区浏览都会在屏幕上输出“你好”,而美国地区浏览都会在屏幕上输出
“Hi”。也就是说,最终显示什么是根据你的配置文件而定的,如果找不到配置文件,
才会输出程序里面的内容。
步骤三:编码测试
1,我们在d:\www下面新建文件hi.php,详细代码如下

复制代码 代码如下:


$domain = 'test';
bindtextdomain($domain, "locale/");//设置某个域的mo文件路径
textdomain($domain);//设置gettext()函数从哪个域去找mo文件
echo gettext("Hi!");//_()是gettext()函数的简写形式
?>


这时你运行改程序,只会输出“Hi”。但我们是中国人,我们不认识“Hi”,
我们只认识“你好”,这时就要配置文件出马。配置文件的生成一般借助一款工具。
下载地址:http://nchc.dl.sourceforge.net/sourceforge/gnuwin32/gettext-0.14.4.exe
安装好以后,为了在任意目录里使用,需要把“安装路径/bin”添加到系统环境变量里。
步骤四:配置文件的生成
1,我们假设你的工具已经安装好,并且可以在任意目录使用。现在就要运行cmd,并把
路径切换到d:\www下面,也就是hi.php所在目录。
键入xgettext -d hi hi.php --from-code=gb2312,然后执行,这时你可以看到新生成一个
hi.po文件,注意:--from-code=gb2312,其中gb2312还可以是utf-8。
2,打开hi.po文件,显示如下:

复制代码 代码如下:


# 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: 2009-01-19 17:47+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"
#: hi.php:6
msgid "Hi!"
msgstr ""


现在有两个地方需要修改,
1:"Content-Type: text/plain; charset=CHARSET\n"
2:msgstr ""
把1中的CHARSET修改成gb2312,然后把2修改成msgstr "你好"。
3,键入msgfmt -o hi.mo hi.po,执行,这时生成hi.mo文件。
然后在d:\www下新建locale\zh_Cn\LC_MESSAGES目录,把hi.mo拷到这里就行了。
4,现在重启apache,再次运行,屏幕上可以输出“你好”。
其他:
如果使用utft-8编码的话,需要使用
bind_textdomain_codeset($domain,'UTF-8');
相应的要把hi.po里的CHARSET改成utf-8,还需要把hi.po保存成utf-8格式,
再次生成hi.mo就行了。
总结:
我们都希望我们写的程序可以被大众甚至国际普遍使用,像大名鼎鼎的wordpress的
国际化使用的也是他。gettext还是非常不错的,简单易用,
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles