From ca271e2a380471c03ed7040a44974ad84781ebf0 Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 22 Mar 2019 03:24:03 +0100 Subject: [PATCH] Rework Translator --- CMakeLists.txt | 2 + main.cpp | 21 ++------ mainwindow.cpp | 105 ++++++++++++---------------------------- mainwindow.h | 16 ++---- translations/gui_de.ts | 105 ++++++++++++++++++++-------------------- translations/gui_en.ts | 107 ++++++++++++++++++++--------------------- translator.cpp | 50 +++++++++++++++++++ translator.h | 30 ++++++++++++ 8 files changed, 223 insertions(+), 213 deletions(-) create mode 100644 translator.cpp create mode 100644 translator.h diff --git a/CMakeLists.txt b/CMakeLists.txt index ec67931..cc37bc0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,6 +32,7 @@ set(SOURCES figuredelegate.cpp minetestmapperexe.cpp colorstxtwizard.cpp + translator.cpp ) set(HEADERS @@ -46,6 +47,7 @@ set(HEADERS figuredelegate.h minetestmapperexe.h colorstxtwizard.h + translator.h ) set(FORMS diff --git a/main.cpp b/main.cpp index d614bff..f2ee4cc 100644 --- a/main.cpp +++ b/main.cpp @@ -1,4 +1,5 @@ #include "mainwindow.h" +#include "translator.h" #include #include #include @@ -15,23 +16,7 @@ int main(int argc, char *argv[]) // Setup the translators - - const QString translationsPath = "./translations/"; - QTranslator qtTranslator; - if(qtTranslator.load("qt_" + QLocale::system().name(), - QLibraryInfo::location(QLibraryInfo::TranslationsPath))) { - a.installTranslator(&qtTranslator); - qDebug()<< QLibraryInfo::location(QLibraryInfo::TranslationsPath); - } - else{ - qtTranslator.load("qt_" + QLocale::system().name(), - translationsPath); - a.installTranslator(&qtTranslator); - } - - QTranslator translator; - if (translator.load("gui_" + QLocale::system().name(), translationsPath)) - a.installTranslator(&translator); + Translator t(QLocale::system()); // Init commandline parser @@ -48,7 +33,7 @@ int main(int argc, char *argv[]) bool portable = parser.isSet(startPortableOption); - MainWindow w(portable, translationsPath, &translator, &qtTranslator); + MainWindow w(portable, &t); w.show(); return a.exec(); diff --git a/mainwindow.cpp b/mainwindow.cpp index 5f02b6d..00ba748 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -2,19 +2,16 @@ #include "ui_mainwindow.h" #include -#include #include #include #include #include -MainWindow::MainWindow(bool portable, const QString &translationsPath, QTranslator *translator, QTranslator *qtTranslator, QWidget *parent) : +MainWindow::MainWindow(bool portable, Translator *translator, QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow), - translator(translator), - qtTranslator(qtTranslator), - translationsPath(translationsPath) + translator(translator) { if(portable){ //Attention: This paths could be non writable locations! @@ -158,41 +155,30 @@ void MainWindow::finishUiInitialisation() void MainWindow::createLanguageMenu() { QActionGroup* langGroup = new QActionGroup(ui->menuLanguage); - langGroup->setExclusive(true); + //langGroup->setExclusive(true); connect(langGroup, &QActionGroup::triggered, this, &MainWindow::slotLanguageChanged); + QStringList translations = translator->getTranslations(); + QLocale::Language defaultLanguage = translator->getCurrentLocale().language(); + for (const QString &locale : translations) { + QLocale l = QLocale(locale); - // format systems language - QString defaultLocale = QLocale::system().name(); // e.g. "de_DE" - qDebug()<<"Default locale:"<translationsPath).arg(locale)); QAction *action = new QAction(ico, lang, this); action->setCheckable(true); - action->setData(locale); + action->setData(l); ui->menuLanguage->addAction(action); langGroup->addAction(action); // set default translators and language checked - if (defaultLocale == locale) + if (defaultLanguage == language) { action->setChecked(true); - //loadLanguage(locale); } } } @@ -200,61 +186,34 @@ void MainWindow::createLanguageMenu() // Called every time, when a menu entry of the language menu is called void MainWindow::slotLanguageChanged(QAction* action) { - if(0 != action) { + if(action) { // load the language dependant on the action content - loadLanguage(action->data().toString()); - + qDebug() << action->data(); + QLocale locale = QLocale(action->data().toLocale()); + translator->setLocale(locale); } } -void MainWindow::switchTranslator(QTranslator *translator, const QString &prefix, const QLocale &locale) -{ - // remove the old translator - qApp->removeTranslator(translator); - qDebug() << "Trying to load language "<< translationsPath << prefix<load(locale , "", prefix, translationsPath)){ - qDebug() << "Loaded translator" << locale; - qApp->installTranslator(translator); - } -} - -void MainWindow::loadLanguage(const QString& rLanguage) -{ - if(m_currLang != rLanguage) { - m_currLang = rLanguage; - QLocale locale = QLocale(m_currLang); - QLocale::setDefault(locale); - QString languageName = QLocale::languageToString(locale.language()); - switchTranslator(translator, "gui_", locale); - switchTranslator(qtTranslator, "qt_", locale); - ui->statusBar->showMessage(tr("Current Language changed to %1").arg(languageName),3000); - } -} void MainWindow::changeEvent(QEvent* event) { - if(0 != event) { - switch(event->type()) { - // this event is send if a translator is loaded - case QEvent::LanguageChange: - ui->retranslateUi(this); - break; + if (event) { + switch (event->type()) { + case QEvent::LanguageChange: + // this event is send if a translator is loaded and installed to the Application + ui->retranslateUi(this); + break; - // this event is send, if the system, language changes - case QEvent::LocaleChange: - { - QString locale = QLocale::system().name(); - locale.truncate(locale.lastIndexOf('_')); - loadLanguage(locale); - } - break; + case QEvent::LocaleChange: + // this event is send by QWidget::setLocale + translator->setLocale(QLocale::system()); + break; - // Ignore other events - default: - break; - } - } - QMainWindow::changeEvent(event); + default: + // Ignore other events + break; + } + } + QMainWindow::changeEvent(event); } MainWindow::~MainWindow() diff --git a/mainwindow.h b/mainwindow.h index 29620e2..273cba7 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -13,20 +13,19 @@ #include #include #include -#include #ifdef Q_OS_WIN #include #include #endif -//#include "colorstxtassistant.h" #include "colorstxtwizard.h" #include "configdialog.h" #include "drawmapfigure.h" #include "drawmapfiguretablemodel.h" #include "figuredelegate.h" #include "minetestmapperexe.h" +#include "translator.h" namespace Ui { class MainWindow; @@ -37,7 +36,7 @@ class MainWindow : public QMainWindow Q_OBJECT public: - explicit MainWindow(bool portable, const QString &translationsPath, QTranslator *translator, QTranslator *qtTranslator, QWidget *parent = 0); + explicit MainWindow(bool portable, Translator *translator, QWidget *parent = 0); ~MainWindow(); enum class GeometryGranularity { @@ -128,8 +127,6 @@ private slots: void on_figureSelect_currentIndexChanged(int index); - void switchTranslator(QTranslator *translator, const QString &prefix, const QLocale &locale); - void on_actionOpen_Terminal_triggered(); private: @@ -146,20 +143,13 @@ private: void finishUiInitialisation(void); - // loads a language by the given language shortcur (e.g. de, en) - void loadLanguage(const QString &rLanguage); - // creates the language menu dynamically from the content of m_langPath void createLanguageMenu(void); - QTranslator *translator; // contains the translations for this application - QTranslator *qtTranslator; // contains the translations for qt - QString m_currLang; // contains the currently loaded language - QString translationsPath; // Path of language files. This is always fixed to /languages. + Translator *translator; // contains the translations for this application QString currentProfile; //contains the name of current loaded profile QString pathAppData; // Path where the settings should be stored. QString pathProfiles; // path where the profiles should be stored. - //QSettings profile; QSettings *settings; QSettings *profile; QString getColorsTxtFilePath(QDir *appDir, QDir *worldDir); diff --git a/translations/gui_de.ts b/translations/gui_de.ts index 02b3851..2675b1e 100644 --- a/translations/gui_de.ts +++ b/translations/gui_de.ts @@ -1290,7 +1290,7 @@ Nodes higher than this level will not be drawn. This can be used to avoid floati - + Cancel Abbrechen @@ -1323,7 +1323,7 @@ Nodes higher than this level will not be drawn. This can be used to avoid floati - + About MinetestMapper Über MinetestMapper @@ -1367,7 +1367,7 @@ Nodes higher than this level will not be drawn. This can be used to avoid floati - + New Profile Neues Profil @@ -1377,7 +1377,7 @@ Nodes higher than this level will not be drawn. This can be used to avoid floati - + Expert Mode Expertenmodus @@ -1392,18 +1392,17 @@ Nodes higher than this level will not be drawn. This can be used to avoid floati Im Expertenmodus werden die Parameter angezeigt, die du verändern kannst. - Current Language changed to %1 - Sprache wurde auf %1 geändert + Sprache wurde auf %1 geändert - + About MinetestMapper GUI Über MinetestMapperGUI - + Minetestmapper not found Minetestmapper nicht gefunden @@ -1428,7 +1427,7 @@ Nodes higher than this level will not be drawn. This can be used to avoid floati Das Bild existiert bereits - + The File <i>%1</i> does already exist. <br><br>Do you want to overwrite? Die Datei <i>%1</i> existiert bereits. <br><br>Soll die Datei Überschrieben werden? @@ -1437,58 +1436,58 @@ Nodes higher than this level will not be drawn. This can be used to avoid floati Der Ordner existiert nicht - + The directory <i>%1</i> does not exist. <br><br>Should it be created? Der Ordner <i>%1</i> existiert nicht. <br><br>Möchten sie den Ordner erstellen? - + MinetestMapper will be executed using this arguments. The arguments can be removed, modified, or new arguments can be added. MinetestMapper wird mit den folgenden Parametern ausgeführt. Die einzelnen Parameter können entfernt, verändert, oder neue hinzugefügt werden. - + Finisched :) Fertig :-) - + minetestmapper terminated minetestmapper abgebrochen - + ERROR: No minetestmapper executable could not be found. Please configure one. FEHLER: Kein Minetestmapper programm gefunden. Bitte Konfiguriere eins. - + ERROR: The Minetetmapper Application (%1) does not look like a Minetetestmapper Please configure a correct MinetestMapper Application. FEHLER: Das Minetestmapper Programm (%1) ist möglicherweise kein Minetestmapper Programm. Bitte Wählen sie eine Korrekte Minetestmapper Anwendung aus. - + (Edit->Preferences) (Bearbeiten->Einstellungen) - + Do you want to open Preferences now? Sollen die Einstellungen jetzt geöffnet werden? - + No input world selected Keine Welt ausgewählt - + ERROR: No MinetestWorld selected. please select a world @@ -1497,12 +1496,12 @@ please select a world Bitte wähle eine aus - + No output image selected Kein Ausgansbild ausgewählt - + ERROR: No output image selected. Please select a output image @@ -1511,28 +1510,28 @@ Please select a output image Bitte ein Ausgabebild wählen - + The image file does already exist Die Datei existiert bereits - + The directory does not exist Der Ordner existiert nicht - - + + Minetest Mapper failed Minetestmapper hat ein Fehler festgestellt - + <h1>ERROR</h1> <h2>minetestmapper failed</h2>Exit code: <i>%1</i> <br>Status of MinetestMapper: <pre>%2</pre><br><br>Please fix the error and try again <h1>Fehler</h1> <h2>kartenerstellung fehlgeschlagen</h2>Exit code: <i>%1</i> <br>Ausgabe des Minetstmapper: <pre>%2</pre><br><br>Bitte den Fehler beheben und erneut versuchen - + <h1>ERROR</h1> <h2>minetestmapper failed</h2>Error code: <i>%1</i> <br>Error Message: <pre>%2</pre><br> <h1>FEHLER</h1> <h2>minetestmapper ist abgestürzt</h2>Fehlercode: <i>%1</i> <br>Fehlernachricht: <pre>%2</pre><br> @@ -1545,92 +1544,92 @@ Bitte ein Ausgabebild wählen Konnte die Einstellungen nicht migrieren - + Can not save settings Kann die Einstellungen nicht speichern - + Minetest Mapper GUI could not save the settings to %1. Please make shure Minetest Mapper Gui can access to the file/directory Minetest Mapper GUI konnte die Einstellungen nicht nach %1 speichern. Bitte stelle sicher, dass Minetest Mapper GUI auf die Datei/ den Ordner zugreifen darf. - + Can not save profile Konnte das Profil nicht speichern - + Minetest Mapper GUI could not save the current Profile '%1' to %2. Please make shure Minetest Mapper Gui can access to the file/directory - + Open Minetest World Minetest Welt-Ordner öffnen - + Save generated map to... Karte speichern nach... - + png image (*.png) png Grafik (*.png) - + Open HeightmapNodes File Öffne HeightmapNodes Datei - - - + + + TXT File (*.txt) TXT Datei (*.txt) - + Open HeightmapColors File Öffne Höhenkarte Farbdefinitionsdatei - + Open colors.txt File Öffne colors.txt Datei - + <h1>About MinetestMapperGUI</h1>The <b>MinetestMapper Gui</b> is written by addi.<br />It is licensed under a <a href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution 3.0 Unported License</a>.<br>The current version is %1. <br>The sourcecode is aviable on <a href='https://bitbucket.org/adrido/minetestmappergui/'>Bitbucket</a>.<br>You may also want to read the <a href='https://forum.minetest.net/viewtopic.php?f=14&t=12139'>Minetest forum thread</a>.<br><br><b>Thanks to:</b><br>McKrustenkaese for his great icon - + <h1>About MinetestMapper</h1>The <b>MinetestMapper</b> is written by:<br>Miroslav Bendík <miroslav.bendik@gmail.com><br>ShadowNinja <shadowninja@minetest.net><br>sfan5 <sfan5@live.de><br>Rogier <rogier777@gmail.com><br><br><u>Version:</u> %1 (%2)<br><u>License:</u> LGPLv2.1+ and BSD 2-clause.<br><u>Source Code:</u> <a href='https://github.com/Rogier-5/minetest-mapper-cpp'>Github</a><br> - + map center - + map origin (top left) - + Create a colors.txt Erstelle eine colors.txt Datei - + There is a nodes.txt but no colors.txt in the world directory Do you want to generate one? If you select 'No' the default colors.txt will be used. @@ -1639,29 +1638,29 @@ Möchtest du eine erstellen? Wenn du auf 'Nein' clickst, wird eine standard colors.txt Datei verwendet. - + No colors.txt file Keine colors.txt Datei - + ERROR: Still no colors.txt file found inside world directory. Do you want to cancel or proceed with default colors.txt file? - + Proceed with default Mit standard Datei fortfahren - + Could not open Terminal - + Error: Could not open scriptfile (%1) for Terminal @@ -1696,12 +1695,12 @@ Bitte erneut eine gültige minetestmapper Anwendung auswählen. (Bearbeiten-> Möchten Sie die Einstellungen jetzt öffnen? - + preview: %1 Vorschau: %1 - + Name of the new Profile: Name des Neuen Profils: diff --git a/translations/gui_en.ts b/translations/gui_en.ts index 9102ad7..6069aa6 100644 --- a/translations/gui_en.ts +++ b/translations/gui_en.ts @@ -978,7 +978,7 @@ Nodes higher than this level will not be drawn. This can be used to avoid floati - + Cancel @@ -1036,7 +1036,7 @@ Nodes higher than this level will not be drawn. This can be used to avoid floati - + About MinetestMapper @@ -1072,7 +1072,7 @@ Nodes higher than this level will not be drawn. This can be used to avoid floati - + About MinetestMapper GUI @@ -1123,13 +1123,13 @@ Nodes higher than this level will not be drawn. This can be used to avoid floati - + New Profile - + Expert Mode @@ -1174,238 +1174,233 @@ Nodes higher than this level will not be drawn. This can be used to avoid floati - - Current Language changed to %1 - - - - + ERROR: No minetestmapper executable could not be found. Please configure one. - + ERROR: The Minetetmapper Application (%1) does not look like a Minetetestmapper Please configure a correct MinetestMapper Application. - + Minetestmapper not found - + The File <i>%1</i> does already exist. <br><br>Do you want to overwrite? - + The directory <i>%1</i> does not exist. <br><br>Should it be created? - + MinetestMapper will be executed using this arguments. The arguments can be removed, modified, or new arguments can be added. - + Finisched :) - + minetestmapper terminated - + (Edit->Preferences) - + Do you want to open Preferences now? - + No input world selected - + ERROR: No MinetestWorld selected. please select a world - + No output image selected - + ERROR: No output image selected. Please select a output image - + The image file does already exist - + The directory does not exist - - + + Minetest Mapper failed - + <h1>ERROR</h1> <h2>minetestmapper failed</h2>Exit code: <i>%1</i> <br>Status of MinetestMapper: <pre>%2</pre><br><br>Please fix the error and try again - + <h1>ERROR</h1> <h2>minetestmapper failed</h2>Error code: <i>%1</i> <br>Error Message: <pre>%2</pre><br> - + Can not save settings - + Minetest Mapper GUI could not save the settings to %1. Please make shure Minetest Mapper Gui can access to the file/directory - + Can not save profile - + Minetest Mapper GUI could not save the current Profile '%1' to %2. Please make shure Minetest Mapper Gui can access to the file/directory - + Open Minetest World - + Save generated map to... - + png image (*.png) - + Open HeightmapNodes File - - - + + + TXT File (*.txt) - + Open HeightmapColors File - + Open colors.txt File - + <h1>About MinetestMapperGUI</h1>The <b>MinetestMapper Gui</b> is written by addi.<br />It is licensed under a <a href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution 3.0 Unported License</a>.<br>The current version is %1. <br>The sourcecode is aviable on <a href='https://bitbucket.org/adrido/minetestmappergui/'>Bitbucket</a>.<br>You may also want to read the <a href='https://forum.minetest.net/viewtopic.php?f=14&t=12139'>Minetest forum thread</a>.<br><br><b>Thanks to:</b><br>McKrustenkaese for his great icon - + <h1>About MinetestMapper</h1>The <b>MinetestMapper</b> is written by:<br>Miroslav Bendík <miroslav.bendik@gmail.com><br>ShadowNinja <shadowninja@minetest.net><br>sfan5 <sfan5@live.de><br>Rogier <rogier777@gmail.com><br><br><u>Version:</u> %1 (%2)<br><u>License:</u> LGPLv2.1+ and BSD 2-clause.<br><u>Source Code:</u> <a href='https://github.com/Rogier-5/minetest-mapper-cpp'>Github</a><br> - + map center - + map origin (top left) - + Create a colors.txt - + There is a nodes.txt but no colors.txt in the world directory Do you want to generate one? If you select 'No' the default colors.txt will be used. - + No colors.txt file - + ERROR: Still no colors.txt file found inside world directory. Do you want to cancel or proceed with default colors.txt file? - + Proceed with default - + Could not open Terminal - + Error: Could not open scriptfile (%1) for Terminal - + preview: %1 - + Name of the new Profile: diff --git a/translator.cpp b/translator.cpp new file mode 100644 index 0000000..b631e4a --- /dev/null +++ b/translator.cpp @@ -0,0 +1,50 @@ +#include "translator.h" + +#include +#include +#include +#include + +Translator::Translator(const QLocale &locale) +{ + load(locale); +} + +void Translator::load(const QLocale &locale) +{ + qDebug() << "Loading Locale:" << locale; + if (qtTranslator.load(locale, "qt", "_", systemTranslationPath)) { + qApp->installTranslator(&qtTranslator); + qDebug() << "Qt Translator installed from system path" << systemTranslationPath; + } + else { + qtTranslator.load(locale, "qt", "_", translationsPath); + qApp->installTranslator(&qtTranslator); + qDebug() << "Qt Translator installed from standard path" << translationsPath; + } + + if (appTranslator.load(locale, "gui", "_", translationsPath)) + qApp->installTranslator(&appTranslator); +} + +QStringList Translator::getTranslations() const +{ + QDir dir(translationsPath); + QStringList fileNames = dir.entryList(QStringList("qt_*.qm")); + for (QString &s : fileNames) { + s.truncate(s.lastIndexOf('.')); // "qt_de" + s.remove(0, s.indexOf('_') + 1); // "de" + } + return fileNames; +} + +bool Translator::setLocale(const QLocale & newLocale) +{ + if (currentLocale == newLocale) + return false; + + QLocale::setDefault(newLocale); + currentLocale = newLocale; + load(newLocale); + return true; +} diff --git a/translator.h b/translator.h new file mode 100644 index 0000000..9569acd --- /dev/null +++ b/translator.h @@ -0,0 +1,30 @@ +#ifndef TRANSLATOR_H +#define TRANSLATOR_H + +#include +#include +#include +#include +#include + +class Translator +{ +public: + explicit Translator(const QLocale &locale = QLocale::system()); + + const QString translationsPath = "./translations/"; + const QString systemTranslationPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath); + + QStringList getTranslations() const; + QLocale getCurrentLocale() const { return currentLocale; } + bool setLocale(const QLocale &newLocale); + +private: + void load(const QLocale &locale); + + QLocale currentLocale; + QTranslator qtTranslator; + QTranslator appTranslator; +}; + +#endif // TRANSLATOR_H