[PyQt] i18n

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


Frédéric wrote:
> On mercredi 11 février 2009, Doug Bell wrote:
> 
> > > 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
> 
> Thanks!
> 
> What is the purpose of the N_() function?

It's for cases where you want to mark a string to be included in the
language files, but you don't want to change the string by doing the
translation immediately.  An example would be a string used as a key in
an untranslated data file that later needs to be translated for output.
The N_() function applied to the string literal would just mark it, then
the _() function could be applied later to a variable containing that
string.

Note that you will also need to slightly modify the pylupdate program
to look for the _() and N_() functions and to use the proper context.

> Is this code still working when the application is frozen? I know there are 
> some problems getting the module name in this case...

This method still works with py2exe, but I don't know about other freeze
methods.

Doug.


More information about the PyQt mailing list