Skip to content

Instantly share code, notes, and snippets.

@eyllanesc
Created March 4, 2019 05:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eyllanesc/d0bb27ed67c465def5da932d82edecad to your computer and use it in GitHub Desktop.
Save eyllanesc/d0bb27ed67c465def5da932d82edecad to your computer and use it in GitHub Desktop.
<?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>
<?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>
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()
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"))
@eyllanesc
Copy link
Author

eyllanesc commented Mar 4, 2019

Steps:

  1. Execute pylupdate5:

     pylupdate5 main_ui.py -ts lang_en.ts
     pylupdate5 main_ui.py -ts lang_pt.ts
    
  2. Open *.ts files with Qt Linguist and edit the necessary fields:

screenshot from 2019-03-04 00-31-47
screenshot from 2019-03-04 00-32-10

  1. Execute lrelease:

     lrelease lang_es.ts lang_pt.ts
    
  2. Modify the main_ui.py so that the translation is loaded according to the button pressed using the load() method of QTranslator, this change will cause the GUI to send the event QEvent::LanguageChange to changeEvent() so that method is overwritten, and in that method it has to be call retranslateUi() method.

Results:

screenshot from 2019-03-04 00-38-35
screenshot from 2019-03-04 00-38-41
screenshot from 2019-03-04 00-38-46

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment