Using gettext for "translation" of original string

Alexander Skwar lists.ASkwar at email-server.info
Thu Mar 13 17:24:19 EST 2003


Hello.

I'd like to use gettext to translate my program to other languages.  I
used pygettext.py to create a messages.pot file containing the to be
translated strings.  I then copied this file to
./locale/en/LC_MESSAGES/xhc.po and translated the strings from the
original German texts to English.  Next I ran msgfmt.py to "compile" a
.mo file out of the .po file.

I'm now able to do:

t = gettext.translation(domain = 'xhc', localedir = 'locale', languages = ['en'])
t.install()
print _('Beenden')

This will print 'Exit'.

But what do I do, when I want the original, untranslated string?
gettext.translation(languages = ['de']) or languages = ['C'] raises an
exception:

>>> t2 = gettext.translation(domain = 'xhc', localedir = 'locale', languages = ['C'])
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.2/gettext.py", line 241, in translation
    raise IOError(ENOENT, 'No translation file found for domain', domain)
IOError: [Errno 2] No translation file found for domain: 'xhc'

Do I have to wrap the gettext.translation() call up in a try: .. except:
clause?  What do I do to my _ method which got defined because of the
t.install() call?  Is something like this the way you do this too?

import gettext

def untranslated(text):
        return text

def installLang(lang):
        try:
                t = gettext.translation(domain = 'xhc', localedir = 'locale',
                                        languages = [lang])
                t.install()
        except IOError:
                import __builtin__
                __builtin__.__dict__['_'] = untranslated

installLang('de')
print _('Beenden')      # prints 'Beenden'

installLang('en')
print _('Beenden')      # prints 'Exit'

When I have a look at C source codes using gettext (eg. GNU hello), this
seems to be somewhat easier in C:

#define _(String) gettext (String)
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);

(No, I do not want to use C - I just wonder why this *seems* to be
easier in C than in Python; however, I'm no good in C and have never
used it much.)

Or is it, that the original C gettext has special provision when the C
locale is used?

Thanks a lot,

Alexander Skwar
-- 
How to quote:	http://learn.to/quote (german) http://quote.6x.to (english)
                       Uptime: 5 days 6 hours 27 minutes





More information about the Python-list mailing list