Merge pull request #50 from ltworf/gettext

Use gettext instead of the Qt way
This commit is contained in:
Salvo 'LtWorf' Tomaselli 2020-10-22 08:16:29 +02:00 committed by GitHub
commit 470bb6d9e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 52 deletions

View File

@ -1,16 +1,19 @@
.PHONY: gui .PHONY: gui
gui: relational_gui/survey.py relational_gui/maingui.py relational_gui/rel_edit.py relational_gui/resources.py gui: relational_gui/survey.py relational_gui/maingui.py relational_gui/rel_edit.py relational_gui/resources.py
relational_gui/survey.py: relational_gui/maingui.py relational_gui/survey.py relational_gui/rel_edit.py:
pyuic5 relational_gui/survey.ui > relational_gui/survey.py # Create .py file
pyuic5 $(basename $@).ui > $@
relational_gui/maingui.py: # Use my custom editor class
pyuic5 relational_gui/maingui.ui > relational_gui/maingui.py sed -i 's/QtWidgets.QPlainTextEdit/editor.Editor/g' $@
sed -i 's/QtWidgets.QPlainTextEdit/editor.Editor/g' relational_gui/maingui.py echo 'from . import editor' >> $@
echo 'from . import editor' >> relational_gui/maingui.py # Use gettext instead of Qt translations
echo 'from gettext import gettext as _' >> $@
relational_gui/rel_edit.py: sed -i \
pyuic5 relational_gui/rel_edit.ui > relational_gui/rel_edit.py -e 's/_translate("MainWindow", /_(/g' \
-e 's/_translate("Dialog", /_(/g' \
-e 's/_translate("Form", /_(/g' \
$@
relational_gui/resources.py: relational_gui/resources.py:
pyrcc5 relational_gui/resources.qrc > relational_gui/resources.py pyrcc5 relational_gui/resources.qrc > relational_gui/resources.py

View File

@ -16,6 +16,8 @@
# #
# author Salvo "LtWorf" Tomaselli <tiposchi@tiscali.it> # author Salvo "LtWorf" Tomaselli <tiposchi@tiscali.it>
from gettext import gettext as _
from PyQt5 import QtGui, QtWidgets from PyQt5 import QtGui, QtWidgets
from relational_gui import rel_edit from relational_gui import rel_edit
@ -86,8 +88,8 @@ class creatorForm(QtWidgets.QDialog):
try: try:
header = relation.Header(h) header = relation.Header(h)
except Exception as e: except Exception as e:
QtWidgets.QMessageBox.information(None, QtWidgets.QApplication.translate("Form", "Error"), "%s\n%s" % ( QtWidgets.QMessageBox.information(None, _("Error"), "%s\n%s" % (
QtWidgets.QApplication.translate("Form", "Header error!"), e.__str__())) _("Header error!"), e.__str__()))
return None return None
content = [] content = []
@ -97,8 +99,7 @@ class creatorForm(QtWidgets.QDialog):
try: try:
hlist.append(self.table.item(i, j).text()) hlist.append(self.table.item(i, j).text())
except: except:
QtWidgets.QMessageBox.information(None, QtWidgets.QApplication.translate( QtWidgets.QMessageBox.information(None, _("Error"), _(f'Unset value in {i + 1},{j + 1}!'))
"Form", "Error"), QtWidgets.QApplication.translate("Form", "Unset value in %d,%d!" % (i + 1, j + 1)))
return None return None
content.append(hlist) content.append(hlist)
return relation.Relation.create_from(header, content) return relation.Relation.create_from(header, content)

View File

