Internationalization in Sugar: Difference between revisions

From OLPC
Jump to navigation Jump to search
Line 1: Line 1:
== Broad Steps to Internationalize/Localize Your Code ==
== Broad Steps to Internationalize/Localize Your Code ==
=== Step 1: ===
=== Step 1: ===

=== Step 2: ===

=== How do I ensure that a text string is correctly translated to another language when appropriate (for internationalization)? ===


To ensure that string output from your activity is correctly translated, you would use the gettext utility. The code below imports gettext, renaming it as '_' for code brevity. Then, whenever there is a string that you want to make sure is translated based on language settings, you simply pass it to the _() function. According to the [http://docs.python.org/lib/node731.html Python Reference Library], gettext will "return the localized translation of message, based on the current global domain, language, and locale directory."
To ensure that string output from your activity is correctly translated, you would use the gettext utility. The code below imports gettext, renaming it as '_' for code brevity. Then, whenever there is a string that you want to make sure is translated based on language settings, you simply pass it to the _() function. According to the [http://docs.python.org/lib/node731.html Python Reference Library], gettext will "return the localized translation of message, based on the current global domain, language, and locale directory."


The code snippet below is part of a larger UI creation routine that creates a sugar.graphics.Notebook object and three pages for that notebook. Each page label should be appropriately translated.

<pre>
from gettext import gettext as _
from gettext import gettext as _
...
...
#Make sure 'hello world' translates
#Add the pages to the notebook.
print _('hello world')
top_container.add_page(_('First Page'), first_page)
top_container.add_page(_('Second Page'), second_page)
top_container.add_page(_('Third Page'), third_page)
</pre>

=== Step 2: ===






Revision as of 20:39, 17 July 2008

Broad Steps to Internationalize/Localize Your Code

Step 1:

To ensure that string output from your activity is correctly translated, you would use the gettext utility. The code below imports gettext, renaming it as '_' for code brevity. Then, whenever there is a string that you want to make sure is translated based on language settings, you simply pass it to the _() function. According to the Python Reference Library, gettext will "return the localized translation of message, based on the current global domain, language, and locale directory."

The code snippet below is part of a larger UI creation routine that creates a sugar.graphics.Notebook object and three pages for that notebook. Each page label should be appropriately translated.

    from gettext import gettext as _
    ...        
        #Add the pages to the notebook. 
        top_container.add_page(_('First Page'), first_page)
        top_container.add_page(_('Second Page'), second_page)
        top_container.add_page(_('Third Page'), third_page)

Step 2:

How do I ensure that using gettext does not crash my activity, especially when I try to translate more complex string substitution?

Since some strings require variables to be substituted into them, they need to be translated carefully. If they're not translated correctly, trying to do a string substitution can crash your activity.

The code below redefines the _() to use the gettext method only if gettext actually works. If there is an exception, the code will simply return the untranslated string. This ensures that instead of crashing, your activity will simply not translate your string.

#Do the import of the gettext, but do not give it the underscore alias
from gettext import gettext
...
#defensive method against variables not translated correctly
def _(s):
    #todo: permanent variable


    istrsTest = {}
    for i in range (0,4):
        istrsTest[str(i)] = str(i)

    #try to use gettext. If it fails, then just return the string untranslated. 
    try:
            #test translating the string with many replacements
            i = gettext(s)
            test = i % istrsTest
            print test
    except:
            #if it doesn't work, revert
            i = s
    return i
...
        #Now we can use the _() function and it should not crash if gettext fails.
        substitutionMap = {}
        substitutionMap[str(1)] = 'one'
        substitutionMap[str(2)] = 'two'
        substitutionMap[str(3)] = 'three'
        print _("Lets count to three: %(1)s, %(2)s, %(3)s") % substitutionMap

Additional Resources

Sugar Localization

Python i18n