Barry Warsaw | 9e03592 | 2000-08-25 19:14:25 +0000 | [diff] [blame^] | 1 | import os |
| 2 | import gettext |
| 3 | |
| 4 | def get_qualified_path(name): |
| 5 | """Return a more qualified path to name""" |
| 6 | import sys |
| 7 | import os |
| 8 | path = sys.path |
| 9 | try: |
| 10 | path = [os.path.dirname(__file__)] + path |
| 11 | except NameError: |
| 12 | pass |
| 13 | for dir in path: |
| 14 | fullname = os.path.join(dir, name) |
| 15 | if os.path.exists(fullname): |
| 16 | return fullname |
| 17 | return name |
| 18 | |
| 19 | # Test basic interface |
| 20 | os.environ['LANGUAGE'] = 'xx' |
| 21 | |
| 22 | mofile = get_qualified_path('xx') |
| 23 | localedir = os.path.dirname(mofile) |
| 24 | |
| 25 | print 'installing gettext' |
| 26 | gettext.install() |
| 27 | |
| 28 | print _('calling bindtextdomain with localedir %s') % localedir |
| 29 | print gettext.bindtextdomain('gettext', localedir) |
| 30 | print gettext.bindtextdomain() |
| 31 | |
| 32 | print gettext.textdomain('gettext') |
| 33 | print gettext.textdomain() |
| 34 | |
| 35 | # test some translations |
| 36 | print _(u'mullusk') |
| 37 | print _(r'Raymond Luxury Yach-t') |
| 38 | print _(ur'nudge nudge') |
| 39 | |
| 40 | # double quotes |
| 41 | print _(u"mullusk") |
| 42 | print _(r"Raymond Luxury Yach-t") |
| 43 | print _(ur"nudge nudge") |
| 44 | |
| 45 | # triple single quotes |
| 46 | print _(u'''mullusk''') |
| 47 | print _(r'''Raymond Luxury Yach-t''') |
| 48 | print _(ur'''nudge nudge''') |
| 49 | |
| 50 | # triple double quotes |
| 51 | print _(u"""mullusk""") |
| 52 | print _(r"""Raymond Luxury Yach-t""") |
| 53 | print _(ur"""nudge nudge""") |
| 54 | |
| 55 | # multiline strings |
| 56 | print _('''This module provides internationalization and localization |
| 57 | support for your Python programs by providing an interface to the GNU |
| 58 | gettext message catalog library.''') |
| 59 | |
| 60 | print gettext.dgettext('gettext', 'nudge nudge') |
| 61 | |
| 62 | # dcgettext |
| 63 | ##import locale |
| 64 | ##if gettext.dcgettext('gettext', 'nudge nudge', |
| 65 | ## locale.LC_MESSAGES) <> 'wink wink': |
| 66 | ## print _('dcgettext failed') |
| 67 | |
| 68 | # test the alternative interface |
| 69 | fp = open(os.path.join(mofile, 'LC_MESSAGES', 'gettext.mo'), 'rb') |
| 70 | t = gettext.GNUTranslations(fp) |
| 71 | fp.close() |
| 72 | |
| 73 | gettext.set(t) |
| 74 | print t == gettext.get() |
| 75 | |
| 76 | print _('nudge nudge') |