Sugar-api-doc: Difference between revisions

From OLPC
Jump to navigation Jump to search
m (→‎Notes: link to a related doc)
Line 17: Line 17:
= MISCELLANEOUS =
= MISCELLANEOUS =
The tasks below are random useful techniques that have come up as I write code and documentation for this reference. They have yet to be categorized, but will be as a sufficient set of related entries are written.
The tasks below are random useful techniques that have come up as I write code and documentation for this reference. They have yet to be categorized, but will be as a sufficient set of related entries are written.

=== How do I write to a log in my activity code? ===
Sugar uses [http://www.onlamp.com/pub/a/python/2005/06/02/logging.html python's standard logging]. The following code shows how to output something at the debug log level.

import logging
_logger = logging.getLogger('annotate-activity')
...
_logger.debug('starting activity')


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

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


= Notes =
= Notes =

Revision as of 19:07, 16 June 2008

Sugar Almanac for Developers

Sugar Almanac Main Page

Package: sugar

sugar.env

sugar.profile

sugar.mime

Package: sugar.activity

sugar.activity.activity

sugar.activity.registry

Package: sugar.graphics

sugar.graphics.alert

sugar.graphics.toolbutton

sugar.graphics.toolbox

Package: sugar.datastore

sugar.datastore.datastore

Logging

sugar.logger

Notes on using Python Standard Logging in Sugar

Internationalization

Internationalization in Sugar

How do I get additional help beyond this almanac?

  • Looking to get started with the basics of sugar development? Check out Christoph Derndorfer's Activity Handbook.

Now, on to the actual almanac ...

Package: sugar.activity

Package: sugar.graphics

Package: sugar.datastore

MISCELLANEOUS

The tasks below are random useful techniques that have come up as I write code and documentation for this reference. They have yet to be categorized, but will be as a sufficient set of related entries are written.

How do I write to a log in my activity code?

Sugar uses python's standard logging. The following code shows how to output something at the debug log level.

   import logging
   _logger = logging.getLogger('annotate-activity')
   ...
        _logger.debug('starting activity')


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.

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

Notes

<references />