Marcar cadenas para traducir

Las cadenas literales deben introducirse en el código fuente en inglés, pero deben rodearse por una llamada a la función gettext(). Estas cadenas se extraerán para traducción y las traducciones podrán usarse en tiempo de ejecución en lugar de las cadenas originales en inglés.

El paquete GNU gettext le permite marcar cadenas en el código fuente, extraer esas cadenas para su traducción, y usar las cadenas traducidas en su aplicación.

However, Glib defines gettext() support macros which are shorter wrappers in an easy-to-use form. To use these macros, include <glibmm/i18n.h>, and then, for example, substitute:

display_message("Getting ready for i18n.");
with:
display_message(_("Getting ready for i18n."));

For reference, it is possible to generate a file which contains all strings which appear in your code, even if they are not marked for translation, together with file name and line number references. To generate such a file named my-strings, execute the following command, within the source code directory:

xgettext -a -o my-strings --omit-header *.cc *.h

Finally, to let your program use the translation for the current locale, add this code to the beginning of your main.cc file, to initialize gettext.

bindtextdomain(GETTEXT_PACKAGE, PROGRAMNAME_LOCALEDIR);
bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
textdomain(GETTEXT_PACKAGE);

25.2.1. Cómo funciona gettext

The intltool-update or xgettext script extracts the strings and puts them in a mypackage.pot file. The translators of your application create their translations by first copying this .pot file to a localename.po file. A locale identifies a language and an encoding for that language, including date and numerical formats. Later, when the text in your source code has changed, the msgmerge or intltool-update script is used to update the localename.po files from the regenerated .pot file.

En tiempo de instalación, los archivos .po se convierten a formato binario (con la extensión .mo) y se ponen en una carpeta de sistema para archivos de localización, por ejemplo /usr/share/locale/.

Cuando la aplicación se ejecuta, la biblioteca gettext verifica la carpeta de sistema para comprobar si hay un archivo .mo para el entorno de localización del usuario (puede establecer la localización, por ejemplo, con «export LANG=de_DE.UTF-8» desde una consola bash). Más tarde, cuando el programa alcance una llamada a gettext, buscará la traducción de la cadena particular. Si no encuentra ninguna, usará la cadena original.

25.2.2. Comprobar y añadir las traducciones

To convince yourself that you've done well, you may wish to add a translation for a new locale. In order to do that, go to the po subdirectory of your project and execute the following command:

intltool-update --pot

Eso creará un archivo llamado programname.pot. Ahora, copie ese archivo a languagecode.po, como por ejemplo de.po o hu.po. También añada ese código de lenguaje a LINGUAS. El archivo .po contiene una cabecera y una lista de cadenas en inglés, con espacios para introducir las cadenas traducidas. Asegúrese de establecer la codificación del archivo .po (especificada en la cabecera, pero también en el contenido) a UTF-8.

Es posible que ciertas cadenas se marquen como fuzzy en el archivo .po. Estas traducciones no sustituirán la cadena original. Para hacerlas aparecer, simplemente elimine la etiqueta fuzzy.

25.2.3. Recursos

More information about what lies behind the internationalization and localization process is presented and demonstrated in: