Home Backend Development PHP Tutorial PHP中使用gettext来支持多语言的方法_PHP

PHP中使用gettext来支持多语言的方法_PHP

Jun 01, 2016 pm 12:16 PM
gettext multi-language

我们今天用一个简单的实例说明一下在PHP中的getText的用法(getText是一系列的工具和库函数,帮助程序员和翻译人员开发多语言软件的), 从而实现PHP的i18n.
现在, 我们假设要显示一个返回主页的link:
复制代码 代码如下:
//home.php:
$str = 'home';
print {$str}
HTML;

下面开启我们多语言的开发之旅:
创建pot文件,pot是Portable Object Template的首字母缩写,与po对应的是mo,mo是Machine Object的首字母缩写。前者意指原始的字符串文件,一般用于给翻译人员去修改的,后者则是与机器相关的,一般是供程序读取。可以手工创建pot文件,也可以通过xgettext从代码中抽取字符串来产生。这里是用xgettext来产生的:
xgettext -a home.php -o home.pot
运行该命令后,我们发现,在当前目录下,产生了一个名home.pot的文件,打开该文件,可以看到:
复制代码 代码如下:
# 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-07-23 20:56+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 "

根据pot产生不同语言的po文件,这里我们先产生一个简体中文的po文件:
export LANG=zh_CN.gb2312
msginit -l zh_CN.gb2312 -i home.pot
运行该命令后,我们发现,在当前目录下,产生了一个名zh_CN.po的文件,打开该文件,可以看到:
复制代码 代码如下:
# Chinese translations for PACKAGE package
# 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: 2009-07-23 20:56+0800\n"
"PO-Revision-Date: 2009-07-23 21:00+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 "

翻译zh_CN.po里对应的字符串为中文:
复制代码 代码如下:
# Chinese translations for PACKAGE package
# 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: 2009-07-23 20:56+0800\n"
"PO-Revision-Date: 2009-07-23 21:00+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 "主页

根据po文件生成mo文件。
msgfmt zh_CN.po -o zh_CN.mo
运行该命令后,我们发现,在当前目录下,产生了一个名zh_CN.mo的文件。它是二进制的,不能用文本编辑器打开。
安装mo文件到特定目录中:
cp -f zh_CN.mo .local/LC_MESSAGES/home.mo
修改程序。
复制代码 代码如下:
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'); //也可以使用_('home')
print {$str}
HTML;

运行这个脚本, 看看, 是不是输出正确的中文了呢?
添加其它语言也很容易,不需要修改程序,只需要像对待中文一样,生成一个mo文件,并安装到系统中对应的目录即可。切换不同的语言仅仅是修改当前的locale就行了。

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
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)

How to use vue and Element-plus to achieve multi-language and international support How to use vue and Element-plus to achieve multi-language and international support Jul 17, 2023 pm 04:03 PM

How to use vue and Element-plus to achieve multi-language and international support Introduction: In today's globalized era, in order to cope with the needs of users in different languages ​​and cultures, multi-language and international support have become essential features for many front-end projects . This article will introduce how to use vue and Element-plus to achieve multi-language and international support so that the project can flexibly adapt to the needs of different language environments. 1. Install Element-plusElement-plus is vue official

How to implement a multilingual website in PHP How to implement a multilingual website in PHP May 22, 2023 am 11:31 AM

With the increasing popularity of the Internet, more and more websites need to support multiple languages. This is because the website's audience may come from different regions and cultural backgrounds, and if the website is only available in a single language, it may limit the number and experience of visitors. This article will introduce how to implement a multilingual website in PHP. 1. Creation and design of language files Language files are files that store all text strings and their corresponding translations, and need to be created in a specific format. When creating a language file, you need to consider the following aspects: 1. Naming and storage location The file name should be

How does CakePHP handle multiple languages? How does CakePHP handle multiple languages? Jun 06, 2023 am 08:03 AM

CakePHP is a popular PHP development framework that helps developers quickly build high-quality web applications. With the development of globalization, more and more applications need to support multiple languages, and CakePHP also provides corresponding support. This article will introduce how CakePHP handles multiple languages. 1. Multi-language support Multi-language support is an important feature of CakePHP. Starting with version 2.0, CakePHP supports the gettext file format, which

Tips for using i18n to implement multi-language switching in Vue Tips for using i18n to implement multi-language switching in Vue Jun 25, 2023 am 09:33 AM

With the continuous development of internationalization, more and more websites and applications need to support multi-language switching functions. As a popular front-end framework, Vue provides a plug-in called i18n that can help us achieve multi-language switching. This article will introduce common techniques for using i18n to implement multi-language switching in Vue. Step 1: Install the i18n plug-in First, we need to install the i18n plug-in using npm or yarn. Enter the following command at the command line: npminst

How to use PHP for multi-language framework development? How to use PHP for multi-language framework development? May 13, 2023 am 08:09 AM

With the deepening of globalization, more and more websites and applications need to support multiple languages. As a programming language widely used in Web development, PHP also needs to support the development of multi-language frameworks. This article will introduce how to use PHP for multi-language framework development. 1. What is a multilingual framework? First, let’s first understand what a multilingual framework is. Multilingual framework, as the name suggests, is a framework that can support multiple languages. In internationalization and localization design, multi-language framework is essential. It can support a variety of

How to use Flask-Babel to implement multi-language support How to use Flask-Babel to implement multi-language support Aug 02, 2023 am 08:55 AM

How to use Flask-Babel to achieve multi-language support Introduction: With the continuous development of the Internet, multi-language support has become a necessary feature for most websites and applications. Flask-Babel is a convenient and easy-to-use Flask extension that provides multi-language support based on the Babel library. This article will introduce how to use Flask-Babel to achieve multi-language support, and attach code examples. 1. Install Flask-Babel. Before starting, we need to install Flask-Bab first.

How to use PHP and Typecho to build a website with multi-language support How to use PHP and Typecho to build a website with multi-language support Jul 21, 2023 pm 11:21 PM

How to use PHP and Typecho to build a website with multi-language support Introduction: With the development of globalization, building a website with multi-language support has gradually become a goal pursued by enterprises and individuals. As a popular programming language, PHP, combined with Typecho, an excellent PHP open source blog program, can easily build multi-language websites. This article will introduce how to use PHP and Typecho to build a website with multi-language support, and provide relevant code examples. 1. Install and configure Typecho first

How to use Layui to develop a website that supports multi-language switching How to use Layui to develop a website that supports multi-language switching Oct 25, 2023 am 10:55 AM

How to use Layui to develop a website that supports multi-language switching. With the development of globalization, more and more websites need to support multi-language switching to meet the needs of different users. Layui is a very popular front-end framework that provides a series of easy-to-use components and tools that can help us quickly develop beautiful websites. This article will introduce how to use Layui to develop a website that supports multi-language switching, and provide specific code examples. First, we need to introduce Layui related files into the web page. Can

See all articles