Skip to content

Commit 652ad9f

Browse files
authored
Add UI language selection (flameshot-org#4249)
* Add UI language config option * Clang format
1 parent f7995dd commit 652ad9f

File tree

6 files changed

+110
-16
lines changed

6 files changed

+110
-16
lines changed

flameshot.example.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
;; Default file extension for screenshots
1919
;saveAsFileExtension=.png
2020
;
21+
;; UI language (auto = detected system language)
22+
; uiLanguage=auto
23+
;
2124
;; Main UI color
2225
;; Color is any valid hex code or W3C color name
2326
;uiColor=#740096

src/config/visualseditor.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
#include "src/config/extendedslider.h"
88
#include "src/config/uicoloreditor.h"
99
#include "src/utils/confighandler.h"
10+
#include <QDirIterator>
1011
#include <QHBoxLayout>
1112
#include <QLabel>
13+
#include <QMessageBox>
1214

1315
VisualsEditor::VisualsEditor(QWidget* parent)
1416
: QWidget(parent)
@@ -57,6 +59,8 @@ void VisualsEditor::initOpacitySlider()
5759

5860
void VisualsEditor::initWidgets()
5961
{
62+
initTranslations();
63+
6064
m_tabWidget = new QTabWidget();
6165
m_layout->addWidget(m_tabWidget);
6266

@@ -89,3 +93,55 @@ void VisualsEditor::initWidgets()
8993
&ButtonListView::selectAll);
9094
listLayout->addWidget(setAllButtons);
9195
}
96+
97+
void VisualsEditor::initTranslations()
98+
{
99+
auto* localLayout = new QHBoxLayout();
100+
localLayout->addWidget(new QLabel(tr("UI language")));
101+
m_selectTranslation = new QComboBox(this);
102+
103+
QStringList translations;
104+
QString tmpFilename;
105+
for (const QString& path : PathInfo::translationsPaths()) {
106+
QDirIterator it(path,
107+
QStringList() << QStringLiteral("*.qm"),
108+
QDir::NoDotAndDotDot | QDir::Files);
109+
while (it.hasNext()) {
110+
it.next();
111+
tmpFilename = it.fileName();
112+
113+
if (tmpFilename.startsWith(
114+
QStringLiteral("Internationalization_"))) {
115+
tmpFilename =
116+
tmpFilename.remove(QStringLiteral("Internationalization_"))
117+
.remove(QStringLiteral(".qm"));
118+
if (!translations.contains(tmpFilename)) {
119+
translations << tmpFilename;
120+
}
121+
}
122+
}
123+
}
124+
translations.sort();
125+
translations.push_front(QStringLiteral("auto"));
126+
m_selectTranslation->addItems(translations);
127+
128+
QString language = ConfigHandler().value("uiLanguage").toString();
129+
m_selectTranslation->setCurrentIndex(
130+
m_selectTranslation->findText(language));
131+
132+
connect(m_selectTranslation,
133+
&QComboBox::currentTextChanged,
134+
this,
135+
[this](const QString& text) {
136+
ConfigHandler().setUiLanguage(text);
137+
// TODO: Retranslate UI without restart
138+
QMessageBox::information(
139+
this,
140+
tr("Configuration"),
141+
tr("Flameshot must be restarted to apply these changes!"));
142+
});
143+
144+
localLayout->addWidget(m_selectTranslation);
145+
localLayout->addStretch();
146+
m_layout->addLayout(localLayout);
147+
}

src/config/visualseditor.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#pragma once
55

6+
#include <QComboBox>
67
#include <QTabWidget>
78
#include <QWidget>
89

@@ -35,6 +36,9 @@ public slots:
3536
ButtonListView* m_buttonList;
3637
ExtendedSlider* m_opacitySlider;
3738

39+
QComboBox* m_selectTranslation;
40+
3841
void initWidgets();
3942
void initOpacitySlider();
43+
void initTranslations();
4044
};

