Translate with gettext, not Qt

I want to use a uniform way of translating.
This commit is contained in:
Salvo 'LtWorf' Tomaselli 2020-10-22 07:38:40 +02:00
parent fdc0f6b4ea
commit f3947b5367
No known key found for this signature in database
GPG Key ID: B3A7CF0C801886CF
3 changed files with 27 additions and 42 deletions

View File

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

View File

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

View File

@ -1,5 +1,5 @@
# 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
# it under the terms of the GNU General Public License as published by
@ -18,6 +18,7 @@
import platform
import locale
from gettext import gettext as _
from PyQt5 import QtWidgets
@ -86,13 +87,10 @@ class surveyForm (QtWidgets.QWidget):
response = maintenance.send_survey(post)
if response == 200:
QtWidgets.QMessageBox.information(None, QtWidgets.QApplication.translate(
"Form", "Thanks"), QtWidgets.QApplication.translate("Form", "Thanks for sending!"))
QtWidgets.QMessageBox.information(None, _('Thanks'), _('Thanks for sending!'))
elif response == -1:
QtWidgets.QMessageBox.information(None, QtWidgets.QApplication.translate(
"Form", "Seriously?"), QtWidgets.QApplication.translate("Form", "Yeah, not sending that."))
QtWidgets.QMessageBox.information(None, _('Seriously?'), _('Yeah, not sending that.'))
else:
QtWidgets.QMessageBox.information(None, QtWidgets.QApplication.translate(
"Form", "Error"), QtWidgets.QApplication.translate("Form", "Unable to send the data!"))
QtWidgets.QMessageBox.information(None, _('Error'), _('Unable to send the data!'))
self.hide()