Preparar su proyecto

In the instructions below we will assume that you will not be using gettext directly, but intltool, which was written specifically for GNOME. intltool uses gettext(), which extracts strings from source code, but intltool can also combine strings from other files, for example from desktop menu details, and GUI resource files such as Glade files, into standard gettext .pot/.po files.

We also assume that you are using autotools (e.g. automake and autoconf) to build your project, and that you are using https://gitlab.gnome.org/GNOME/gnome-common/blob/master/autogen.sh or a similar autogen.sh file, which, among other things, takes care of some intltool initialization.

An alternative to gnome-common's autogen.sh may look like this:

#! /bin/sh -e
test -n "$srcdir" || srcdir=`dirname "$0"`
test -n "$srcdir" || srcdir=.

autoreconf --force --install --verbose --warnings=all "$srcdir"
echo "Running intltoolize --copy --force --automake"
intltoolize --copy --force --automake
test -n "$NOCONFIGURE" || "$srcdir/configure" "$@"

Cree una subcarpeta llamada po en la carpeta raíz de su proyecto. Esta carpeta eventualmente contendrá todas sus traducciones. Dentro de ella, cree un archivo llamado LINGUAS y otro llamado POTFILES.in. Es práctica común crear también un archivo ChangeLog en la carpeta po para que los traductores puedan rastrear los cambios de las traducciones.

LINGUAS contiene una lista ordenada alfabéticamente de códigos que identifican los idiomas a los que se traduce su programa (las líneas de comentarios que comienzan con # se ignoran). Cada código de idioma listado en el archivo LINGUAS debe tener un archivo .po correspondiente. Entonces, si su programa estuviera traducido al alemán y al japonés, su archivo LINGUAS se vería así:

# keep this file sorted alphabetically, one language code per line
de
ja

(Además, tendría los archivos ja.po y de.po en su carpeta po que contendrían las traducciones al alemán y al japonés, respectivamente).

POTFILES.in es una lista de rutas a todos los archivos que contienen cadenas marcadas para traducción, comenzando desde la carpeta raíz del proyecto. Entonces, por ejemplo, si el código fuente de su proyecto estuviera en una subcarpeta llamda src, y si tuviera dos archivos conteniendo cadenas que deben traducirse, su archivo POTFILES.in se vería así:

src/main.cc
src/other.cc

Si está usando gettext directamente, sólo puede marcar cadenas para traducir si están en el archivo de código fuente. Sin embargo, si usa intltool, puede marcar cadenas para traducir en una variedad de otros formatos de archivo, incluyendo archivos de IU de Glade, xml, archivos .desktop y muchos más. Por lo tanto, si ha diseñado parte de la IU de la aplicación en Glade, entonces también añada sus archivos .glade a la lista en POTFILES.in.

Ahora que hay un lugar en el que poner sus traducciones, necesita inicializar intltool y gettext. Añádale el siguiente código a su configure.ac, substituyendo «programname» con el nombre de su programa:

IT_PROG_INTLTOOL([0.35.0])

GETTEXT_PACKAGE=programname
AC_SUBST(GETTEXT_PACKAGE)
AC_DEFINE_UNQUOTED([GETTEXT_PACKAGE], ["$GETTEXT_PACKAGE"],
                   [The domain to use with gettext])
AM_GNU_GETTEXT([external])
AM_GNU_GETTEXT_VERSION([0.17])

PROGRAMNAME_LOCALEDIR=[${datadir}/locale]
AC_SUBST(PROGRAMNAME_LOCALEDIR)

Esta variable PROGRAMNAME_LOCALEDIR se usará luego en el archivo Makefile.am, para definir una macro que se usará cuando inicialice gettext en su código fuente.

AM_GLIB_GNU_GETTEXT has been an alternative to AM_GNU_GETTEXT and AM_GNU_GETTEXT_VERSION, but AM_GLIB_GNU_GETTEXT is now deprecated, and shall not be used in new code.

In the top-level Makefile.am:

  • Add po to the SUBDIRS variable. Without this, your translations won't get built and installed when you build the program
  • Define INTLTOOL_FILES as:
    INTLTOOL_FILES = intltool-extract.in \
                     intltool-merge.in \
                     intltool-update.in
  • Add INTLTOOL_FILES to the EXTRA_DIST list of files. This ensures that when you do a make dist, these files will be included in the source tarball.
  • Update your DISTCLEANFILES:
    DISTCLEANFILES = ... intltool-extract \
                     intltool-merge \
                     intltool-update \
                     po/.intltool-merge-cache
  • Depending on the types of files that contain translatable strings, add code such as
    desktopdir = $(datadir)/applications
    desktop_in_files = programname.desktop.in
    desktop_DATA = $(desktop_in_files:.desktop.in=.desktop)
    @INTLTOOL_DESKTOP_RULE@

En su src/Makefile.am, actualice su AM_CPPFLAGS para añadir la siguiente definición de macro de preprocesador:

AM_CPPFLAGS = ... -DPROGRAMNAME_LOCALEDIR=\"${PROGRAMNAME_LOCALEDIR}\"

Esta macro se usará cuando inicialice gettext en su código fuente.