Development Team/Almanac/Internationalization: Difference between revisions

Line 2: Line 2:
=== Step 1: ===
=== Step 1: ===


=== Step 2: ===
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.


=== How do I ensure that a text string is correctly translated to another language when appropriate (for internationalization)? ===
<pre>
    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)
</pre>


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."
=== Step 2: ===


    from gettext import gettext as _
    ...
        #Make sure 'hello world' translates
        print _('hello world')