gettext 国际化
最后更新于:2022-04-02 02:23:57
[TOC]
## 概述
1. 支持 linux 与 windows
1. 除C语言外, GNU gettext还支持C++,Objective-C,Pascal/Object Pascal,sh脚本,bash脚本,Python,GNU CLISP,Emacs Lisp,librep,GNU Smalltalk,Java,GNU awk,wxWidgets(通过wxLocale类),YCP(YaST2语言),Tcl,Perl,PHP,Pike,Ruby以及R。用法均与在C语言上类似
1. wordpress的国际化就是用的GNU gettext
### 大致原理
1. GNU gettext使用PO或MO文件来实现国际化和本地化
2. PO的意思是Portable Object,是一种文本结构,可以方便的由人们阅读和修改
3. PO的意思是Portable Object,是一种文本结构,可以方便的由人们阅读和修改
4. 一个程序要支持多种语言,每一种语言都需要自己 的PO或MO文件
![UTOOLS1596544492132.png](https://img03.sogoucdn.com/app/a/100520146/0d42b4e557e1954b4bf20d1100a4430a)
## php 开启模块
`;extension=php_gettext.dll`
### 提取字符可手动也可通过 xgettext
## 示例
### xgettext 提取
xgettext 提取 home.php `xgettext -a home.php -o home.pot` 生成的 home.pot
, 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 ""
```
生成 home.pot 文件 ``` > echo $LANG zh_CN.UTF-8 > msginit -l zh_CN.UTF-8 -i home.pot ```
, 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` 移动到指定目录 `cp -f zh_CN.mo local/zh_CN/LC_MESSAGES/home.mo` 执行,展示效果 修改 home.php 文件
执行 ``` > php home.php > 主页 ``` ### 手动生成 .pot 文件 手动添加 home.pot
, 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 ""
```
';
home.php
``` $str = 'home'; print <<{$str} ```xgettext 提取 home.php `xgettext -a home.php -o home.pot` 生成的 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生成 home.pot 文件 ``` > echo $LANG zh_CN.UTF-8 > msginit -l zh_CN.UTF-8 -i home.pot ```
home.pot
``` # 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. #根据po文件生成mo的二进制文件 `msgfmt zh_CN.po -o zh_CN.mo` 移动到指定目录 `cp -f zh_CN.mo local/zh_CN/LC_MESSAGES/home.mo` 执行,展示效果 修改 home.php 文件
home.php
``` setlocale(LC_ALL, 'zh_CN'); // Specify location of translation tables bindtextdomain("home", "locale"); // Choose domain textdomain("home"); // Translation is looking for in ./locale/zh_CN/LC_MESSAGES/home.mo now $str = gettext('home'); //也可以使用_('home') print <<{$str} html; ```执行 ``` > php home.php > 主页 ``` ### 手动生成 .pot 文件 手动添加 home.pot