Skip to content

Commit 1cf032c

Browse files
committed
Refactor: Translation function for Database
For #168
1 parent 7a85c58 commit 1cf032c

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

src/db/database.cpp

+12-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include "src/db/db_error.h"
2727

28+
#include <QCoreApplication>
2829
#include <QSqlError>
2930
#include <QSqlQuery>
3031
#include <QFile>
@@ -114,8 +115,9 @@ void Database::createNew(QWidget* parent, const QString& filepath)
114115
sql.setDatabaseName(filepath);
115116

116117
// Open connection
117-
if (!sql.open())
118+
if (!sql.open()) {
118119
displayError(parent, sql.lastError());
120+
}
119121
databaseLoaded = true;
120122

121123
qDebug() << "Creating tables in SQL";
@@ -143,8 +145,9 @@ void Database::openExisting(QWidget* parent, const QString& filepath)
143145
sql.setDatabaseName(filepath);
144146

145147
// Open connection
146-
if (!sql.open())
148+
if (!sql.open()) {
147149
displayError(parent, sql.lastError());
150+
}
148151
databaseLoaded = true;
149152

150153
populateBuffers(parent);
@@ -224,6 +227,13 @@ void Database::populateBuffers(QWidget* parent)
224227

225228

226229

230+
QString Database::tr(const QString& string)
231+
{
232+
return QCoreApplication::translate("Database", string.toStdString().c_str());
233+
}
234+
235+
236+
227237
/**
228238
* Returns a list of all tables in the database (not including the project settings table).
229239
*

src/db/database.h

+3
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ class Database {
123123
QList<WhatIfDeleteResult> removeRow_referenceSearch(QWidget* parent, bool searchNotExecute, NormalTable* table, ValidItemID primaryKey);
124124

125125
void populateBuffers(QWidget* parent);
126+
127+
public:
128+
static QString tr(const QString& string);
126129
};
127130

128131

src/db/db_error.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
#include "db_error.h"
2525

26-
#include <QApplication>
26+
#include "database.h"
2727

2828
#include <QMessageBox>
2929

@@ -37,7 +37,7 @@
3737
*/
3838
void displayError(QWidget* parent, QString error)
3939
{
40-
QMessageBox::critical(parent, QCoreApplication::translate("Database", "Database error"), error);
40+
QMessageBox::critical(parent, Database::tr("Database error"), error);
4141
exit(1);
4242
}
4343

@@ -51,7 +51,7 @@ QString formatSqlError(QSqlError error)
5151
{
5252
QString driverError = error.driverText(); // Translated
5353
QString databaseError = error.databaseText(); // Untranslated
54-
return driverError + ".\n\n" + QCoreApplication::translate("Database", "Details:") + " \"" + databaseError + "\"";
54+
return driverError + ".\n\n" + Database::tr("Details:") + " \"" + databaseError + "\"";
5555
}
5656

5757

@@ -65,7 +65,7 @@ QString formatSqlError(QSqlError error)
6565
*/
6666
void displayError(QWidget* parent, QString error, QString& queryString)
6767
{
68-
return displayError(parent, error + "\n\n" + QCoreApplication::translate("Database", "Query:") + "\n" + queryString);
68+
return displayError(parent, error + "\n\n" + Database::tr("Query:") + "\n" + queryString);
6969
}
7070

7171
/**

0 commit comments

Comments
 (0)