Skip to content

Commit

Permalink
chore: Add language selection functionality to the UI
Browse files Browse the repository at this point in the history
  • Loading branch information
LyubomirT committed Jul 26, 2024
1 parent 59ddff6 commit 3fd404d
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 7 deletions.
6 changes: 5 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def __init__(self):
self.setWindowFlags(self.windowFlags() | Qt.WindowStaysOnTopHint)
self.startButton.clicked.connect(self.start_typing)
self.stopButton.clicked.connect(self.stop_typing)
self.languageComboBox.currentTextChanged.connect(self.change_language)
self.thread = None

def start_typing(self):
Expand Down Expand Up @@ -79,9 +80,12 @@ def stop_typing(self):
self.thread = None
self.typing_finished()

def change_language(self, language):
self.update_ui_text()

if __name__ == "__main__":
app = QApplication(sys.argv)
app.setStyleSheet(open("style.css").read())
window = MainWindow()
window.show()
sys.exit(app.exec_())
sys.exit(app.exec_())
28 changes: 28 additions & 0 deletions translations/English.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"MainWindow": [
{
"component": "delayLabel",
"text": "Delay (s):"
},
{
"component": "intervalLabel",
"text": "Interval (s):"
},
{
"component": "charPerStrokeLabel",
"text": "Chars per stroke:"
},
{
"component": "enterCheckBox",
"text": "Type Enter"
},
{
"component": "startButton",
"text": "Start Typing"
},
{
"component": "stopButton",
"text": "Stop Typing"
}
]
}
28 changes: 28 additions & 0 deletions translations/French.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"MainWindow": [
{
"component": "delayLabel",
"text": "Délai (s) :"
},
{
"component": "intervalLabel",
"text": "Intervalle (s) :"
},
{
"component": "charPerStrokeLabel",
"text": "Caractères par frappe :"
},
{
"component": "enterCheckBox",
"text": "Taper Entrée"
},
{
"component": "startButton",
"text": "Commencer la frappe"
},
{
"component": "stopButton",
"text": "Arrêter la frappe"
}
]
}
44 changes: 38 additions & 6 deletions ui.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from PyQt5 import QtCore, QtGui, QtWidgets
import ctypes
import json
import os

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
Expand Down Expand Up @@ -73,6 +75,10 @@ def setupUi(self, MainWindow):
self.progressBar.setObjectName("progressBar")
self.settingsLayout.addWidget(self.progressBar)

self.languageComboBox = QtWidgets.QComboBox(self.horizontalLayoutWidget)
self.languageComboBox.setObjectName("languageComboBox")
self.settingsLayout.addWidget(self.languageComboBox)

self.horizontalLayout.addLayout(self.settingsLayout)

MainWindow.setCentralWidget(self.centralwidget)
Expand All @@ -81,11 +87,37 @@ def setupUi(self, MainWindow):
QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):
self.current_language = "en" # Default language
self.translations = {}
self.load_translations()

self.languageComboBox.currentTextChanged.connect(self.change_language)
self.update_ui_text()

def load_translations(self):
translations_dir = "translations"
for filename in os.listdir(translations_dir):
if filename.endswith(".json"):
language_code = filename[:-5] # Remove .json extension
with open(os.path.join(translations_dir, filename), "r", encoding="utf-8") as f:
self.translations[language_code] = json.load(f)
self.languageComboBox.addItem(language_code)

def change_language(self, language):
self.current_language = language
self.update_ui_text()

def update_ui_text(self):
translation = self.translations.get(self.current_language, self.translations["English"])
for item in translation["MainWindow"]:
component = getattr(self, item["component"], None)
if component:
component.setText(item["text"])

_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Chainsaw Human Typing"))
self.delayLabel.setText(_translate("MainWindow", "Delay (s):"))
self.intervalLabel.setText(_translate("MainWindow", "Interval (s):"))
self.charPerStrokeLabel.setText(_translate("MainWindow", "Chars per stroke:"))
self.delayLabel.setText(_translate("MainWindow", "Delay (seconds)"))
self.intervalLabel.setText(_translate("MainWindow", "Interval (seconds)"))
self.charPerStrokeLabel.setText(_translate("MainWindow", "Characters per stroke"))
self.enterCheckBox.setText(_translate("MainWindow", "Type Enter"))
self.startButton.setText(_translate("MainWindow", "Start Typing"))
self.stopButton.setText(_translate("MainWindow", "Stop Typing"))
self.startButton.setText(_translate("MainWindow", "Start"))
self.stopButton.setText(_translate("MainWindow", "Stop"))

0 comments on commit 3fd404d

Please sign in to comment.