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 [[Translating GRAMPS|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 ''configure.in'' file into ''ALL_LINGUAS'' section.

+

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 [[Translating Gramps|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 ''configure.in'' file into ''ALL_LINGUAS'' section.

==How to allow translations==

==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.

+

Gramps is a fully-internationalized application with translations in many languages. All code which presents text to users must provide for that text to be translated. Fortunately, Gramps provides an extension of [http://docs.python.org/3/library/gettext.html gettext] which makes this fairly painless. First, alias the gettext function from the single localization instance:

−

from gen.ggettext import gettext as _

+

from gramps.gen.const import GRAMPS_LOCALE as glocale

−

+

_ = glocale.translation.gettext

−

This statement imports the <code>sgettext</code> function under the name of <code>_</code>. 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.

+

This statement imports the <code>gettext</code> function and aliases it as <code>_</code>. The translation tools treat strings wrapped in _() as translatable and assemble them into catalogs for the translators to work with; by aliasing it to gettext(), we also enable python to retrieve the translation appropriate for the user's locale.

Example 1:

Example 1:

Line 24:

Line 24:

print _("Hello world!")

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.

+

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.

+

+

=== More complicated translations ===

+

+

In addition to <tt>gettext</tt>, GrampsTranslation offers two more specialized retrieval functions, <tt>ngettext</tt> and <tt>sgettext</tt>.

+

+

In some strings, it's necessary to specify different translations depending upon the number of an argument. For example,

We're making sure that the translators know that this message id means "what's left" rather than "take a nap". When the file is translated, this is no problem, because the translation doesn't include the hint -- but if the user is working in English, we don't want him to see the hint, so we need to alias _ to sgettext:

+

_ = glocale.translation.sgettext

+

+

Often you need to combine them. While <tt>ngettext</tt> and <tt>sgettext</tt> can each handle plain strings, neither can handle the other's strings. Fortunately the <tt>intltool</tt> message extractor is pretty stupid, so any function name that ends in either <tt>_</tt> or <tt>gettext</tt> will work. This will work pretty well:

+

+

_ = glocale.translation.gettext

+

N_ = glocale.translation.ngettext

+

S_ = glocale.translation.sgettext

+

+

Obviously you would pass the translatable string to the right function.

+

+

=== Encoding ===

+

String handling can be a bit tricky in a localized environment. Gramps's translation facility will always return Unicode-encoded strings. For as long as it is necessary to support both Python2 and Python3, developers will need to understand Unicode string handling in both versions of the language.

−

All strings meant

+

If you use non ASCII characters in a string literal the string must be Unicode.

−

for the user should be always be preceeded with the _ function.

+

−

If you use non ASCII characters in a string, that shall be translated, the string must be unicode.

+

'''Don't do this:'''

−

Example:

+

print _(u"Eg, valid values are 12.0154, 50° 52′ 21.92″N")

print _(u"Eg, valid values are 12.0154, 50° 52′ 21.92″N")

+

Because the <tt>u</tt> prefix was removed for Python 3.0-3.2. (It was restored in 3.3 for compatibility with 2.7, but it's not necessary.)

+

Instead, put in the first line of the module

+

# *-* coding: utf-u *-*

+

then in the imports section

+

from __future__ import unicode_literals

+

which makes all of the literals unicode. Make sure that your editor is set up to save utf-8!

−

===Into glade file===

+

===Glade files===

Just enable the translatable attribute on an XML element.

Just enable the translatable attribute on an XML element.

Line 49:

Line 82:

Eg, valid values are 12.0154, 50<code>&amp;</code>#xB0; 52' 21.92"N

Eg, valid values are 12.0154, 50<code>&amp;</code>#xB0; 52' 21.92"N

−

use in stead:

+

use instead:

Eg, valid values are 12.0154, 50° 52′ 21.92″N

Eg, valid values are 12.0154, 50° 52′ 21.92″N

−

In this case note the special characters for deg, min, sec.

+

In this case note the unicode characters for deg, min, sec. '''Ensure that your editor is set up to encode the characters in UTF-8!'''

====Accessibility====

====Accessibility====

+

+

In addition to [http://developer.gnome.org/gtk/2.24/GtkWidget.html#id1294298 accelerators], ''[http://developer.gnome.org/gtk/2.24/GtkWidget.html GtkWidget]'' also support a custom <accessible> element, which supports actions and relations. Properties on the accessible implementation of an object can be set by accessing the internal child "accessible" of a ''[http://developer.gnome.org/gtk/2.24/GtkWidget.html GtkWidget]''. See [http://developer.gnome.org/gtk/2.24/GtkWidget.html#GtkWidget-BUILDER-UI GtkBuilder UI].

''A [http://developer.gnome.org/gtk/2.24/GtkLabel.html GtkLabel]'' '''with mnemonic support''' will automaticaly generate accessibility keys on linked ''[http://developer.gnome.org/gtk/2.24/GtkEntry.html GtkEntry]'' and ''UndoableEntry'' fields. Remember that Gramps also uses custom widgets like ''StyledTextEditor'' and ''ValidatableMaskedEntry'', which do not always have relation with a ''GtkLabel''.

−

Remember that Gramps also uses custom widgets like ''StyledTextEditor'' and ''ValidatableMaskedEntry'', which do not provide relation with a ''GtkLabel''.

+

* Toggle buttons and Icons on toolbar

* Toggle buttons and Icons on toolbar

−

Gramps often uses toggle buttons and alone image (no label), this excludes blind people and generates a poor interface for accessibility.

+

Gramps often uses ''[http://developer.gnome.org/gtk/2.24/GtkToggleButton.html GtkToggleButtons]'' and alone ''[http://developer.gnome.org/gtk/2.24/GtkImage.html GtkImage]'' (image without label), this excludes blind people and generates a poor interface for accessibility.

See [[Accessibility]].

See [[Accessibility]].

−

===Into addons plugins===

+

===Addons===

−

from TransUtils import get_addon_translator

+

External addons often need to provide their own message catalogs. To pull one in, use

−

_ = get_addon_translator().gettext

+

this instead of the usual.

+

from gramps.gen.const import import GRAMPS_LOCALE as glocale

+

_ = glocale.get_addon_translator(__file__).gettext

+

or if you need more than one retrieval function:

+

_translation = glocale.get_addon_translator(__file__)

+

_ = _translation.gettext

+

S_ = _translation.sgettext

−

See [[Addons_Development#Localization|Addon developpement]].

+

The addon translator is another instance of GrampsTranslation, so the rules for creating

+

translatable strings and for retrieving the translated values are the same as for internal modules.

+

+

See [[Addons_Development#Localization|Addons development]] for more details.

Gramps has used different environments according to versions for retrieving strings to translate:

−

intltool-merge -d po/ data/gramps.desktop.in data/gramps.desktop

+

−

# Merges translated strings into xml file, ''root'' directory :

+

* [[Translation_environment20|2.0.x]]

−

intltool-merge -x po/ data/gramps.xml.in data/gramps.xml

+

* [[Translation_environment22|2.2.x to Gramps 3.4.x]]

+

* [[Translation_environment4|Trunk]]

−

# Merges translated strings into key file, ''root'' directory :

+

There are two stages to getting a translation to work.

−

intltool-merge -k po/ data/gramps.keys.in data/gramps.keys

+

===Files and directory===

===Files and directory===

−

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

+

Translations are stored in a <code>.po</code> file that contains the mappings between the original strings and the translated strings, see [[Translating Gramps]].

Translators use a generic file <code>gramps.pot</code> to generate their <code>.po</code> file.

Translators use a generic file <code>gramps.pot</code> to generate their <code>.po</code> file.

−

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

+

Gramps uses a utility that extracts the strings from the source code to build the <code>.po</code> file. This utility examines the source files for strings that have been marked as translatable. In the python source, these are the strings enclosed in the <code>_()</code> function calls.

−

+

−

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

+

−

+

−

...

+

−

# plugins directory

+

−

src/plugins/AncestorChart2.py

+

−

src/plugins/AncestorReport.py

+

−

...

+

−

src/plugins/FindDupes.py

+

−

src/plugins/Leak.py

+

−

src/plugins/MediaManager.py

+

−

src/plugins/Myreport.py # <------

+

−

src/plugins/NarrativeWeb.py

+

−

src/plugins/PatchNames.py

+

−

...

+

−

+

−

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 <code>_()</code> call. In the following example, the extraction script will not extract the string.

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

Line 130:

Line 147:

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

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

−

You can check missing references (not on <code>POTFILES.in</code> and <code>POTFILES.skip</code>) with the command

+

===Add the reference to the file===

−

/intltool-update -m

+

−

into <code>/po</code> directory.

+

We need to also add a reference to this file for generating the translation template.

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.).

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===

+

===Use named %s/%d 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:

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')

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

Line 148:

Line 168:

'city' : 'Toronto', 'male_name' : 'Joe'}

'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.

+

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:

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

print "%(male_name)s was born in %(city)s" % dict(

print "%(male_name)s was born in %(city)s" % dict(

city = 'Toronto', male_name = 'Joe')

city = 'Toronto', male_name = 'Joe')

+

+

Some languages are using right-to-left text direction. It is important to use named arguments when there is more than one %s/%d value into a translation string.

===Provide separate strings for masculine and feminine.===

===Provide separate strings for masculine and feminine.===

Line 170:

Line 192:

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.

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 [[Translating_GRAMPS#Plural_forms|plural forms]] support, useful for locales with multiples plurals according to a number (''often slavic based languages'') or for Asian family languages (''singular = plural'').

+

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

Note, some locales need singular form with [http://en.wikipedia.org/wiki/Plural#Zero zero] and plural form might be also used in this case.

Note, some locales need singular form with [http://en.wikipedia.org/wiki/Plural#Zero zero] and plural form might be also used in this case.

Line 191:

Line 213:

We need to call module :

We need to call module :

−

from TransUtils import sgettext as _

+

from gen.ggettext import sgettext as _

or

or

−

from TransUtils import sngettext as _

+

from gen.ggettext import sngettext as _

−

(if you use ngettext)

+

(if you use ngettext) # not implemented

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

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

Line 218:

Line 240:

See ''the person'' details # or See ''the family, the event, etc...'' details

See ''the person'' details # or See ''the family, the event, etc...'' details

Make ''the person'' active

Make ''the person'' active

+

+

===Genitive form===

+

+

Genitive (and some other) forms need to modify the name itself into some locales, like Finnish or Swedish.

+

+

Instead of "free form" text that talks about

+

e.g.

+

son '''of %s'''

+

better would be for example some tabulated format like this:

+

son: %s

+

daughter: %s

+

which doesn't require genitive.

==Textual reports==

==Textual reports==

Revision as of 09:37, 4 May 2013

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

Introduction

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 configure.in file into ALL_LINGUAS section.

How to allow translations

Gramps is a fully-internationalized application with translations in many languages. All code which presents text to users must provide for that text to be translated. Fortunately, Gramps provides an extension of gettext which makes this fairly painless. First, alias the gettext function from the single localization instance:

This statement imports the gettext function and aliases it as _. The translation tools treat strings wrapped in _() as translatable and assemble them into catalogs for the translators to work with; by aliasing it to gettext(), we also enable python to retrieve the translation appropriate for the user's locale.

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.

More complicated translations

In addition to gettext, GrampsTranslation offers two more specialized retrieval functions, ngettext and sgettext.

In some strings, it's necessary to specify different translations depending upon the number of an argument. For example,

George Smith and Annie Jones have 1 child
George Smith and Annie Jones have 3 children

We're making sure that the translators know that this message id means "what's left" rather than "take a nap". When the file is translated, this is no problem, because the translation doesn't include the hint -- but if the user is working in English, we don't want him to see the hint, so we need to alias _ to sgettext:

_ = glocale.translation.sgettext

Often you need to combine them. While ngettext and sgettext can each handle plain strings, neither can handle the other's strings. Fortunately the intltool message extractor is pretty stupid, so any function name that ends in either _ or gettext will work. This will work pretty well:

Obviously you would pass the translatable string to the right function.

Encoding

String handling can be a bit tricky in a localized environment. Gramps's translation facility will always return Unicode-encoded strings. For as long as it is necessary to support both Python2 and Python3, developers will need to understand Unicode string handling in both versions of the language.

If you use non ASCII characters in a string literal the string must be Unicode.

Don't do this:

print _(u"Eg, valid values are 12.0154, 50° 52′ 21.92″N")

Because the u prefix was removed for Python 3.0-3.2. (It was restored in 3.3 for compatibility with 2.7, but it's not necessary.)
Instead, put in the first line of the module

# *-* coding: utf-u *-*

then in the imports section

from __future__ import unicode_literals

which makes all of the literals unicode. Make sure that your editor is set up to save utf-8!

Non ASCII characters

If you plan to use non ASCII characters in a string, that shall be translated,
do not use escape sequences:

Eg, valid values are 12.0154, 50&#xB0; 52' 21.92"N

use instead:

Eg, valid values are 12.0154, 50° 52′ 21.92″N

In this case note the unicode characters for deg, min, sec. Ensure that your editor is set up to encode the characters in UTF-8!

Accessibility

In addition to accelerators, GtkWidget also support a custom <accessible> element, which supports actions and relations. Properties on the accessible implementation of an object can be set by accessing the internal child "accessible" of a GtkWidget. See GtkBuilder UI.

Gtk label

A GtkLabelwith mnemonic support will automaticaly generate accessibility keys on linked GtkEntry and UndoableEntry fields. Remember that Gramps also uses custom widgets like StyledTextEditor and ValidatableMaskedEntry, which do not always have relation with a GtkLabel.

Toggle buttons and Icons on toolbar

Gramps often uses GtkToggleButtons and alone GtkImage (image without label), this excludes blind people and generates a poor interface for accessibility.

Files and directory

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 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.

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.

Add the reference to the file

We need to also add a reference to this file for generating the translation template.

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/%d 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:

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:

Some languages are using right-to-left text direction. It is important to use named arguments when there is more than one %s/%d value into a translation string.

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.

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).

Note, some locales need singular form with zero and plural form might be also used in this case.

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 gen.ggettext import sgettext as _

or

from gen.ggettext import sngettext as _

(if you use ngettext) # not implemented

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

_("context|string")

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.

Object classes

Gramps often displays names of primary objects (Person, Family, Event, etc ...), for being consistent on displayed strings (also in english!), there is a trans_objclass(objclass_str) function on TransUtils module.

So, when we need to display the primary object name in lower case into a sentence, we can use this function.

ex:

from gen.ggettext import sgettext as _
from TransUtils import trans_objclass