src/main.cpp

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -111,31 +111,60 @@ void configureTranslation(QTranslator& translator, QTranslator& qtTranslator)
111111
bool foundTranslation;
112112
// Configure translations
113113
for (const QString& path : PathInfo::translationsPaths()) {
114-
foundTranslation =
115-
translator.load(QLocale(),
116-
QStringLiteral("Internationalization"),
117-
QStringLiteral("_"),
118-
path);
114+
if (ConfigHandler().uiLanguage() == QStringLiteral("auto")) {
115+
// Load language, which was detected from the system
116+
foundTranslation =
117+
translator.load(QLocale(),
118+
QStringLiteral("Internationalization"),
119+
QStringLiteral("_"),
120+
path);
121+
} else {
122+
// Load language from settings
123+
foundTranslation =
124+
translator.load(QStringLiteral("Internationalization_") +
125+
ConfigHandler().uiLanguage(),
126+
path);
127+
}
119128
if (foundTranslation) {
120129
break;
121130
}
122131
}
123132
if (!foundTranslation) {
124-
QLocale l;
125-
qWarning() << QStringLiteral("No Flameshot translation found for %1")
126-
.arg(l.uiLanguages().join(", "));
133+
if (ConfigHandler().uiLanguage() == QStringLiteral("auto")) {
134+
QLocale l;
135+
qWarning() << QStringLiteral(
136+
"No Flameshot translation found for %1")
137+
.arg(l.uiLanguages().join(", "));
138+
} else {
139+
qWarning() << QStringLiteral(
140+
"No Flameshot translation found for %1")
141+
.arg(ConfigHandler().uiLanguage());
142+
}
127143
}
128144

129-
foundTranslation =
130-
qtTranslator.load(QLocale::system(),
131-
"qt",
132-
"_",
133-
QLibraryInfo::path(QLibraryInfo::TranslationsPath));
145+
if (ConfigHandler().uiLanguage() == QStringLiteral("auto")) {
146+
foundTranslation =
147+
qtTranslator.load(QLocale::system(),
148+
"qt",
149+
"_",
150+
QLibraryInfo::path(QLibraryInfo::TranslationsPath));
151+
} else {
152+
foundTranslation = qtTranslator.load(
153+
QStringLiteral("qt_") + ConfigHandler().uiLanguage(),
154+
155+
QLibraryInfo::path(QLibraryInfo::TranslationsPath));
156+
}
134157
if (!foundTranslation) {
135-
qWarning() << QStringLiteral("No Qt translation found for %1")
136-
.arg(QLocale::languageToString(
137-
QLocale::system().language()));
158+
if (ConfigHandler().uiLanguage() == QStringLiteral("auto")) {
159+
qWarning() << QStringLiteral("No Qt translation found for %1")
160+
.arg(QLocale::languageToString(
161+
QLocale::system().language()));
162+
} else {
163+
qWarning() << QStringLiteral("No Qt translation found for %1")
164+
.arg(ConfigHandler().uiLanguage());
165+
}
138166
}
167+
139168
qApp->installTranslator(&translator);
140169
qApp->installTranslator(&qtTranslator);
141170
}

src/utils/confighandler.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ static QMap<class QString, QSharedPointer<ValueHandler>>
107107
OPTION("uploadHistoryMax" ,LowerBoundedInt ( 0, 25 )),
108108
OPTION("undoLimit" ,BoundedInt ( 0, 999, 100 )),
109109
// Interface tab
110+
OPTION("uiLanguage" ,String ( "auto" )),
110111
OPTION("uiColor" ,Color ( {116, 0, 150} )),
111112
OPTION("contrastUiColor" ,Color ( {39, 0, 50} )),
112113
OPTION("contrastOpacity" ,BoundedInt ( 0, 255, 190 )),

src/utils/confighandler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class ConfigHandler : public QObject
7474
CONFIG_GETTER_SETTER(userColors, setUserColors, QVector<QColor>);
7575
CONFIG_GETTER_SETTER(savePath, setSavePath, QString)
7676
CONFIG_GETTER_SETTER(savePathFixed, setSavePathFixed, bool)
77+
CONFIG_GETTER_SETTER(uiLanguage, setUiLanguage, QString)
7778
CONFIG_GETTER_SETTER(uiColor, setUiColor, QColor)
7879
CONFIG_GETTER_SETTER(contrastUiColor, setContrastUiColor, QColor)
7980
CONFIG_GETTER_SETTER(drawColor, setDrawColor, QColor)

0 commit comments

Comments
 (0)