summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_gettext.py
blob: 1760c7816d51c6da88e56613cbb4a111cc5ee2ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
import gettext

def get_qualified_path(name):
    """Return a more qualified path to name"""
    import sys
    import os
    path = sys.path
    try:
        path = [os.path.dirname(__file__)] + path
    except NameError:
        pass
    for dir in path:
        fullname = os.path.join(dir, name)
        if os.path.exists(fullname):
            return fullname
    return name

# Test basic interface
os.environ['LANGUAGE'] = 'xx'

mofile = get_qualified_path('xx')
localedir = os.path.dirname(mofile)

print 'installing gettext'
gettext.install()

print _('calling bindtextdomain with localedir %s') % localedir
print gettext.bindtextdomain('gettext', localedir)
print gettext.bindtextdomain()

print gettext.textdomain('gettext')
print gettext.textdomain()

# test some translations
print _(u'mullusk')
print _(r'Raymond Luxury Yach-t')
print _(ur'nudge nudge')

# double quotes
print _(u"mullusk")
print _(r"Raymond Luxury Yach-t")
print _(ur"nudge nudge")

# triple single quotes
print _(u'''mullusk''')
print _(r'''Raymond Luxury Yach-t''')
print _(ur'''nudge nudge''')

# triple double quotes
print _(u"""mullusk""")
print _(r"""Raymond Luxury Yach-t""")
print _(ur"""nudge nudge""")

# multiline strings
print _('''This module provides internationalization and localization
support for your Python programs by providing an interface to the GNU
gettext message catalog library.''')

print gettext.dgettext('gettext', 'nudge nudge')

# dcgettext
##import locale
##if gettext.dcgettext('gettext', 'nudge nudge',
##                     locale.LC_MESSAGES) <> 'wink wink':
##    print _('dcgettext failed')

# test the alternative interface
fp = open(os.path.join(mofile, 'LC_MESSAGES', 'gettext.mo'), 'rb')
t = gettext.GNUTranslations(fp)
fp.close()

gettext.set(t)
print t == gettext.get()

print _('nudge nudge')