Issue
I want to support multiple languages in my PyQt application. I have *.qm files for each language and install my QTranslator on the app instance. It works fine in my main class but doesn't work in the Settings modal. What is the problem?
this is my code:
run.py
if __name__ == "__main__":
app = QApplication([])
translator = QTranslator()
translator.load(resource_path(r'app_en.qm'))
app.installTranslator(translator)
if API.configs.language == 'fa':
translator.load(resource_path('app_fa.qm'))
app.installTranslator(translator)
main_window = Main()
main_window.show()
app.exec()
main.py
class Main(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
ui_path = resource_path('views', 'main.ui')
loadUi(ui_path, self)
self.config = ConfigParser()
self.config.read("MY_CONFIG_PATH")
self.settings_btn.clicked.connect(self.open_settings_window)
def open_settings_window(self):
from forms.settings import Settings
settings_window = Settings(self.config, parent=self)
settings_window.exec()
settings.py
class Settings(QDialog):
def __init__(self, config, parent=None):
super(Settings, self).__init__(parent)
ui_file_path = resource_path('views', 'settings.ui')
loadUi(ui_file_path, self)
I guarantee that all this words are in my ts files.
Solution
After much effort, trial and error, I finally found the solution to my problem. The issue originated from a place I was entirely sure didn't have any errors (translation files, ts files)! I wasn't using Qt Linguist and manually created tags using the message tag for each translation. However, I had overlooked one point: I was creating all these message tags in the Context block with the name 'MainWindow,' while I should have placed them in the Context block with the tag name 'Dialog.'
app_fa.ts
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="fa">
<context>
<name>MainWindow</name>
<message>
<message>
<source>capturing</source>
<translation>عکس برداری</translation>
</message>
<message>
<source>saved</source>
<translation>ذخیره شدهها</translation>
</message>
<message>
<source>sended</source>
<translation>ارسال شدهها</translation>
</message>
</context>
<context>
<name>Dialog</name>
<message>
<source>Software</source>
<translation>نرم افزار</translation>
</message>
</context>
</TS>
Answered By - Yousef
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.