1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| #!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import division
# Python 2.7
import sys
from PyQt4 import QtCore, QtGui
#############################################################################
def changelangage(fen, langage):
"""appelé à chaque changement de langage"""
global app, translator_qt
# traduction des messages Qt
translator_qt = QtCore.QTranslator()
if translator_qt.load(QtCore.QString("qt_") + langage, QtCore.QLibraryInfo.location(QtCore.QLibraryInfo.TranslationsPath)):
app.installTranslator(translator_qt)
# installation de la traduction des textes
translator = QtCore.QTranslator()
if translator.load(u"test01_" + langage):
app.installTranslator(translator)
# forcer la traduction à chaque changement dynamique de langage
fen.setWindowTitle(QtCore.QCoreApplication.translate("Fenetre", "fenêtre", None, QtGui.QApplication.UnicodeUTF8))
fen.langue1.setText(QtCore.QCoreApplication.translate("Fenetre", "français", None, QtGui.QApplication.UnicodeUTF8))
fen.langue2.setText(QtCore.QCoreApplication.translate("Fenetre", "anglais", None, QtGui.QApplication.UnicodeUTF8))
fen.bouton.setText(QtCore.QCoreApplication.translate("Fenetre", "bouton éèçàùôï", None, QtGui.QApplication.UnicodeUTF8))
fen.titrequestion = QtCore.QCoreApplication.translate("Fenetre", "Question", None, QtGui.QApplication.UnicodeUTF8)
fen.question = QtCore.QCoreApplication.translate("Fenetre", "Est-ce que la traduction marche?", None, QtGui.QApplication.UnicodeUTF8)
#############################################################################
class Fenetre(QtGui.QWidget):
#========================================================================
def __init__(self, parent=None):
super(Fenetre, self).__init__(parent)
self.setWindowTitle(QtCore.QCoreApplication.translate("Fenetre", "fenêtre", None, QtGui.QApplication.UnicodeUTF8))
self.resize(400, 300)
# création des boutons radio
self.langue1 = QtGui.QRadioButton(QtCore.QCoreApplication.translate("Fenetre", "français", None, QtGui.QApplication.UnicodeUTF8), self)
self.langue1.clicked.connect(self.changelangue)
self.langue2 = QtGui.QRadioButton(QtCore.QCoreApplication.translate("Fenetre", "anglais", None, QtGui.QApplication.UnicodeUTF8), self)
self.langue2.clicked.connect(self.changelangue)
# creation du bouton pour voir le messagebox
self.bouton = QtGui.QPushButton(QtCore.QCoreApplication.translate("Fenetre", "bouton éèçàùôï", None, QtGui.QApplication.UnicodeUTF8), self)
self.bouton.clicked.connect(self.boutonclic)
# positionnement dans la fenêtre
posit = QtGui.QGridLayout()
posit.addWidget(self.langue1, 0, 0)
posit.addWidget(self.langue2, 1, 0)
posit.addWidget(self.bouton, 2, 0)
self.setLayout(posit)
# diverses initialisations pour préparer les changements de langue
self.titrequestion = QtCore.QCoreApplication.translate("Fenetre", "Question", None, QtGui.QApplication.UnicodeUTF8)
self.question = QtCore.QCoreApplication.translate("Fenetre", "Est-ce que la traduction marche?", None, QtGui.QApplication.UnicodeUTF8)
# initialisation de la langue au lancement
self.langue1.setChecked(True)
self.changelangue()
#========================================================================
@QtCore.pyqtSlot()
def changelangue(self):
"""appelé à chaque clic sur l'un des radioboutons de changement des
langues
"""
if self.langue1.isChecked():
# passer au français
changelangage(self, "fr_FR")
else:
# passer à l'anglais
changelangage(self, "en_EN")
#========================================================================
@QtCore.pyqtSlot()
def boutonclic(self):
"""appelé au clic sur le bouton pour vérifier la traduction des
messages Qt
"""
reponse = QtGui.QMessageBox.question(self,
self.titrequestion,
self.question,
QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
if reponse == QtGui.QMessageBox.Yes:
pass
else:
pass
#############################################################################
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
fen = Fenetre()
fen.show()
sys.exit(app.exec_()) |
Partager