[PyQt] Sharing localized strings with non-PyQt applications

Giovanni Bajo rasky at develer.com
Wed Jul 11 18:04:26 BST 2007

On 7/11/2007 6:26 PM, dwelch91 wrote:

> I have a need to be able to "share" PyQt localized strings with a non-Qt 
> program. I was hoping on putting the strings into a module that is then 
> imported into both the PyQt and non-PyQt program, and somehow have the 
> non-PyQt program reassign __tr() so that it can access the strings. Any 
> thoughts on how I might do this?

1) Put your strings freely within your non-qt code. Mark them with 
self.tr() calls.

2) Implement a fake tr method in your non-qt classes, like this:

   def tr(txt):
       return txt

3) When the string eventually arrives to qt code, you must translate it:

   def whatever(self, txt):
      # txt passed through a *fake* tr() call: translate it now
      txt = qApp.translate("CLASS_NAME", txt)

4) "CLASS_NAME" is the name of the class in your non-qt code where the 
string was "born" :)

Giovanni Bajo

More information about the PyQt mailing list