Coding for translation

From Gramps
Revision as of 18:53, 14 March 2011 by PaulFranklin (talk | contribs)
Jump to: navigation, search

Coding guidelines to enable easy and correct translation of strings on the User Interface.


GRAMPS has always been internationalized (see Therefore, all strings meant for the user should always be flagged for translation.

In order to be considered for inclusion in the offical GRAMPS release, any piece of code must support internationalization. What this means is that the Python module must support translations into different languages. GRAMPS provides support to make this as easy as possible for the developer. For enabling, a language code must be set on file into ALL_LINGUAS section.

How to allow translations

GRAMPS provides a simple interface (based on the gettext interface) to mark strings as being translatable. First, import the gettext function from the intl library.

from gen.ggettext import gettext as _

This statement imports the sgettext function under the name of _. This is the function that both marks the strings for translation and performs the actual translation at runtime. Strings that should be translated should be enclosed as an argument to the function.

Example 1:

print "Hello world!"

In this example, the string will always be printed as specified.

Example 1 internationalized:

print _("Hello world!")

In this example, GRAMPS will attempt to translate the string. If a translation exists, the call to the function will return the translation. If a translation does not exist, the original string is returned.

All strings meant for the user should be always be preceeded with the _ function.

Into glade file

Just enable the translatable attribute on an XML element.

<property name="label" translatable="yes">_Family:</property>
<property name="tooltip" translatable="yes">Abandon changes and close window</property>
<property name="label" translatable="no"><b> - </b> </property>

Into addons plugins

from TransUtils import get_addon_translator
_ = get_addon_translator().gettext

See Addon developpement.

How it works

GNU gettext and Gnome provide utilities and a translation framework (previously build_po and get_strings):

  • msginit will generate a standard gettext header.
  • intltool-update will manage template and translations.
  • intltool-extract will extract translation strings on .glade and .xml files, by generating files with .h extension.
# Generates a new template (gramps.pot), into /po directory :
intltool-update -p
  • intltool-merge will merge cached translations into .in files
# Merges translated strings into desktop file, root directory :
intltool-merge -d po/ data/ data/gramps.desktop
# Merges translated strings into xml file, root directory :
intltool-merge -x po/ data/ data/gramps.xml
# Merges translated strings into key file, root directory :
intltool-merge -k po/ data/ data/gramps.keys 

Files and directory

There are two stages to getting a translation to work. Translations are stored in a .po file that contains the mappings between the original strings and the translated strings, see Translating GRAMPS.

Translators use a generic file gramps.pot to generate their .po file. GRAMPS uses a utility that extracts the strings from the source code to build the .po file. This utility (a perl script called by the command make) examines the source files for strings that have been marked as translatable. In the python source, these are the strings enclosed in the _() function calls.

If you want this script to take your translatable strings into account, you must add your source file path in the file : po/ For this report example, you should add:

# plugins directory
src/plugins/                # <------

In this file, the sources are sorted within each directory or category.

Note that because strings are extracted by a script from the source file, string constants and not variables must be enclosed in the _() call. In the following example, the extraction script will not extract the string.

mystring = "Hello World!"
print _(mystring)

The correct method would be to use one of the following:

mystring = _("Hello World!")
print mystring

At run time, the _() calls will translate the string by looking it up in the translation database (created from the .po files) and returning the translated string.

You can check missing references (not on and POTFILES.skip) with the command

/intltool-update -m 

into /po directory.

Tips for writing a translatable Python module

Use complete sentences

Don't build up a sentence from phrases. Because a sentence is ordered in a particular way in your language does not mean that it is ordered the same way in another. Providing the entire sentence as a single unit allows the translator to make a meaningful translation. Do not concatenate phrases or terms as they will then show up as separate phrases or terms to be translated and the complete sentence may then show up incorrectly, especially in right-to-left languages (Arabic, Hebrew, etc.).

Use named %s values

Python provides a powerful mechanism that allows the reordering of %s values in a string. A translator may need to rearrange the structure of a sentence, and it may not match the order you chose. For example:

print "%s was born in %s" % ('Joe','Toronto')

In some languages it may make more sense to say:

print "%s is the city in which %s was born" % ('Toronto', 'Joe')

The problem is that this requires a change to the order of the arguments. Python provides a solution for this. By using named operators and dictionaries, we can say:

print "%(male_name)s was born in %(city)s" % {
           'city' : 'Toronto', 'male_name' : 'Joe'}

In this case, the order of the %s formatters is not important, since the values will be looked up in the dictionary at run time to resolve the value. The translator can reorder the %s formatters, or even remove them without causing any problems.

Note that Python also allows a variation which some people find easier to read:

print "%(male_name)s was born in %(city)s" % dict(
           city = 'Toronto', male_name = 'Joe')

Provide separate strings for masculine and feminine.

Many languages have the concept of gender, while others don't. A sentence may need to be phrased differently depending on whether the subject is male or female. By using the named %s values along with a bit of code, this problem can be solved.

if person.getGender() == Person.male:
       print _("%(male_name)s was born in %(city)s\n") % {
               'male_name' : name, 'city' : city }
       print _("%(female_name)s was born in %(city)s\n") % {
               'female_name' : name, 'city' : city }

This allows languages with gender differences to map nicely into your sentence.

Provide support for plural forms.

Plurals are handled differently in various languages. Whilst English or German have a singular and a plural form, other languages like Turkish don't distinguish between plural or singular and there are languages which use different plurals for different numbers, e.g. Polish.

Gramps provides a plural forms support, useful for locales with multiples plurals according to a number (often slavic based languages) or for Asian family languages (singular = plural).

We need to call module :

from gen.ggettext import ngettext

and code like this :

 ngettext("singular %d", "plural %d", n) %n


msg = ngettext('Import Complete: %d second',
               'Import Complete: %d seconds', t ) % t

Provide a context support.

A translator needs context for a good translation. Keep in mind you can help him/her, by using context on translation string.

We need to call module :

from TransUtils import sgettext as _


from TransUtils import sngettext as _

(if you use ngettext)

Translation string will use context, but this will be hidden on user interface.


Translator will see the translation string and a help string without loading program. Program will only display the string in English or with another locale.

Textual reports

Since Gramps-3.2 we are able to select the language for textual reports, see this feature.

Currently only available on Ancestor report.

For providing this option:

  1. import EnumeratedListOption
  2. import libtranslate
from import EnumeratedListOption 
import TransUtils
from libtranslate import Translator, get_language_string

Sample of code:

language = menu.get_option_by_name('trans').get_value()
       translator = Translator(language)
       self._ = translator.gettext
       self.__narrator = Narrator(self.database, self.verbose, use_call, 
                                   empty_date, empty_place, 
       self.__get_date = translator.get_date