Skip to content

Commit 1998d33

Browse files
nilsdingmgallien
authored andcommitted
feat: allow overriding the language through the configuration
e.g. this config entry: ```ini ; nextcloud.cfg [General] language=de ``` will ensure that the application will always be translated to German. It's also possible to set the configuration option from the command line: ```sh # set the language to German for the next client startup ./nextcloud --set-language de # set the language to the OS default for the next client startup ./nextcloud --set-language "" ``` partial fix for #111, as this is not a GUI option Signed-off-by: Jyrki Gadinger <nilsding@nilsding.org> # Date: Mon Mar 31 12:44:55 2025 +0200 # # On branch feature/enforce-translations # Your branch and 'origin/feature/enforce-translations' have diverged, # and have 1 and 1 different commits each, respectively. # # Changes to be committed: # modified: src/gui/application.cpp # modified: src/gui/application.h # modified: src/libsync/configfile.cpp # modified: src/libsync/configfile.h # # Untracked files: # .kateproject.build # metainfo.po # test.metainfo.xml #
1 parent 75f9028 commit 1998d33

4 files changed

Lines changed: 42 additions & 1 deletion

File tree

src/gui/application.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ namespace {
9797
" --overridelocaldir : specify a local dir to be used in the account setup wizard.\n"
9898
" --userid : userId (username as on the server) to pass when creating an account via command-line.\n"
9999
" --apppassword : appPassword to pass when creating an account via command-line.\n"
100+
" --set-language <lang> : specify a language to use for the client, regardless of the OS language.\n"
100101
" --localdirpath : (optional) path where to create a local sync folder when creating an account via command-line.\n"
101102
" --isvfsenabled : whether to set a VFS or non-VFS folder (1 for 'yes' or 0 for 'no') when creating an account via command-line.\n"
102103
" --remotedirpath : (optional) path to a remote subfolder when creating an account via command-line.\n"
@@ -362,6 +363,12 @@ Application::Application(int &argc, char **argv)
362363
shouldExit = true;
363364
}
364365

366+
if (!_setLanguage.isNull()) {
367+
// checking for .isNull here as setting the value to an empty string will use the OS language again
368+
cfg.setLanguage(_setLanguage);
369+
shouldExit = true;
370+
}
371+
365372
if (AccountSetupCommandLineManager::instance()) {
366373
cfg.setVfsEnabled(AccountSetupCommandLineManager::instance()->isVfsEnabled());
367374
}
@@ -881,6 +888,10 @@ void Application::parseOptions(const QStringList &options)
881888
} else {
882889
showHint("Invalid URL passed to --overridelocaldir");
883890
}
891+
} else if (option == QStringLiteral("--set-language")) {
892+
if (it.hasNext() && !it.peekNext().startsWith(QLatin1String("--"))) {
893+
_setLanguage = it.next();
894+
}
884895
} else if (option == QStringLiteral("--forcelegacyconfigimport")) {
885896
AccountManager::instance()->setForceLegacyImport(true);
886897
} else {
@@ -1001,10 +1012,22 @@ QString substLang(const QString &lang)
10011012
return lang;
10021013
}
10031014

1015+
QString enforcedLanguage()
1016+
{
1017+
const ConfigFile cfg;
1018+
const auto configLanguage = cfg.language();
1019+
if (!configLanguage.isEmpty()) {
1020+
// always prefer value from configuration
1021+
return configLanguage;
1022+
}
1023+
1024+
return Theme::instance()->enforcedLocale();
1025+
}
1026+
10041027
void Application::setupTranslations()
10051028
{
10061029
qCInfo(lcApplication) << "System UI languages are:" << QLocale::system().uiLanguages();
1007-
const auto enforcedLocale = Theme::instance()->enforcedLocale();
1030+
const auto enforcedLocale = enforcedLanguage();
10081031
const auto lang = substLang(!enforcedLocale.isEmpty() ? enforcedLocale : QLocale::system().uiLanguages(QLocale::TagSeparator::Underscore).first());
10091032
qCInfo(lcApplication) << "selected application language:" << lang;
10101033

src/gui/application.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ protected slots:
150150

151151
QString _overrideServerUrl;
152152
QString _overrideLocalDir;
153+
QString _setLanguage;
153154

154155
#if defined(WITH_CRASHREPORTER)
155156
QScopedPointer<CrashReporter::Handler> _crashHandler;

src/libsync/configfile.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ static const QString defaultUpdateChannelName = "stable";
115115
static const QStringList enterpriseUpdateChannelsList { QStringLiteral("stable"), QStringLiteral("enterprise") };
116116
static const QString defaultEnterpriseChannel = "enterprise";
117117

118+
static constexpr char languageC[] = "language";
119+
118120
static constexpr int deleteFilesThresholdDefaultValue = 100;
119121
}
120122

@@ -1252,6 +1254,17 @@ void ConfigFile::setDesktopEnterpriseChannel(const QString &channel)
12521254
settings.setValue(QLatin1String(desktopEnterpriseChannelName), channel);
12531255
}
12541256

1257+
QString ConfigFile::language() const
1258+
{
1259+
QSettings settings(configFile(), QSettings::IniFormat);
1260+
return settings.value(QLatin1String(languageC), QLatin1String("")).toString();
1261+
}
1262+
1263+
void ConfigFile::setLanguage(const QString& language)
1264+
{
1265+
QSettings settings(configFile(), QSettings::IniFormat);
1266+
settings.setValue(QLatin1String(languageC), language);
1267+
}
12551268

12561269
Q_GLOBAL_STATIC(QString, g_configFileName)
12571270

src/libsync/configfile.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ class OWNCLOUDSYNC_EXPORT ConfigFile
241241
[[nodiscard]] QString desktopEnterpriseChannel() const;
242242
void setDesktopEnterpriseChannel(const QString &channel);
243243

244+
/// Enforce a specific language used for the UI
245+
[[nodiscard]] QString language() const;
246+
void setLanguage(const QString &language);
247+
244248
/** Returns a new settings pre-set in a specific group. The Settings will be created
245249
with the given parent. If no parent is specified, the caller must destroy the settings */
246250
static std::unique_ptr<QSettings> settingsWithGroup(const QString &group, QObject *parent = nullptr);

0 commit comments

Comments
 (0)