intltool README
http://intltool.freedesktop.org
The intltool collection can be used to do these things:
o Extract translatable strings from various source files (.xml.in, .glade, .desktop.in, .server.in, .oaf.in).
o Collect the extracted strings together with messages from traditional source files (.c, .h) in po/$(PACKAGE).pot.
o Merge back the translations from .po files into .xml, .desktop and .oaf files. This merge step will happen at build resp. installation time.
The intltool package has a script, intltoolize, which copies the various scripts and does the other magic to your module. So users building from tarballs don't need intltool, only folks building from cvs.
(This is modelled on gettextize.)
How to Use with autoconf/automake
---------------------------------
(There is a section for non-auto* configurations below)
To use intltool in your module, do the following:
o Install intltool, and make sure that the macro it installs is in aclocal's path, or do:
export ACLOCAL_FLAGS='-I /usr/local/share/aclocal'
o Add these lines to autogen.sh, after the call to gettextize:
echo "Running intltoolize"
intltoolize --copy --force --automake
o Add this line to configure.in near the top
AC_PROG_INTLTOOL([minimum required version], [no-xml])
o Add intltool-extract.in, intltool-merge.in, and intltool-update.in to EXTRA_DIST in your top-level Makefile.am and also to the top-level .cvsignore. Also add the non-.in versions to .cvsignore.
o Remove po/desk.pl and po/update.* scripts. intltool-update will take over their functionality.
At this point, translatable strings will be automatically extracted to the .po files, if you make use of the following recommendations.
The intltool-prepare script will help you to prepare the package. It will try to extract translations from existing .desktop files which will become obsolete after intltoolization has taken place.
Examples of packages that use intltool are listed in the USED file.
Details of the AC_PROG_INTLTOOL macro
-------------------------------------------
The first parameter indicates the minimum required version. The configure script will halt if the version is older than the first parameter.
The second parameter is to tell intltool that we don't need the extended xml parsing abilities provided by the XML::Parser perl module. If it is not provided, or is any value other than "no-xml", then XML::Parser will be checked for by the configure script. This feature is only available in intltool 0.31 or newer.
Extra Steps for DESKTOP Files
..............................
This step also applies for similar files (.directory, .soundlist).
o Try to run intltool-prepare.
o Make sure intltool-prepare did find existing translations in the old .desktop files and did correctly merge them into the various po/*.po files. Don't forget to commit the changed .po files; otherwise exiting translations will get lost!
o Remove old .desktop files and add new .desktop.in files.
o Adjust .cvsignore
o Adjust Makefile.am, e.g.:
--- start ----
utilsdir = $(datadir)/gnome/apps/Utilities
utils_in_files = bug-buddy.desktop.in
utils_DATA = $(utils_in_files:.desktop.in=.desktop)
@INTLTOOL_DESKTOP_RULE@
--- end ----
o Add .desktop.in files to po/POTFILES.in
Here's a .desktop.in example:
--- start ----
[Desktop Entry]
_Name=Bug Report Tool
_Comment=Report a bug in GNOME
Exec=bug-buddy
Icon=bug-buddy.png
Terminal=0
Type=Application
--- end ----
Extra Steps for GLADE Files
...........................
o Add the .glade files you want translated to POTFILES.in
o Remove the intermediate *-glade.h or strings-glade.c files and drop them from POTFILES.in
Extra Steps for SERVER Files (formerly .oafinfo or .oaf)
.............................
To get server translation extraction and merging requires a few more steps:
o Rename your .oafinfo (or .oaf) files to .oaf.in or .server.in and put an underscore before every value property for string attributes that should be localized.
o Add the new .oaf.in or .server.in files to POTFILES.in.
o Put lines like these in every Makefile.am that installs oaf files:
--- start ----
oafdir = $(datadir)/oaf
oaf_in_files = My_OAF_info_file.oaf.in
oaf_DATA = $(oaf_in_files:.oaf.in=.oaf)
@INTLTOOL_OAF_RULE@
EXTRA_DIST=$(oaf_in_files) $(oaf_DATA)
--- end ----
At this point, your oaf translations will be extracted and merged. Also, so long as you are renaming the .oafinfo files to .oaf.in, you should take the opportunity to rename them to the new base naming convention, with namespacing, for example:
foo.oafinfo --> GNOME_Foo.oaf.in
foo-baa.oafinfo --> GNOME_Foo_baa.oaf.in
Extra Steps for XML Files (Files with .xml Extension)
.....................................................
To get xml (files with .xml extension) translation extraction and merging requires these steps:
o Rename your .xml files to .xml.in and put an underscore before every element that should be localized.
o Add the .xml.in files to POTFILES.in.
o Put lines like these in every Makefile.am that installs xml files:
--- start ----
xmldir = $(datadir)/xml
xml_in_files = My_xml_file.xml.in
xml_DATA = $(xml_in_files:.xml.in=.xml)
@INTLTOOL_XML_RULE@
EXTRA_DIST=$(xml_in_files) $(xml_DATA)
--- end ----
At this point, your xml translations will be extracted and merged. All .po files will be converted on the fly to UTF-8, and the resulting XML file will have a UTF-8 effective encoding (you should make sure that the encoding="..." declaration in the .xml.in file is either absent or actually specifies UTF-8).
Previous versions of intltool generated XML files whose contents were made of the contents of the .po files, without paying attention to the encodings used. A single "XML" file could thus have strings in different encodings. This broken behavior can be requested only by using the old xml-i18n-tools API instead of the intltool one. See old versions of xml-i18n-tools for documentation on how the old API worked.
---
XXX: add section for KEYS files. Works almost like XML files .
How to use without autoconf/automake
------------------------------------
intltool can also be used without the auto* tools. For instance in order to translate a somename.desktop.in file, you can do the following.
o Create a po/ dir.
o Add a po/POTFILES.in file, including the path to the somename.desktop.in file
Then to create the somename.desktop file all you do is:
$ intltool-merge po/ -d -u -c po/.intltool-merge-cache somename.desktop.in somename.desktop
You can also type intltool-merge --help for a bit more info.
To specify parameters for intltool-update (such as keywords or gettext domain), you can use Makevars syntax as used in recent GNU gettext, by putting something like the following in po/Makevars file:
DOMAIN = mydomain
XGETTEXT_OPTIONS = --keyword --keyword=blah
This will make "intltool-update -p" produce mydomain.pot, passing parameters "--keyword --keyword=blah" to xgettext when extracting strings.
Passing special parameters to xgettext via environment
......................................................
If you need to add parameters passed to xgettext on a case-by-case basis, you can do so using environment variable XGETTEXT_ARGS.
If you would run it as follows:
XGETTEXT_ARGS=--no-location intltool-update -p
You would create a PO Template file without lines which indicate location of messages in the source code.
Changing keywords used in xgettext invocation
.............................................
If you need to change default keywords used to extract messages from source code, you need to add variable XGETTEXT_KEYWORDS to Makefile.in.in file inside directory where intltool-update is run from, eg.
--- start ----
XGETTEXT_KEYWORDS = --keyword --keyword=P_
--- end ----
Default keywords xgettext looks for if no XGETTEXT_KEYWORDS is defined are _, N_ and U_.
Translators' comments in XML and .schemas files
...............................................
To provide comments to translators in free-form XML or .schema files, you need to precede the string to be translated with the plain XML comment. If comments contain character ">", they will be ignored (this is implementation issue, should be fixed in the future).
In .schemas files, comments need to be inside <default>, <short> or <long> elements (i.e. they cannot be before the opening tag).
这个文挡一步步地讲得很清楚,可以照着试验一下。
Autoconf/I18n-ify HelloWorld HOW-TO
In this article we are going to explain how to turn a simple Hello World application with a standard Makefile into an autotools-and I18N-enabled tree up to the point where it can be distributed.
Our existing helloworld.c file looks like the following:
#include <stdio.h>
int main (void) {
printf ("Hello, world!/n");
}
1. First we create a source tree :
/ - This is the top level directory
/src/ - Here the source will end up.
and place the helloworld.c file in the src/ dir
2. If your program has not been autoconf-enabled yet, you can create configure.scan (which is a good starting point for configure.ac) and rename it to configure.ac
autoscan # creates configure.scan
mv configure.scan configure.ac
Now edit configure.ac and make some changes. You can remove everything after AC_INIT, we'll be using AM_INIT_AUTOMAKE to pass on variables.
Add the lines
PACKAGE=helloworld
VERSION=0.0.1
AM_INIT_AUTOMAKE($PACKAGE, $VERSION)
to configure.in, just after AC_INIT
Change AC_CONFIG_HEADER to AM_CONFIG_HEADER as well.
If you have an empty AC_CONFIG_FILES macro, then comment that, or automake will fail in the next step.
Finally, add Makefile to the AC_OUTPUT macro by changing that line to read
AC_OUTPUT(Makefile)
NOTE: configure.ac used to be called configure.in
3. We add some files that automake does not make but are necessary to adhere to GNU standards.
touch NEWS README AUTHORS ChangeLog
These two files need to be created to satisfy automake
touch config.h.in Makefile.am
We will create Makefile.am later on.
4. To add some basic files (like COPYING, INSTALL, etc..)
we run automake in the toplevel directory.
automake --add-missing --gnu
5. After that we do the big i18n trick :-), also in the toplevel directory.
gettextize --force --copy # created po/ dir with some files
intltoolize # bring in the perl helper scripts
6. Run autoheader which will create config.h.in
autoheader # create config.h.in
7. Now, open up configure.in and make some modifications.
The gettext macros need to be added after the initial checks. Putting them after the checks for library functions is a good idea.
AC_PROG_INTLTOOL(0.26)
AM_GNU_GETTEXT([external])
ALL_LINGUAS="da nl" # Internationalization, means there is
# a .po file for danish and dutch.
AM_GLIB_GNU_GETTEXT
AC_OUTPUT(
Makefile
src/Makefile
intl/Makefile
po/Makefile.in
)
AC_PROG_INTLTOOL checks if a good enough intltool is available. Please require the latest intltool that exists. Intltool releases are pretty stable and often only contains bugfixes.
AM_GNU_GETTEXT adds native language support to automake, together with a compile option.
AM_GNU_GETTEXT will check for additional required functions and programs and will finally create po/POTFILES during configure.
Instead of using AM_GLIB_GNU_GETTEXT you can do the following:
[sed -e "/POTFILES =/r po/POTFILES" po/Makefile.in > po/Makefile]
The text domain is identified by PACKAGE. We will need to add a few functions later on to helloworld.c that will use this #define'd variable.
Also, this will be the base filename for all your translation files, so make sure you choose a unique one.
8.
Now add the add the supported languages to po/LINGUAS:
da nl
NOTE: These used to be in configure.{in,ac} in the ALL_LINGUAS variable. This is deprecated since gettext 0.11
9. Run
aclocal
to make sure that the necessary autoconf and automake macros are inserted in aclocal.m4
Run
autoconf
to create the configure script.
10. install the gettext.h file (since gettext 0.11) and include it:
#include "gettext.h"
#define _(String) gettext (String)
11. Now add the following to helloworld.c
#include <locale.h>
#include "gettext.h"
#define _(String) gettext (String)
/* includes used by original program here */
int main (void)
{
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
/* Original Helloworld code here */
}
If you use GNOME og GTK+ the setlocale sentence shouldn't be needed
We also substitute all strings we want to be translated with _("original string") to make sure that gettext is run on the strings. So the printf now looks like
printf (_("Hello, world!/n"));
12. We create src/Makefile.am (from which Makefile.in and Makefile will be generated)
INCLUDES = -I$(top_srcdir) -I$(includedir) /
-DLOCALEDIR=/""$(datadir)/locale"/"
bin_PROGRAMS = helloworld
helloworld_SOURCES = helloworld.c
noinst_HEADERS = i18n-support.h
13. Now we create the following toplevel Makefile.am
SUBDIRS = src po
EXTRA_DIST = intltool-extract.in intltool-merge.in intltool-update.in
14. Go into the directory po/ and create POTFILES.in
This file should contain a list of all the files in your distribution (starting from the top, one level above the po dir) that contain strings to be internationalized.
For the helloworld sample, it would contain
src/helloworld.c
Run
intltool-update --pot
Run
intltool-update --maintain
to see if you are missing files that contain marked strings. You should consider adding these to POTFILES.in
15. Now we start making a Danish and Dutch translation
msginit --locale=da
msginit --locale=nl
intltool-update da
intltool-update nl
edit and update da.po and nl.po (The respective translations are "Hej verden" and "Hallo wereld")
16. Now we can compile. We will test it later, so we will install it in
a temporary location.
Close your eyes and type
./configure --prefix=/tmp/helloworld && make
in the toplevel directory. :-)
17. To test if it works, you have to install the package.
Run
make install
in the toplevel directory.
18. Now set the environment variable LC_ALL to your preferred language :
export LC_ALL=nl_NL
/tmp/helloworld/bin/helloworld
export LC_ALL=da_DK
/tmp/helloworld/bin/helloworld
And if all goes well, the string should be translated in the two languages.
19. To finish it all up, run
make dist
to create a distributable tarball containing your internationalized program.
20. Exercises :
- add another language