Created
March 4, 2019 05:30
-
-
Save eyllanesc/d0bb27ed67c465def5da932d82edecad to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<!DOCTYPE TS> | |
<TS version="2.1" language="es"> | |
<context> | |
<name>Form</name> | |
<message> | |
<location filename="main_ui.py" line="31"/> | |
<source>Form</source> | |
<translation>Formulario</translation> | |
</message> | |
<message> | |
<location filename="main_ui.py" line="32"/> | |
<source>save</source> | |
<translation>guardar</translation> | |
</message> | |
<message> | |
<location filename="main_ui.py" line="33"/> | |
<source>cancel</source> | |
<translation>cancelar</translation> | |
</message> | |
<message> | |
<location filename="main_ui.py" line="34"/> | |
<source>send</source> | |
<translation>enviar</translation> | |
</message> | |
<message> | |
<location filename="main_ui.py" line="35"/> | |
<source>EN</source> | |
<translation>EN</translation> | |
</message> | |
<message> | |
<location filename="main_ui.py" line="36"/> | |
<source>PT</source> | |
<translation>PT</translation> | |
</message> | |
<message> | |
<location filename="main_ui.py" line="37"/> | |
<source>ES</source> | |
<translation>ES</translation> | |
</message> | |
</context> | |
</TS> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<!DOCTYPE TS> | |
<TS version="2.1" language="pt"> | |
<context> | |
<name>Form</name> | |
<message> | |
<location filename="main_ui.py" line="31"/> | |
<source>Form</source> | |
<translation>Formulário</translation> | |
</message> | |
<message> | |
<location filename="main_ui.py" line="32"/> | |
<source>save</source> | |
<translation>salvar</translation> | |
</message> | |
<message> | |
<location filename="main_ui.py" line="33"/> | |
<source>cancel</source> | |
<translation>cancelar</translation> | |
</message> | |
<message> | |
<location filename="main_ui.py" line="34"/> | |
<source>send</source> | |
<translation>enviar</translation> | |
</message> | |
<message> | |
<location filename="main_ui.py" line="35"/> | |
<source>EN</source> | |
<translation>EN</translation> | |
</message> | |
<message> | |
<location filename="main_ui.py" line="36"/> | |
<source>PT</source> | |
<translation>PT</translation> | |
</message> | |
<message> | |
<location filename="main_ui.py" line="37"/> | |
<source>ES</source> | |
<translation>ES</translation> | |
</message> | |
</context> | |
</TS> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from PyQt5 import QtCore, QtWidgets | |
import main_ui | |
class ExampleApp(QtWidgets.QMainWindow, main_ui.Ui_Form): | |
def __init__(self): | |
super().__init__() | |
self.setupUi(self) | |
self.en_btn.clicked.connect(self.retr) | |
self.pt_btn.clicked.connect(self.retr) | |
self.es_btn.clicked.connect(self.retr) | |
self.trans = QtCore.QTranslator(self) | |
@QtCore.pyqtSlot() | |
def retr(self): | |
text = self.sender().text() | |
d = { | |
"ES" : "lang_es", | |
"PT": "lang_pt" | |
} | |
data = d.get(text) | |
if data: | |
self.trans.load(data) | |
QtWidgets.QApplication.instance().installTranslator(self.trans) | |
else: | |
QtWidgets.QApplication.instance().removeTranslator(self.trans) | |
def changeEvent(self, event): | |
if event.type() == QtCore.QEvent.LanguageChange: | |
self.retranslateUi(self) | |
super(ExampleApp, self).changeEvent(event) | |
def main(): | |
app = QtWidgets.QApplication(sys.argv) | |
window = ExampleApp() | |
window.show() | |
app.exec_() | |
if __name__ == '__main__': | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PyQt5 import QtCore, QtGui, QtWidgets | |
class Ui_Form(object): | |
def setupUi(self, Form): | |
Form.setObjectName("Form") | |
Form.resize(364, 135) | |
self.save_btn = QtWidgets.QPushButton(Form) | |
self.save_btn.setGeometry(QtCore.QRect(30, 30, 85, 27)) | |
self.save_btn.setObjectName("save_btn") | |
self.cancel_btn = QtWidgets.QPushButton(Form) | |
self.cancel_btn.setGeometry(QtCore.QRect(150, 30, 85, 27)) | |
self.cancel_btn.setObjectName("cancel_btn") | |
self.send_btn = QtWidgets.QPushButton(Form) | |
self.send_btn.setGeometry(QtCore.QRect(260, 30, 85, 27)) | |
self.send_btn.setObjectName("send_btn") | |
self.en_btn = QtWidgets.QPushButton(Form) | |
self.en_btn.setGeometry(QtCore.QRect(30, 80, 85, 27)) | |
self.en_btn.setObjectName("en_btn") | |
self.pt_btn = QtWidgets.QPushButton(Form) | |
self.pt_btn.setGeometry(QtCore.QRect(260, 80, 85, 27)) | |
self.pt_btn.setObjectName("pt_btn") | |
self.es_btn = QtWidgets.QPushButton(Form) | |
self.es_btn.setGeometry(QtCore.QRect(150, 80, 85, 27)) | |
self.es_btn.setObjectName("es_btn") | |
self.retranslateUi(Form) | |
QtCore.QMetaObject.connectSlotsByName(Form) | |
def retranslateUi(self, Form): | |
_translate = QtCore.QCoreApplication.translate | |
Form.setWindowTitle(_translate("Form", "Form")) | |
self.save_btn.setText(_translate("Form", "save")) | |
self.cancel_btn.setText(_translate("Form", "cancel")) | |
self.send_btn.setText(_translate("Form", "send")) | |
self.en_btn.setText(_translate("Form", "EN")) | |
self.pt_btn.setText(_translate("Form", "PT")) | |
self.es_btn.setText(_translate("Form", "ES")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Steps:
Execute pylupdate5:
Open *.ts files with Qt Linguist and edit the necessary fields:
Execute lrelease:
Modify the
main_ui.py
so that the translation is loaded according to the button pressed using theload()
method ofQTranslator
, this change will cause the GUI to send the eventQEvent::LanguageChange
tochangeEvent()
so that method is overwritten, and in that method it has to be callretranslateUi()
method.Results: