SIP4 python bindings for TQt
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

i18n.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/usr/bin/env python
  2. # Copyright (c) 2002 Detlev Offenbach <detlev@die-offenbachs.de>
  3. from whrandom import randint
  4. import sys
  5. from python_tqt.qt import *
  6. from mywidget import MyWidget
  7. class TQVDialog(TQDialog):
  8. def __init__(self, parent=None, name=None, modal=0, flags=0):
  9. TQDialog.__init__(self, parent, name, modal, flags)
  10. self.vb = TQVBoxLayout(self, 8)
  11. self.vb.setAutoAdd(1)
  12. self.hb = None
  13. self.map = {}
  14. #self.sm = TQSignalMapper(self)
  15. self.connect(self, PYSIGNAL('mapped(int)'), self.done)
  16. def addButtons(self, cancel=None, ok=None, mid1=None, mid2=None, mid3=None):
  17. if ok is None:
  18. self.addButton(self.tr("OK"),1)
  19. else:
  20. self.addButton(ok,1)
  21. if mid1 is not None:
  22. self.addButton(mid1,2)
  23. if mid2 is not None:
  24. self.addButton(mid2,3)
  25. if mid3 is not None:
  26. self.addButton(mid3,4)
  27. if cancel is None:
  28. self.addButton(self.tr('Cancel'),0)
  29. else:
  30. self.addButton(cancel,0)
  31. def addButton(self, text, result):
  32. if self.hb is None:
  33. self.hb = TQHBox(self)
  34. c = TQPushButton(text, self.hb)
  35. self.setMapping(c, result)
  36. self.connect(c, SIGNAL('clicked()'), self.mapit)
  37. def setMapping(self, c, result):
  38. self.map[c] = result
  39. def mapit(self):
  40. qo = self.sender()
  41. self.emit(PYSIGNAL('mapped(int)'), (self.map[qo],))
  42. translator = None
  43. wlist = [] # keep reference to widgets
  44. def showLang(lang):
  45. global translator
  46. tqApp.setPalette(TQPalette(TQColor(220-randint(0,64),220-randint(0,64),220-randint(0,64))))
  47. language = "mywidget_"+lang+".qm"
  48. fi = TQFileInfo(language)
  49. if not fi.exists():
  50. TQMessageBox.warning(None, "File error",
  51. "Cannot find translation for language: "+lang+\
  52. "\n(try eg. 'de' or 'en')")
  53. return None
  54. if translator is not None:
  55. tqApp.removeTranslator(translator)
  56. translator = None
  57. translator = TQTranslator(None)
  58. translator.load(language,".")
  59. tqApp.installTranslator(translator)
  60. m = MyWidget()
  61. m.setCaption("PyTQt Example - i18n - " + unicode(m.caption()))
  62. wlist.append(m)
  63. return m
  64. def main(argv):
  65. app = TQApplication(argv)
  66. qm = ["cs", "de", "el", "en", "eo", "fr", "it", "jp", "ko", "no", "ru", "zh"]
  67. lang = None
  68. if len(argv) == 2:
  69. lang = argv[1]
  70. if (len(argv) != 2) or (lang == "all"):
  71. dlg = TQVDialog(None, None, 1)
  72. qmb = []
  73. r = 0
  74. if lang == "all":
  75. r=2
  76. else:
  77. bg = TQButtonGroup(4, TQt.Vertical, "Choose Locales", dlg)
  78. loc = TQTextCodec.locale()
  79. for i in range(0,len(qm)):
  80. qmb.append(TQCheckBox(qm[i], bg))
  81. qmb[i].setChecked(str(loc) == qm[i])
  82. dlg.addButtons("Cancel","OK","All")
  83. r = dlg.exec_loop()
  84. if r:
  85. tight = tqApp.desktop().screen().width < 1024
  86. x = 5
  87. y = 25
  88. for i in range(0,len(qm)):
  89. if (r == 2) or (qmb[i].isChecked()):
  90. w = showLang(qm[i])
  91. if w == None:
  92. sys.exit(0)
  93. app.connect(app, SIGNAL('lastWindowClosed()'), tqApp, SLOT('quit()'))
  94. w.setGeometry(x,y,197,356)
  95. w.show()
  96. if tight:
  97. x += 8
  98. y += 8
  99. else:
  100. x += 205
  101. if x > 1000:
  102. x = 5
  103. y += 384
  104. else:
  105. sys.exit(0)
  106. else:
  107. lang = argv[1]
  108. m = showLang(lang)
  109. app.setMainWidget(m)
  110. m.setCaption("PyTQt Example - i18n")
  111. m.show()
  112. return app.exec_loop()
  113. if __name__ == "__main__":
  114. main(sys.argv)