Implement setting language with the gui

This commit is contained in:
Martin Marmsoler 2023-11-12 14:47:40 +01:00
parent 49d994134b
commit f2f19ab415
10 changed files with 82 additions and 3 deletions

View File

@ -14,10 +14,18 @@ set(LANGUAGES
set(SRC_DIR ${CMAKE_SOURCE_DIR}/src)
file(GLOB_RECURSE SOURCE_FILES ${SRC_DIR}/*.h ${SRC_DIR}/*.cpp ${SRC_DIR}/*.mm)
set(SYSTEM_LANG_KEY "System")
set(SUPPORTED_LANGUAGES "{\"${SYSTEM_LANG_KEY}\", \"${SYSTEM_LANG_KEY}\"},")
foreach(LANGUAGE ${LANGUAGES})
set(TS_FILES ${TS_FILES} gittyup_${LANGUAGE}.ts)
set(SUPPORTED_LANGUAGES
"${SUPPORTED_LANGUAGES} {\"${LANGUAGE}\", \"${LANGUAGE}\"},")
endforeach()
set(LANGUAGE_SOURCE_FILE "${CMAKE_CURRENT_BINARY_DIR}/languages.cpp")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/languages.cpp.inc"
${LANGUAGE_SOURCE_FILE} @ONLY)
if(UPDATE_TRANSLATIONS)
# FIXME: Clean removes the .ts files.
qt5_create_translation(QM_FILES ${SOURCE_FILES} ${TS_FILES})
@ -28,6 +36,10 @@ endif()
add_custom_target(translations DEPENDS ${QM_FILES})
add_dependencies(gittyup translations)
add_library(translation ${LANGUAGE_SOURCE_FILE})
target_include_directories(translation PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
target_link_libraries(translation Qt5::Core)
# install language files
foreach(LANGUAGE ${LANGUAGES})

9
l10n/languages.cpp.inc Normal file
View File

@ -0,0 +1,9 @@
#include "languages.h"
#include <QList>
#include <QString>
namespace Languages {
const QString system = QStringLiteral("@SYSTEM_LANG_KEY@");
const QMap<const char*, const char*> languages = { @SUPPORTED_LANGUAGES@ };
}

11
l10n/languages.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef LANGUAGES_H
#define LANGUAGES_H
#include <QMap>
namespace Languages {
extern const QString system;
extern const QMap<const char*, const char*> languages;
}
#endif // LANGUAGES_H

View File

@ -16,6 +16,7 @@
#include "ui/RepoView.h"
#include "ui/TabWidget.h"
#include "update/Updater.h"
#include "languages.h"
#include "util/Debug.h"
#include <QCloseEvent>
#include <QCommandLineParser>
@ -172,6 +173,12 @@ Application::Application(int &argc, char **argv, bool haltOnParseError)
.toBool()) &&
(!parser.isSet("no-translation"))) {
// Load translation files.
const auto &language =
Settings::instance()->value(Setting::Id::Language).toString();
if (language != Languages::system)
QLocale::setDefault(QLocale(language));
QLocale locale;
QDir l10n = Settings::l10nDir();
QString name = QString(GITTYUP_NAME).toLower();

View File

@ -67,7 +67,14 @@ target_compile_definitions(
PRIVATE QT_TRANSLATIONS_DIR="${QT_TRANSLATIONS_DIR}"
BUILD_DESCRIPTION="${BUILD_DESCRIPTION}")
target_link_libraries(app conf git ui update Qt5::Widgets)
target_link_libraries(
app
translation
conf
git
ui
update
Qt5::Widgets)
set_target_properties(app PROPERTIES AUTOMOC ON)

View File

@ -1,7 +1,7 @@
add_library(conf ConfFile.cpp Settings.cpp Setting.cpp RecentRepositories.cpp
RecentRepository.cpp)
target_link_libraries(conf lua Qt5::Core util)
target_link_libraries(conf lua Qt5::Core util translation)
# SRC_ definitions point to the source directly
target_compile_definitions(
@ -9,7 +9,7 @@ target_compile_definitions(
PRIVATE CONF_DIR="${RESOURCES_DIR}"
SRC_CONF_DIR="${CMAKE_SOURCE_DIR}/conf"
L10N_DIR="${L10N_INSTALL_DIR}"
SRC_L10N_DIR="${CMAKE_SOURCE_DIR}/l10n"
SRC_L10N_DIR="${CMAKE_BINARY_DIR}/l10n"
SCINTILLUA_LEXERS_DIR="${SCINTILLUA_LEXERS_DIR}"
SRC_SCINTILLUA_LEXERS_DIR="${SRC_SCINTILLUA_LEXERS_DIR}")
set_target_properties(conf PROPERTIES AUTOMOC ON)

View File

@ -26,6 +26,7 @@ void Setting::initialize(QMap<Id, QString> &keys) {
keys[Id::TerminalName] = "terminal/name";
keys[Id::TerminalPath] = "terminal/path";
keys[Id::DontTranslate] = "translation/disable";
keys[Id::Language] = "translation/language";
keys[Id::AllowSingleInstanceOnly] = "singleInstance";
keys[Id::CheckForUpdatesAutomatically] = "update/check";
keys[Id::InstallUpdatesAutomatically] = "update/download";

View File

@ -63,6 +63,7 @@ public:
ShowCommitsId,
ShowChangedFilesAsList,
ShowChangedFilesInSingleView,
Language,
};
Q_ENUM(Id)

View File

@ -11,6 +11,7 @@
#include "ConfFile.h"
#include "Debug.h"
#include "qtsupport.h"
#include "languages.h"
#include <QCoreApplication>
#include <QDir>
#include <QSettings>
@ -26,6 +27,8 @@ namespace {
const QString kIgnoreWsKey("diff/whitespace/ignore");
const QString kLastPathKey("lastpath");
const QString kTranslation("translation");
const QString kTranslationLanguage("language");
// Look up variant at key relative to root.
QVariant lookup(const QVariantMap &root, const QString &key) {
@ -51,6 +54,10 @@ Settings::Settings(QObject *parent) : QObject(parent) {
foreach (const QFileInfo &file, confDir().entryInfoList(QStringList("*.lua")))
mDefaults[file.baseName()] = ConfFile(file.absoluteFilePath()).parse();
mDefaults[kLastPathKey] = QDir::homePath();
QVariantMap map;
map[kTranslationLanguage] = QVariant(Languages::system);
mDefaults[kTranslation] = map;
mDefaults[kTranslation].toMap()[kTranslationLanguage] = Languages::system;
mCurrentMap = mDefaults;
}

View File

@ -25,6 +25,7 @@
#include "ui/MainWindow.h"
#include "ui/MenuBar.h"
#include "ui/RepoView.h"
#include "languages.h"
#include "update/Updater.h"
#include <QAction>
#include <QApplication>
@ -105,6 +106,13 @@ public:
new QCheckBox(tr("Update submodules after pull and clone"), this);
mAutoPrune = new QCheckBox(tr("Prune when fetching"), this);
mNoTranslation = new QCheckBox(tr("No translation"), this);
mLanguages = new QComboBox(this);
QMapIterator<const char *, const char *> i(Languages::languages);
while (i.hasNext()) {
i.next();
mLanguages->addItem(tr(i.key()), QVariant(i.value()));
}
mStoreCredentials =
new QCheckBox(tr("Store credentials in secure storage"), this);
@ -123,6 +131,7 @@ public:
form->addRow(QString(), mPullUpdate);
form->addRow(QString(), mAutoPrune);
form->addRow(tr("Language:"), mNoTranslation);
form->addRow(tr("Language:"), mLanguages);
form->addRow(tr("Credentials:"), mStoreCredentials);
form->addRow(tr("Credential store type:"), mAvailableStores);
form->addRow(QString(), privacy);
@ -184,6 +193,12 @@ public:
Settings::instance()->setValue(Setting::Id::DontTranslate, checked);
});
connect(mLanguages, QOverload<int>::of(&QComboBox::currentIndexChanged),
[this]() {
const auto &language = mLanguages->currentData().toString();
Settings::instance()->setValue(Setting::Id::Language, language);
});
connect(mStoreCredentials, &QCheckBox::toggled, [this](bool checked) {
git::Config config = git::Config::global();
mAvailableStores->setEnabled(checked);
@ -234,6 +249,14 @@ public:
mNoTranslation->setChecked(
settings->value(Setting::Id::DontTranslate).toBool());
const auto &l = settings->value(Setting::Id::Language).toString();
for (int i = 0; i < mLanguages->count(); i++) {
if (mLanguages->itemData(i).toString() == l) {
mLanguages->setCurrentIndex(i);
break;
}
}
auto currentHelper = config.value<QString>("credential.helper");
auto checked = CredentialHelper::isHelperValid(currentHelper);
mStoreCredentials->setChecked(checked);
@ -259,6 +282,7 @@ private:
QCheckBox *mPullUpdate;
QCheckBox *mAutoPrune;
QCheckBox *mNoTranslation;
QComboBox *mLanguages;
QCheckBox *mStoreCredentials;
QComboBox *mAvailableStores;
QCheckBox *mSingleInstance;