@ -16,6 +16,7 @@
# #
# author Salvo "LtWorf" Tomaselli <tiposchi@tiscali.it> # author Salvo "LtWorf" Tomaselli <tiposchi@tiscali.it>
import sys import sys
from gettext import gettext as _
from PyQt5 import QtCore, QtWidgets, QtGui from PyQt5 import QtCore, QtWidgets, QtGui
@ -115,19 +116,15 @@ class relForm(QtWidgets.QMainWindow):
online = maintenance.check_latest_version() online = maintenance.check_latest_version()
if online is None: if online is None:
r = QtWidgets.QApplication.translate("Form", "Network error") r = _('Network error')
elif online > version: elif online > version:
r = QtWidgets.QApplication.translate( r = _(f'New version available online: {online}.')
"Form", "New version available online: %s." % online)
elif online == version: elif online == version:
r = QtWidgets.QApplication.translate( r = _('Latest version installed.')
"Form", "Latest version installed.")
else: else:
r = QtWidgets.QApplication.translate( r = _('You are using an unstable version.')
"Form", "You are using an unstable version.")
QtWidgets.QMessageBox.information( QtWidgets.QMessageBox.information(_('Version'), r)
self, QtWidgets.QApplication.translate("Form", "Version"), r)
def setHistoryShown(self, history_shown): def setHistoryShown(self, history_shown):
self.history_shown = history_shown self.history_shown = history_shown
@ -241,7 +238,7 @@ class relForm(QtWidgets.QMainWindow):
if rel is None: # No relation to show if rel is None: # No relation to show
self.ui.table.setColumnCount(1) self.ui.table.setColumnCount(1)
self.ui.table.headerItem().setText(0, "Empty relation") self.ui.table.headerItem().setText(0, _('Empty relation'))
return return
self.ui.table.setColumnCount(len(rel.header)) self.ui.table.setColumnCount(len(rel.header))
@ -282,17 +279,14 @@ class relForm(QtWidgets.QMainWindow):
def saveRelation(self): def saveRelation(self):
if not self.ui.lstRelations.selectedItems(): if not self.ui.lstRelations.selectedItems():
r = QtWidgets.QApplication.translate(
"Form", "Select a relation first."
)
QtWidgets.QMessageBox.information( QtWidgets.QMessageBox.information(
self, QtWidgets.QApplication.translate("Form", "Error"), r self, _('Error'), _('Select a relation first.')
) )
return return
filename = QtWidgets.QFileDialog.getSaveFileName( filename = QtWidgets.QFileDialog.getSaveFileName(
self, QtWidgets.QApplication.translate("Form", "Save Relation"), self, _("Save Relation"),
"", "",
QtWidgets.QApplication.translate("Form", "Json relations (*.json);;CSV relations (*.csv)") _("Json relations (*.json);;CSV relations (*.csv)")
)[0] )[0]
if (len(filename) == 0): # Returns if no file was selected if (len(filename) == 0): # Returns if no file was selected
return return
@ -318,7 +312,7 @@ class relForm(QtWidgets.QMainWindow):
) )
except Exception as e: except Exception as e:
QtWidgets.QMessageBox.warning( QtWidgets.QMessageBox.warning(
self, QtWidgets.QApplication.translate("Form", "Error"), str(e) self, _("Error"), str(e)
) )
return return
if result != None: if result != None:
@ -328,7 +322,7 @@ class relForm(QtWidgets.QMainWindow):
def error(self, exception): def error(self, exception):
print (exception) print (exception)
QtWidgets.QMessageBox.information( QtWidgets.QMessageBox.information(
None, QtWidgets.QApplication.translate("Form", "Error"), None, _("Error"),
str(exception) str(exception)
) )
@ -336,21 +330,16 @@ class relForm(QtWidgets.QMainWindow):
while True: while True:
res = QtWidgets.QInputDialog.getText( res = QtWidgets.QInputDialog.getText(
self, self,
QtWidgets.QApplication.translate("Form", "New relation"), _("New relation"),
QtWidgets.QApplication.translate( _("Insert the name for the new relation"),
"Form", "Insert the name for the new relation"),
QtWidgets.QLineEdit.Normal, '' QtWidgets.QLineEdit.Normal, ''
) )
if res[1] == False: # or len(res[0]) == 0: if res[1] == False: # or len(res[0]) == 0:
return None return None
name = res[0] name = res[0]
if not rtypes.is_valid_relation_name(name): if not rtypes.is_valid_relation_name(name):
r = QtWidgets.QApplication.translate(
"Form", str(
"Wrong name for destination relation: %s." % name)
)
QtWidgets.QMessageBox.information( QtWidgets.QMessageBox.information(
self, QtWidgets.QApplication.translate("Form", "Error"), r self, _("Error"), _('Wrong name for destination relation: {name}.')
) )
continue continue
return name return name
@ -418,12 +407,9 @@ class relForm(QtWidgets.QMainWindow):
if not filenames: if not filenames:
f = QtWidgets.QFileDialog.getOpenFileNames( f = QtWidgets.QFileDialog.getOpenFileNames(
self, self,
QtWidgets.QApplication.translate("Form", "Load Relation"), _("Load Relation"),
"", "",
QtWidgets.QApplication.translate( _("Relations (*.json *.csv);;Text Files (*.txt);;All Files (*)")
"Form",
"Relations (*.json *.csv);;Text Files (*.txt);;All Files (*)"
)
) )
filenames = f[0] filenames = f[0]

View File

@ -1,5 +1,5 @@
# Relational # Relational
# Copyright (C) 2008-2017 Salvo "LtWorf" Tomaselli # Copyright (C) 2008-2020 Salvo "LtWorf" Tomaselli
# #
# Relational is free software: you can redistribute it and/or modify # Relational is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
@ -18,6 +18,7 @@
import platform import platform
import locale import locale
from gettext import gettext as _
from PyQt5 import QtWidgets from PyQt5 import QtWidgets
@ -86,13 +87,10 @@ class surveyForm (QtWidgets.QWidget):
response = maintenance.send_survey(post) response = maintenance.send_survey(post)
if response == 200: if response == 200:
QtWidgets.QMessageBox.information(None, QtWidgets.QApplication.translate( QtWidgets.QMessageBox.information(None, _('Thanks'), _('Thanks for sending!'))
"Form", "Thanks"), QtWidgets.QApplication.translate("Form", "Thanks for sending!"))
elif response == -1: elif response == -1:
QtWidgets.QMessageBox.information(None, QtWidgets.QApplication.translate( QtWidgets.QMessageBox.information(None, _('Seriously?'), _('Yeah, not sending that.'))
"Form", "Seriously?"), QtWidgets.QApplication.translate("Form", "Yeah, not sending that."))
else: else:
QtWidgets.QMessageBox.information(None, QtWidgets.QApplication.translate( QtWidgets.QMessageBox.information(None, _('Error'), _('Unable to send the data!'))
"Form", "Error"), QtWidgets.QApplication.translate("Form", "Unable to send the data!"))
self.hide() self.hide()