[PyQt] i18n

Doug Bell dougb at bellz.org
Wed Feb 11 16:18:33 GMT 2009


Frédéric wrote:
> Does anybody could point me on example how to use i18n in PyQt?
> 
> Riverbank documentation says that one should not use tr() method, but
> insteead QtCore.QCoreApplication.translate(). As the object must be
> passed to this method, this not improves the readability :o/
> 
> Are there some tips, for example, to bind the _() method I use with
> gettext and PyGTK? Something to help me migrate my code without having
> to modify it everywhere...

Here's how I do it:

    def translate(text, comment=''):
        """Translation function that sets context to calling module's
           filename"""
        try:
            frame = sys._getframe(1)
            fileName = frame.f_code.co_filename
        finally:
            del frame
        context = os.path.basename(os.path.splitext(fileName)[0])
        return unicode(QtCore.QCoreApplication.translate(context, text,
                                                         comment))

    def markNoTranslate(text, comment=''):
        """Mark text for translation without actually doing the
           translation"""
        return text

    __builtin__._ = translate
    __builtin__.N_ = markNoTranslate


> Are there also some tools to migrate a .po file to a Qt i18n stuff?

I don't know.

Doug.


More information about the PyQt mailing list