使用autotool整合gettext

初始的目录结构是

.
└── src
    └── fool.c

fool.c的内容是

#include <stdio.h>

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include "gettext.h"
#define _(STR) gettext(STR)
#define N_(STR) STR

char *gs = N_("Global string\n");

int main (void)
{
	setlocale (LC_ALL, "");

	bindtextdomain (PACKAGE, LOCALEDIR);

	textdomain (PACKAGE);

	printf (_("Hello, world!\n"));
	printf (_(gs));

	return 0;
}

1.生成configure.ac

autoscan
mv configure.scan configure.ac
gvim configure.ac

修改后的configure.ac

#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.65])
AC_INIT([fool], [0.0.1], [4179e1@gmail.com])
AC_CONFIG_SRCDIR([src/fool.c])
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_MACRO_DIR([m4])
AC_CONFIG_AUX_DIR([build-aux])
AM_GNU_GETTEXT_VERSION([0.17])
AM_GNU_GETTEXT([external])
AM_INIT_AUTOMAKE([gnu -Wall -Werror])

# Checks for programs.
AC_PROG_CC
AC_PROG_INSTALL
AC_LANG([C])

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.
AC_CHECK_FUNCS([setlocale])

AC_CONFIG_FILES([Makefile src/Makefile])
AC_OUTPUT
  1. Makefile.am
ACLOCAL_AMFLAGS = -I m4
SUBDIRS = src

3.src/Makefile.am

AM_CFLAGS = -Wall -DLOCALEDIR=\"$(localedir)\"

ACLOCAL_AMFLAGS = -I m4

bin_PROGRAMS = fool

fool_SOURCES = fool.c
fool_LDADD = $(LIBINTL)

EXTRA_DIST = gettext.h

4.初始化gettext

lyre@linux:~/misc/gettext/autotool> gettextize --copy --no-changelog
Creating po/ subdirectory
Copying file ABOUT-NLS
Copying file build-aux/config.rpath
Not copying intl/ directory.
Copying file po/Makefile.in.in
Copying file po/boldquot.sed
Copying file po/en@boldquot.header
Copying file po/en@quot.header
Copying file po/insert-header.sin
Copying file po/Makevars.template
Copying file po/quot.sed
Copying file po/remove-potcdate.sin
Copying file po/Rules-quot
Creating initial po/POTFILES.in
Creating po/ChangeLog
Copying file m4/gettext.m4
Copying file m4/iconv.m4
Copying file m4/lib-ld.m4
Copying file m4/lib-link.m4
Copying file m4/lib-prefix.m4
Copying file m4/nls.m4
Copying file m4/po.m4
Copying file m4/progtest.m4
Creating m4/ChangeLog
Updating Makefile.am (backup is in Makefile.am~)
Updating configure.ac (backup is in configure.ac~)
Creating ChangeLog

Please use AM_GNU_GETTEXT([external]) in order to cause autoconfiguration
to look for an external libintl.

Please create po/Makevars from the template in po/Makevars.template.
You can then remove po/Makevars.template.

Please fill po/POTFILES.in as described in the documentation.

Please run 'aclocal -I m4' to regenerate the aclocal.m4 file.
You need aclocal from GNU automake 1.9 (or newer) to do this.
Then run 'autoconf' to regenerate the configure file.

You will also need config.guess and config.sub, which you can get from the CVS
of the 'config' project at http://savannah.gnu.org/. The commands to fetch them
are
$ wget 'http://savannah.gnu.org/cgi-bin/viewcvs/*checkout*/config/config/config.guess'
$ wget 'http://savannah.gnu.org/cgi-bin/viewcvs/*checkout*/config/config/config.sub'

You might also want to copy the convenience header file gettext.h
from the /usr/share/gettext directory into your package.
It is a wrapper around <libintl.h> that implements the configure --disable-nls
option.

你会发现configure.ac和Makefile.am的内容有所变化,并且多了po目录

5.复制gettext.h

cp /usr/share/gettext/gettext.h src

7.把m4宏复制到m4目录下

aclocal -I m4 --install

8.生成config.h

autoheader

9.生成configure脚本

autoconf

10.执行automake

lyre@linux:~/misc/gettext/autotool> automake --add-missing -fc
configure.ac:10: installing `build-aux/install-sh'
configure.ac:10: installing `build-aux/missing'
src/Makefile.am: installing `build-aux/depcomp'
Makefile.am: installing `./INSTALL'
Makefile.am: required file `./NEWS' not found
Makefile.am: required file `./README' not found
Makefile.am: required file `./AUTHORS' not found
Makefile.am: required file `./ChangeLog' not found
Makefile.am: installing `./COPYING' using GNU General Public License v3 file
Makefile.am:     Consider adding the COPYING file to the version control system
Makefile.am:     for your code, to avoid questions about which license your project uses.

11.新建缺少的文件

touch NEWS README AUTHORS ChangeLog
automake --add-missing -fc

12.建立翻译环境

cd po
gvim Makevers.template #修改COPYRIGHT_HOLDER
mv Makevars.template Makevars
gvim POTFILES.in

POTFILES.in内容如下:

# List of source files which contain translatable strings.
src/fool.c

13.中文翻译

cd po
msginit -l zh_CN
gvim zh_CN.po #翻译
echo zh_CN >>LINGUAS

14.重新配置

autoreconf

15.编译安装

mkdir build
cd build
../configure
cd po
make update-po
cd ..
make
make install

16.测试

lyre@linux:~/misc/gettext/autotool/build> fool             
你好,世界!
全局字符串
lyre@linux:~/misc/gettext/autotool/build> LC_ALL=C fool
Hello, world!
Global string
updatedupdated2022-02-222022-02-22