From c1457d3ef03cfb2c270dc262ec7b8e13d3b74aae Mon Sep 17 00:00:00 2001 From: arcan1s Date: Fri, 11 Jul 2014 07:00:16 +0400 Subject: [PATCH] add language class --- sources/gui/src/CMakeLists.txt | 11 ++- sources/gui/src/language.cpp | 90 +++++++++++++++++++ sources/gui/src/language.h | 39 ++++++++ sources/gui/src/main.cpp | 19 +--- sources/gui/src/settingswindow.cpp | 5 +- sources/resources/resources.qrc | 4 +- .../translations/{english.ts => en.ts} | 0 .../translations/{russian.ts => ru.ts} | 0 8 files changed, 141 insertions(+), 27 deletions(-) create mode 100644 sources/gui/src/language.cpp create mode 100644 sources/gui/src/language.h rename sources/resources/translations/{english.ts => en.ts} (100%) rename sources/resources/translations/{russian.ts => ru.ts} (100%) diff --git a/sources/gui/src/CMakeLists.txt b/sources/gui/src/CMakeLists.txt index 8507db1..bcf1709 100644 --- a/sources/gui/src/CMakeLists.txt +++ b/sources/gui/src/CMakeLists.txt @@ -3,8 +3,7 @@ file (GLOB SOURCES *.cpp) file (GLOB HEADERS *.h) file (GLOB FORMS *.ui) -set (LANGUAGES english - russian) +file (GLOB LANGUAGES ${SUBPROJECT_TRANSLATION_DIR}/*.ts) set (RESOURCES ${SUBPROJECT_RESOURCE_DIR}/resources.qrc) message (STATUS "${SUBPROJECT} Sources: ${SOURCES}") @@ -26,8 +25,8 @@ if (USE_QT5) qt5_add_resources (QRC_SOURCES ${RESOURCES}) foreach (LANGUAGE ${LANGUAGES}) - set (TS ${SUBPROJECT_TRANSLATION_DIR}/${LANGUAGE}.ts) - set (QM ${SUBPROJECT_TRANSLATION_DIR}/${LANGUAGE}.qm) + set (TS ${LANGUAGE}) + string (REPLACE ".ts" ".qm" QM ${TS}) set (TRANSLATIONS ${TRANSLATIONS} ${TS}) set (TRANSLATIONS_BINARY ${TRANSLATIONS_BINARY} ${QM}) add_custom_command (OUTPUT ${QM} COMMAND ${Qt5_LRELEASE_EXECUTABLE} ${TS} MAIN_DEPENDENCY ${TS}) @@ -51,8 +50,8 @@ else () qt4_add_resources (QRC_SOURCES ${RESOURCES}) foreach (LANGUAGE ${LANGUAGES}) - set (TS ${SUBPROJECT_TRANSLATION_DIR}/${LANGUAGE}.ts) - set (QM ${SUBPROJECT_TRANSLATION_DIR}/${LANGUAGE}.qm) + set (TS ${LANGUAGE}) + string (REPLACE ".ts" ".qm" QM ${TS}) set (TRANSLATIONS ${TRANSLATIONS} ${TS}) set (TRANSLATIONS_BINARY ${TRANSLATIONS_BINARY} ${QM}) add_custom_command (OUTPUT ${QM} COMMAND ${QT_LRELEASE_EXECUTABLE} ${TS} MAIN_DEPENDENCY ${TS}) diff --git a/sources/gui/src/language.cpp b/sources/gui/src/language.cpp new file mode 100644 index 0000000..20291e1 --- /dev/null +++ b/sources/gui/src/language.cpp @@ -0,0 +1,90 @@ +/*************************************************************************** + * This file is part of netctl-gui * + * * + * netctl-gui is free software: you can redistribute it and/or * + * modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * netctl-gui is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with netctl-gui. If not, see http://www.gnu.org/licenses/ * + ***************************************************************************/ + + +#include "language.h" + +#include +#include + + +Language::Language(const QString configPath) +{ +} + + +QString Language::defineLanguage(const QString configPath) +{ + QString language; + language = defineLanguageFromFile(configPath); + if (language.isEmpty()) + language = defineLanguageFromLocale(); + language = checkLanguage(language, QString("en")); + return language; +} + + +QString Language::defineLanguageFromFile(const QString configPath) +{ + QString language; + if (configPath.isEmpty()) + return language; + QFile configFile(configPath); + QString fileStr; + if (configFile.open(QIODevice::ReadOnly)) + while (true) { + fileStr = QString(configFile.readLine()); + if (fileStr[0] != '#') { + if (fileStr.contains(QString("LANGUAGE="))) + language = fileStr.split(QString("="))[1] + .remove(QString(" ")) + .trimmed(); + } + if (configFile.atEnd()) + break; + } + configFile.close(); + return language; +} + + +QString Language::defineLanguageFromLocale() +{ + return QLocale::system().name(); +} +\ + +QString Language::checkLanguage(const QString language, const QString defaultLanguage) +{ + QStringList availableLanguages = getAvailableLanguages(); + for (int i=0; i + + +class Language +{ +public: + explicit Language(const QString configPath); + static QString defineLanguage(const QString configPath); + static QString defineLanguageFromFile(const QString configPath); + static QString defineLanguageFromLocale(); + +private: + static QString checkLanguage(const QString language, + const QString defaultLanguage = QString("en")); + static QStringList getAvailableLanguages(); +}; + + +#endif /* LANGUAGE_H */ diff --git a/sources/gui/src/main.cpp b/sources/gui/src/main.cpp index 611e907..0f94fc5 100644 --- a/sources/gui/src/main.cpp +++ b/sources/gui/src/main.cpp @@ -19,10 +19,10 @@ #include #include -#include #include #include +#include "language.h" #include "mainwindow.h" #include "version.h" @@ -37,22 +37,7 @@ int main(int argc, char *argv[]) // translation QString configPath = QDir::homePath() + QDir::separator() + QString(".config") + QDir::separator() + QString("netctl-gui.conf"); - QFile configFile(configPath); - QString fileStr; - QString language = QString("english"); - if (configFile.open(QIODevice::ReadOnly)) - while (true) { - fileStr = QString(configFile.readLine()); - if (fileStr[0] != '#') { - if (fileStr.contains(QString("LANGUAGE="))) - language = fileStr.split(QString("="))[1] - .remove(QString(" ")) - .trimmed(); - } - if (configFile.atEnd()) - break; - } - configFile.close(); + QString language = Language::defineLanguage(configPath); QTranslator translator; translator.load(QString(":/translations/") + language); a.installTranslator(&translator); diff --git a/sources/gui/src/settingswindow.cpp b/sources/gui/src/settingswindow.cpp index 85e025e..bc71360 100644 --- a/sources/gui/src/settingswindow.cpp +++ b/sources/gui/src/settingswindow.cpp @@ -73,8 +73,9 @@ void SettingsWindow::keyPressEvent(QKeyEvent *pressedKey) void SettingsWindow::addLanguages() { - ui->comboBox_language->addItem(QString("english")); - ui->comboBox_language->addItem(QString("russian")); + ui->comboBox_language->clear(); + ui->comboBox_language->addItem(QString("en")); + ui->comboBox_language->addItem(QString("ru")); } diff --git a/sources/resources/resources.qrc b/sources/resources/resources.qrc index 20ef7fd..e353a07 100644 --- a/sources/resources/resources.qrc +++ b/sources/resources/resources.qrc @@ -1,8 +1,8 @@ - translations/english.qm - translations/russian.qm + translations/en.qm + translations/ru.qm icon.png wifi.png diff --git a/sources/resources/translations/english.ts b/sources/resources/translations/en.ts similarity index 100% rename from sources/resources/translations/english.ts rename to sources/resources/translations/en.ts diff --git a/sources/resources/translations/russian.ts b/sources/resources/translations/ru.ts similarity index 100% rename from sources/resources/translations/russian.ts rename to sources/resources/translations/ru.ts