mirror of
https://github.com/arcan1s/netctl-gui.git
synced 2025-04-24 23:47:21 +00:00
add language class
This commit is contained in:
parent
6dab355d87
commit
c1457d3ef0
@ -3,8 +3,7 @@ file (GLOB SOURCES *.cpp)
|
|||||||
file (GLOB HEADERS *.h)
|
file (GLOB HEADERS *.h)
|
||||||
file (GLOB FORMS *.ui)
|
file (GLOB FORMS *.ui)
|
||||||
|
|
||||||
set (LANGUAGES english
|
file (GLOB LANGUAGES ${SUBPROJECT_TRANSLATION_DIR}/*.ts)
|
||||||
russian)
|
|
||||||
set (RESOURCES ${SUBPROJECT_RESOURCE_DIR}/resources.qrc)
|
set (RESOURCES ${SUBPROJECT_RESOURCE_DIR}/resources.qrc)
|
||||||
|
|
||||||
message (STATUS "${SUBPROJECT} Sources: ${SOURCES}")
|
message (STATUS "${SUBPROJECT} Sources: ${SOURCES}")
|
||||||
@ -26,8 +25,8 @@ if (USE_QT5)
|
|||||||
qt5_add_resources (QRC_SOURCES ${RESOURCES})
|
qt5_add_resources (QRC_SOURCES ${RESOURCES})
|
||||||
|
|
||||||
foreach (LANGUAGE ${LANGUAGES})
|
foreach (LANGUAGE ${LANGUAGES})
|
||||||
set (TS ${SUBPROJECT_TRANSLATION_DIR}/${LANGUAGE}.ts)
|
set (TS ${LANGUAGE})
|
||||||
set (QM ${SUBPROJECT_TRANSLATION_DIR}/${LANGUAGE}.qm)
|
string (REPLACE ".ts" ".qm" QM ${TS})
|
||||||
set (TRANSLATIONS ${TRANSLATIONS} ${TS})
|
set (TRANSLATIONS ${TRANSLATIONS} ${TS})
|
||||||
set (TRANSLATIONS_BINARY ${TRANSLATIONS_BINARY} ${QM})
|
set (TRANSLATIONS_BINARY ${TRANSLATIONS_BINARY} ${QM})
|
||||||
add_custom_command (OUTPUT ${QM} COMMAND ${Qt5_LRELEASE_EXECUTABLE} ${TS} MAIN_DEPENDENCY ${TS})
|
add_custom_command (OUTPUT ${QM} COMMAND ${Qt5_LRELEASE_EXECUTABLE} ${TS} MAIN_DEPENDENCY ${TS})
|
||||||
@ -51,8 +50,8 @@ else ()
|
|||||||
qt4_add_resources (QRC_SOURCES ${RESOURCES})
|
qt4_add_resources (QRC_SOURCES ${RESOURCES})
|
||||||
|
|
||||||
foreach (LANGUAGE ${LANGUAGES})
|
foreach (LANGUAGE ${LANGUAGES})
|
||||||
set (TS ${SUBPROJECT_TRANSLATION_DIR}/${LANGUAGE}.ts)
|
set (TS ${LANGUAGE})
|
||||||
set (QM ${SUBPROJECT_TRANSLATION_DIR}/${LANGUAGE}.qm)
|
string (REPLACE ".ts" ".qm" QM ${TS})
|
||||||
set (TRANSLATIONS ${TRANSLATIONS} ${TS})
|
set (TRANSLATIONS ${TRANSLATIONS} ${TS})
|
||||||
set (TRANSLATIONS_BINARY ${TRANSLATIONS_BINARY} ${QM})
|
set (TRANSLATIONS_BINARY ${TRANSLATIONS_BINARY} ${QM})
|
||||||
add_custom_command (OUTPUT ${QM} COMMAND ${QT_LRELEASE_EXECUTABLE} ${TS} MAIN_DEPENDENCY ${TS})
|
add_custom_command (OUTPUT ${QM} COMMAND ${QT_LRELEASE_EXECUTABLE} ${TS} MAIN_DEPENDENCY ${TS})
|
||||||
|
90
sources/gui/src/language.cpp
Normal file
90
sources/gui/src/language.cpp
Normal file
@ -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 <QFile>
|
||||||
|
#include <QLocale>
|
||||||
|
|
||||||
|
|
||||||
|
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<availableLanguages.count(); i++)
|
||||||
|
if (language == availableLanguages[i])
|
||||||
|
return availableLanguages[i];
|
||||||
|
for (int i=0; i<availableLanguages.count(); i++)
|
||||||
|
if (language.contains(availableLanguages[i] + QChar('_')))
|
||||||
|
return availableLanguages[i];
|
||||||
|
return defaultLanguage;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
QStringList Language::getAvailableLanguages()
|
||||||
|
{
|
||||||
|
QStringList languages;
|
||||||
|
languages.append(QString("en"));
|
||||||
|
languages.append(QString("ru"));
|
||||||
|
return languages;
|
||||||
|
}
|
39
sources/gui/src/language.h
Normal file
39
sources/gui/src/language.h
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
/***************************************************************************
|
||||||
|
* 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/ *
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
#ifndef LANGUAGE_H
|
||||||
|
#define LANGUAGE_H
|
||||||
|
|
||||||
|
#include <QStringList>
|
||||||
|
|
||||||
|
|
||||||
|
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 */
|
@ -19,10 +19,10 @@
|
|||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
|
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
#include <QTextStream>
|
|
||||||
#include <QTranslator>
|
#include <QTranslator>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "language.h"
|
||||||
#include "mainwindow.h"
|
#include "mainwindow.h"
|
||||||
#include "version.h"
|
#include "version.h"
|
||||||
|
|
||||||
@ -37,22 +37,7 @@ int main(int argc, char *argv[])
|
|||||||
// translation
|
// translation
|
||||||
QString configPath = QDir::homePath() + QDir::separator() + QString(".config") +
|
QString configPath = QDir::homePath() + QDir::separator() + QString(".config") +
|
||||||
QDir::separator() + QString("netctl-gui.conf");
|
QDir::separator() + QString("netctl-gui.conf");
|
||||||
QFile configFile(configPath);
|
QString language = Language::defineLanguage(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();
|
|
||||||
QTranslator translator;
|
QTranslator translator;
|
||||||
translator.load(QString(":/translations/") + language);
|
translator.load(QString(":/translations/") + language);
|
||||||
a.installTranslator(&translator);
|
a.installTranslator(&translator);
|
||||||
|
@ -73,8 +73,9 @@ void SettingsWindow::keyPressEvent(QKeyEvent *pressedKey)
|
|||||||
|
|
||||||
void SettingsWindow::addLanguages()
|
void SettingsWindow::addLanguages()
|
||||||
{
|
{
|
||||||
ui->comboBox_language->addItem(QString("english"));
|
ui->comboBox_language->clear();
|
||||||
ui->comboBox_language->addItem(QString("russian"));
|
ui->comboBox_language->addItem(QString("en"));
|
||||||
|
ui->comboBox_language->addItem(QString("ru"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
<!DOCTYPE RCC><RCC version="1.0">
|
<!DOCTYPE RCC><RCC version="1.0">
|
||||||
<qresource>
|
<qresource>
|
||||||
<!-- gui translations -->
|
<!-- gui translations -->
|
||||||
<file>translations/english.qm</file>
|
<file>translations/en.qm</file>
|
||||||
<file>translations/russian.qm</file>
|
<file>translations/ru.qm</file>
|
||||||
<!-- icons -->
|
<!-- icons -->
|
||||||
<file>icon.png</file>
|
<file>icon.png</file>
|
||||||
<file>wifi.png</file>
|
<file>wifi.png</file>
|
||||||
|
Loading…
Reference in New Issue
Block a user