GNOME uses GNU gettext to support locale. Here is how to use gettext in your program:
Set the configure.ac macros for gettext as described in Section 6.2.2.
Run gettextize and intltoolize in your source tree.
In your source code, enclose all target strings with _() , and all target string constants with N_() :
const gchar *str_array[] = { N_("egg"), N_("miracle"), N_("drill"), }; g_print("%s\n", _(str_array[i]));
Change to your po directory and create POTFILES.in , a list of all of the source file names that you changed (relative to your source tree root):
src/main.c src/aux.c
Run make prog.pot in your po directory to create a translation template that looks something like this:
# 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 <EMAIL@ADDRESS>, YEAR. # #, fuzzy msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2003-07-03 16:51-0700\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Language-Team: LANGUAGE <LL@li.org>\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=CHARSET\n" "Content-Transfer-Encoding: 8bit\n" #: src/main.c:7 msgid "egg" msgstr "" #: src/main.c:8 msgid "miracle" msgstr "" #: src/main.c:9 msgid "drill" msgstr "" #: src/main.c:15 msgid "strings:\n" msgstr ""
Edit prog.pot , changing the header lines. Do not change PO-Revision-Date and Last-Translator ; the translator should do this.
Copy prog.pot to locale.po , where locale is the target locale, and fill in the blanks. Here is de.po for the preceding example:
# Example package. # Copyright (C) 2003 J. Random Developer # This file is distributed under the same license as the example package. # J. Random Developer <jrd@example.com>, 2003. # #, fuzzy msgid "" msgstr "" "Project-Id-Version: Example 0.1\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2003-07-03 16:51-0700\n" "PO-Revision-Date: 2003-07-08 13:32+0100\n" "Last-Translator: J. Zufaelliger Uebersetzer <jzu@example.com>\n" "Language-Team: Example Translator Team <ett@example.com>\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" #: src/main.c:7 msgid "egg" msgstr "Ei" #: src/main.c:8 msgid "miracle" msgstr "Wunder" #: src/main.c:9 msgid "drill" msgstr "Bohrmaschine" #: src/main.c:15 msgid "strings:\n" msgstr "Zeichenketten:\n"
Make sure that you define the character set as UTF-8.
Rebuild your source tree; make install checks the available translations and puts them in their proper system directory, and the program is ready to use.
As a programmer, you only need to put _(...) and N_(...) into the strings in your code. If your package is popular enough, someone else will probably volunteer to maintain translations.
gettext supports only C source files, but you can use the same . po files for auxiliary data; for instance, you can use prog.desktop with intltool. Let's say that you want to create miracletext.desktop from Section 6.3:
Rename your miracletext.desktop file to miracletext.desktop.in .
Edit miracletext.desktop.in , adding an underscore to the beginning of each line that you want translated:
[Desktop Entry] Name=Example _Comment=MiracleText Word Processor _Name=Edit documents with MiracleText TryExec=example << ... >>
Add miracletext.desktop .in to your po/POTFILES.in file.
Add a set of intltool directives to your top-level Makefile.am . Section 7.3.6 contains an example for a schemas file, and a MIME keys example appears in Section 8.7.2.
Add a rule to remove miracletext.desktop for clean-local in Makefile.am .
Rebuild your source tree.