home *** CD-ROM | disk | FTP | other *** search
/ Komputer for Alle 2004 #2 / K-CD-2-2004.ISO / OpenOffice Sv / f_0397 / python-core-2.2.2 / lib / test / test_gettext.py < prev    next >
Encoding:
Python Source  |  2003-07-18  |  5.8 KB  |  201 lines

  1. import os
  2. import base64
  3. import gettext
  4.  
  5.  
  6. def test_api_1(localedir, mofile):
  7.     print 'test api 1'
  8.  
  9.     # Test basic interface
  10.     os.environ['LANGUAGE'] = 'xx'
  11.  
  12.     print 'installing gettext'
  13.     gettext.install('gettext', localedir)
  14.  
  15.     # test some translations
  16.     print _('albatross')
  17.     print _(u'mullusk')
  18.     print _(r'Raymond Luxury Yach-t')
  19.     print _(ur'nudge nudge')
  20.  
  21.     # double quotes
  22.     print _("albatross")
  23.     print _(u"mullusk")
  24.     print _(r"Raymond Luxury Yach-t")
  25.     print _(ur"nudge nudge")
  26.  
  27.     # triple single quotes
  28.     print _('''albatross''')
  29.     print _(u'''mullusk''')
  30.     print _(r'''Raymond Luxury Yach-t''')
  31.     print _(ur'''nudge nudge''')
  32.  
  33.     # triple double quotes
  34.     print _("""albatross""")
  35.     print _(u"""mullusk""")
  36.     print _(r"""Raymond Luxury Yach-t""")
  37.     print _(ur"""nudge nudge""")
  38.  
  39.     # multiline strings
  40.     print _('''This module provides internationalization and localization
  41. support for your Python programs by providing an interface to the GNU
  42. gettext message catalog library.''')
  43.  
  44.     # test the alternative interface
  45.     fp = open(os.path.join(mofile), 'rb')
  46.     t = gettext.GNUTranslations(fp)
  47.     fp.close()
  48.  
  49.     t.install()
  50.  
  51.     print _('nudge nudge')
  52.  
  53.     # try unicode return type
  54.     t.install(unicode=1)
  55.  
  56.     print _('mullusk')
  57.  
  58.  
  59.  
  60. def test_api_2(localedir, mofile):
  61.     print 'test api 2'
  62.  
  63.     gettext.bindtextdomain('gettext', localedir)
  64.     print gettext.bindtextdomain('gettext') == localedir
  65.  
  66.     gettext.textdomain('gettext')
  67.     # should return 'gettext'
  68.     print gettext.textdomain()
  69.  
  70.     # local function override builtin
  71.     _ = gettext.gettext
  72.  
  73.     # test some translations
  74.     print _('albatross')
  75.     print _(u'mullusk')
  76.     print _(r'Raymond Luxury Yach-t')
  77.     print _(ur'nudge nudge')
  78.  
  79.     # double quotes
  80.     print _("albatross")
  81.     print _(u"mullusk")
  82.     print _(r"Raymond Luxury Yach-t")
  83.     print _(ur"nudge nudge")
  84.  
  85.     # triple single quotes
  86.     print _('''albatross''')
  87.     print _(u'''mullusk''')
  88.     print _(r'''Raymond Luxury Yach-t''')
  89.     print _(ur'''nudge nudge''')
  90.  
  91.     # triple double quotes
  92.     print _("""albatross""")
  93.     print _(u"""mullusk""")
  94.     print _(r"""Raymond Luxury Yach-t""")
  95.     print _(ur"""nudge nudge""")
  96.  
  97.     # multiline strings
  98.     print _('''This module provides internationalization and localization
  99. support for your Python programs by providing an interface to the GNU
  100. gettext message catalog library.''')
  101.  
  102.     # Now test dgettext()
  103.     def _(message):
  104.         return gettext.dgettext('gettext')
  105.  
  106.  
  107.  
  108. GNU_MO_DATA = '''\
  109. 3hIElQAAAAAFAAAAHAAAAEQAAAAHAAAAbAAAAAAAAACIAAAAFQAAAIkAAAChAAAAnwAAAAcAAABB
  110. AQAACwAAAEkBAAAZAQAAVQEAABYAAABvAgAAoQAAAIYCAAAFAAAAKAMAAAkAAAAuAwAAAQAAAAQA
  111. AAACAAAAAAAAAAUAAAAAAAAAAwAAAABSYXltb25kIEx1eHVyeSBZYWNoLXQAVGhpcyBtb2R1bGUg
  112. cHJvdmlkZXMgaW50ZXJuYXRpb25hbGl6YXRpb24gYW5kIGxvY2FsaXphdGlvbgpzdXBwb3J0IGZv
  113. ciB5b3VyIFB5dGhvbiBwcm9ncmFtcyBieSBwcm92aWRpbmcgYW4gaW50ZXJmYWNlIHRvIHRoZSBH
  114. TlUKZ2V0dGV4dCBtZXNzYWdlIGNhdGFsb2cgbGlicmFyeS4AbXVsbHVzawBudWRnZSBudWRnZQBQ
  115. cm9qZWN0LUlkLVZlcnNpb246IDIuMApQTy1SZXZpc2lvbi1EYXRlOiAyMDAwLTA4LTI5IDEyOjE5
  116. LTA0OjAwCkxhc3QtVHJhbnNsYXRvcjogQmFycnkgQS4gV2Fyc2F3IDxiYXJyeUBweXRob24ub3Jn
  117. PgpMYW5ndWFnZS1UZWFtOiBYWCA8cHl0aG9uLWRldkBweXRob24ub3JnPgpNSU1FLVZlcnNpb246
  118. IDEuMApDb250ZW50LVR5cGU6IHRleHQvcGxhaW47IGNoYXJzZXQ9a29pOF9yCkNvbnRlbnQtVHJh
  119. bnNmZXItRW5jb2Rpbmc6IG5vbmUKR2VuZXJhdGVkLUJ5OiBweWdldHRleHQucHkgMS4xCgBUaHJv
  120. YXR3b2JibGVyIE1hbmdyb3ZlAEd1dmYgemJxaHlyIGNlYml2cXJmIHZhZ3JlYW5ndmJhbnl2bW5n
  121. dmJhIG5hcSB5YnBueXZtbmd2YmEKZmhjY2JlZyBzYmUgbGJoZSBDbGd1YmEgY2VidGVuemYgb2wg
  122. Y2ViaXZxdmF0IG5hIHZhZ3Jlc25wciBnYiBndXIgVEFICnRyZ2dya2cgenJmZm50ciBwbmdueWJ0
  123. IHl2b2VuZWwuAGJhY29uAHdpbmsgd2luawA=
  124. '''
  125.  
  126.  
  127. LOCALEDIR = os.path.join('xx', 'LC_MESSAGES')
  128. MOFILE = os.path.join(LOCALEDIR, 'gettext.mo')
  129.  
  130. def setup():
  131.     os.makedirs(LOCALEDIR)
  132.     fp = open(MOFILE, 'wb')
  133.     fp.write(base64.decodestring(GNU_MO_DATA))
  134.     fp.close()
  135.  
  136. def teardown():
  137.     os.unlink(MOFILE)
  138.     os.removedirs(LOCALEDIR)
  139.  
  140.  
  141. try:
  142.     setup()
  143.     test_api_1(os.curdir, MOFILE)
  144.     test_api_2(os.curdir, MOFILE)
  145. finally:
  146.     teardown()
  147.     pass
  148.  
  149.  
  150.  
  151. # For reference, here's the .po file used to created the .mo data above.
  152.  
  153. '''
  154. # Dummy translation for Python's test_gettext.py module.
  155. # Copyright (C) 2001 Python Software Foundation
  156. # Barry Warsaw <barry@python.org>, 2000.
  157. #
  158. msgid ""
  159. msgstr ""
  160. "Project-Id-Version: 2.0\n"
  161. "PO-Revision-Date: 2000-08-29 12:19-04:00\n"
  162. "Last-Translator: Barry A. Warsaw <barry@python.org>\n"
  163. "Language-Team: XX <python-dev@python.org>\n"
  164. "MIME-Version: 1.0\n"
  165. "Content-Type: text/plain; charset=koi8_r\n"
  166. "Content-Transfer-Encoding: none\n"
  167. "Generated-By: pygettext.py 1.1\n"
  168.  
  169. #: test_gettext.py:19 test_gettext.py:25 test_gettext.py:31 test_gettext.py:37
  170. #: test_gettext.py:51 test_gettext.py:80 test_gettext.py:86 test_gettext.py:92
  171. #: test_gettext.py:98
  172. msgid "nudge nudge"
  173. msgstr "wink wink"
  174.  
  175. #: test_gettext.py:16 test_gettext.py:22 test_gettext.py:28 test_gettext.py:34
  176. #: test_gettext.py:77 test_gettext.py:83 test_gettext.py:89 test_gettext.py:95
  177. msgid "albatross"
  178. msgstr ""
  179.  
  180. #: test_gettext.py:18 test_gettext.py:24 test_gettext.py:30 test_gettext.py:36
  181. #: test_gettext.py:79 test_gettext.py:85 test_gettext.py:91 test_gettext.py:97
  182. msgid "Raymond Luxury Yach-t"
  183. msgstr "Throatwobbler Mangrove"
  184.  
  185. #: test_gettext.py:17 test_gettext.py:23 test_gettext.py:29 test_gettext.py:35
  186. #: test_gettext.py:56 test_gettext.py:78 test_gettext.py:84 test_gettext.py:90
  187. #: test_gettext.py:96
  188. msgid "mullusk"
  189. msgstr "bacon"
  190.  
  191. #: test_gettext.py:40 test_gettext.py:101
  192. msgid ""
  193. "This module provides internationalization and localization\n"
  194. "support for your Python programs by providing an interface to the GNU\n"
  195. "gettext message catalog library."
  196. msgstr ""
  197. "Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba\n"
  198. "fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH\n"
  199. "trggrkg zrffntr pngnybt yvoenel."
  200. '''
  201.