From 81bc910d68df88365983538933957ccb8de45c0a Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Mon, 12 Jun 2023 14:03:12 +0800 Subject: [PATCH] Provide context to translation strings PR #19120. --- src/app/cmdoptions.cpp | 75 +++++++++++++++++++++--------------------- src/app/main.cpp | 31 ++++++++--------- src/app/upgrade.cpp | 13 ++++---- 3 files changed, 61 insertions(+), 58 deletions(-) diff --git a/src/app/cmdoptions.cpp b/src/app/cmdoptions.cpp index fa80a57dc..b694b0285 100644 --- a/src/app/cmdoptions.cpp +++ b/src/app/cmdoptions.cpp @@ -32,6 +32,7 @@ #include +#include #include #include #include @@ -152,7 +153,7 @@ namespace QStringList parts = arg.split(u'='); if (parts.size() == 2) return Utils::String::unquote(parts[1], u"'\""_qs); - throw CommandLineParameterError(QObject::tr("Parameter '%1' must follow syntax '%1=%2'", + throw CommandLineParameterError(QCoreApplication::translate("CMD Options", "Parameter '%1' must follow syntax '%1=%2'", "e.g. Parameter '--webui-port' must follow syntax '--webui-port=value'") .arg(fullParameter(), u""_qs)); } @@ -203,7 +204,7 @@ namespace const int res = val.toInt(&ok); if (!ok) { - throw CommandLineParameterError(QObject::tr("Parameter '%1' must follow syntax '%1=%2'", + throw CommandLineParameterError(QCoreApplication::translate("CMD Options", "Parameter '%1' must follow syntax '%1=%2'", "e.g. Parameter '--webui-port' must follow syntax '--webui-port='") .arg(fullParameter(), u""_qs)); } @@ -219,7 +220,7 @@ namespace int res = val.toInt(&ok); if (!ok) { - qDebug() << QObject::tr("Expected integer number in environment variable '%1', but got '%2'") + qDebug() << QCoreApplication::translate("CMD Options", "Expected integer number in environment variable '%1', but got '%2'") .arg(envVarName(), val); return defaultValue; } @@ -275,7 +276,7 @@ namespace } } - throw CommandLineParameterError(QObject::tr("Parameter '%1' must follow syntax '%1=%2'", + throw CommandLineParameterError(QCoreApplication::translate("CMD Options", "Parameter '%1' must follow syntax '%1=%2'", "e.g. Parameter '--add-paused' must follow syntax " "'--add-paused='") .arg(fullParameter(), u""_qs)); @@ -302,7 +303,7 @@ namespace return false; } - qDebug() << QObject::tr("Expected %1 in environment variable '%2', but got '%3'") + qDebug() << QCoreApplication::translate("CMD Options", "Expected %1 in environment variable '%2', but got '%3'") .arg(u"true|false"_qs, envVarName(), val); return std::nullopt; } @@ -388,7 +389,7 @@ QBtCommandLineParameters parseCommandLine(const QStringList &args) { result.webUiPort = WEBUI_PORT_OPTION.value(arg); if ((result.webUiPort < 1) || (result.webUiPort > 65535)) - throw CommandLineParameterError(QObject::tr("%1 must specify a valid port (1 to 65535).") + throw CommandLineParameterError(QCoreApplication::translate("CMD Options", "%1 must specify a valid port (1 to 65535).") .arg(u"--webui-port"_qs)); } else if (arg == TORRENTING_PORT_OPTION) @@ -396,7 +397,7 @@ QBtCommandLineParameters parseCommandLine(const QStringList &args) result.torrentingPort = TORRENTING_PORT_OPTION.value(arg); if ((result.torrentingPort < 1) || (result.torrentingPort > 65535)) { - throw CommandLineParameterError(QObject::tr("%1 must specify a valid port (1 to 65535).") + throw CommandLineParameterError(QCoreApplication::translate("CMD Options", "%1 must specify a valid port (1 to 65535).") .arg(u"--torrenting-port"_qs)); } } @@ -499,58 +500,58 @@ QString makeUsage(const QString &prgName) { const QString indentation {USAGE_INDENTATION, u' '}; - const QString text = QObject::tr("Usage:") + u'\n' - + indentation + prgName + u' ' + QObject::tr("[options] [( | )...]") + u'\n' + const QString text = QCoreApplication::translate("CMD Options", "Usage:") + u'\n' + + indentation + prgName + u' ' + QCoreApplication::translate("CMD Options", "[options] [( | )...]") + u'\n' - + QObject::tr("Options:") + u'\n' + + QCoreApplication::translate("CMD Options", "Options:") + u'\n' #if !defined(Q_OS_WIN) || defined(DISABLE_GUI) - + SHOW_VERSION_OPTION.usage() + wrapText(QObject::tr("Display program version and exit")) + u'\n' + + SHOW_VERSION_OPTION.usage() + wrapText(QCoreApplication::translate("CMD Options", "Display program version and exit")) + u'\n' #endif - + SHOW_HELP_OPTION.usage() + wrapText(QObject::tr("Display this help message and exit")) + u'\n' - + WEBUI_PORT_OPTION.usage(QObject::tr("port")) - + wrapText(QObject::tr("Change the Web UI port")) + + SHOW_HELP_OPTION.usage() + wrapText(QCoreApplication::translate("CMD Options", "Display this help message and exit")) + u'\n' + + WEBUI_PORT_OPTION.usage(QCoreApplication::translate("CMD Options", "port")) + + wrapText(QCoreApplication::translate("CMD Options", "Change the Web UI port")) + u'\n' - + TORRENTING_PORT_OPTION.usage(QObject::tr("port")) - + wrapText(QObject::tr("Change the torrenting port")) + + TORRENTING_PORT_OPTION.usage(QCoreApplication::translate("CMD Options", "port")) + + wrapText(QCoreApplication::translate("CMD Options", "Change the torrenting port")) + u'\n' #ifndef DISABLE_GUI - + NO_SPLASH_OPTION.usage() + wrapText(QObject::tr("Disable splash screen")) + u'\n' + + NO_SPLASH_OPTION.usage() + wrapText(QCoreApplication::translate("CMD Options", "Disable splash screen")) + u'\n' #elif !defined(Q_OS_WIN) - + DAEMON_OPTION.usage() + wrapText(QObject::tr("Run in daemon-mode (background)")) + u'\n' + + DAEMON_OPTION.usage() + wrapText(QCoreApplication::translate("CMD Options", "Run in daemon-mode (background)")) + u'\n' #endif //: Use appropriate short form or abbreviation of "directory" - + PROFILE_OPTION.usage(QObject::tr("dir")) - + wrapText(QObject::tr("Store configuration files in ")) + u'\n' - + CONFIGURATION_OPTION.usage(QObject::tr("name")) - + wrapText(QObject::tr("Store configuration files in directories qBittorrent_")) + u'\n' + + PROFILE_OPTION.usage(QCoreApplication::translate("CMD Options", "dir")) + + wrapText(QCoreApplication::translate("CMD Options", "Store configuration files in ")) + u'\n' + + CONFIGURATION_OPTION.usage(QCoreApplication::translate("CMD Options", "name")) + + wrapText(QCoreApplication::translate("CMD Options", "Store configuration files in directories qBittorrent_")) + u'\n' + RELATIVE_FASTRESUME.usage() - + wrapText(QObject::tr("Hack into libtorrent fastresume files and make file paths relative " + + wrapText(QCoreApplication::translate("CMD Options", "Hack into libtorrent fastresume files and make file paths relative " "to the profile directory")) + u'\n' - + Option::padUsageText(QObject::tr("files or URLs")) - + wrapText(QObject::tr("Download the torrents passed by the user")) + u'\n' + + Option::padUsageText(QCoreApplication::translate("CMD Options", "files or URLs")) + + wrapText(QCoreApplication::translate("CMD Options", "Download the torrents passed by the user")) + u'\n' + u'\n' - + wrapText(QObject::tr("Options when adding new torrents:"), 0) + u'\n' - + SAVE_PATH_OPTION.usage(QObject::tr("path")) + wrapText(QObject::tr("Torrent save path")) + u'\n' - + PAUSED_OPTION.usage() + wrapText(QObject::tr("Add torrents as started or paused")) + u'\n' - + SKIP_HASH_CHECK_OPTION.usage() + wrapText(QObject::tr("Skip hash check")) + u'\n' - + CATEGORY_OPTION.usage(QObject::tr("name")) - + wrapText(QObject::tr("Assign torrents to category. If the category doesn't exist, it will be " + + wrapText(QCoreApplication::translate("CMD Options", "Options when adding new torrents:"), 0) + u'\n' + + SAVE_PATH_OPTION.usage(QCoreApplication::translate("CMD Options", "path")) + wrapText(QCoreApplication::translate("CMD Options", "Torrent save path")) + u'\n' + + PAUSED_OPTION.usage() + wrapText(QCoreApplication::translate("CMD Options", "Add torrents as started or paused")) + u'\n' + + SKIP_HASH_CHECK_OPTION.usage() + wrapText(QCoreApplication::translate("CMD Options", "Skip hash check")) + u'\n' + + CATEGORY_OPTION.usage(QCoreApplication::translate("CMD Options", "name")) + + wrapText(QCoreApplication::translate("CMD Options", "Assign torrents to category. If the category doesn't exist, it will be " "created.")) + u'\n' - + SEQUENTIAL_OPTION.usage() + wrapText(QObject::tr("Download files in sequential order")) + u'\n' + + SEQUENTIAL_OPTION.usage() + wrapText(QCoreApplication::translate("CMD Options", "Download files in sequential order")) + u'\n' + FIRST_AND_LAST_OPTION.usage() - + wrapText(QObject::tr("Download first and last pieces first")) + u'\n' + + wrapText(QCoreApplication::translate("CMD Options", "Download first and last pieces first")) + u'\n' + SKIP_DIALOG_OPTION.usage() - + wrapText(QObject::tr("Specify whether the \"Add New Torrent\" dialog opens when adding a " + + wrapText(QCoreApplication::translate("CMD Options", "Specify whether the \"Add New Torrent\" dialog opens when adding a " "torrent.")) + u'\n' + u'\n' - + wrapText(QObject::tr("Option values may be supplied via environment variables. For option named " + + wrapText(QCoreApplication::translate("CMD Options", "Option values may be supplied via environment variables. For option named " "'parameter-name', environment variable name is 'QBT_PARAMETER_NAME' (in upper " "case, '-' replaced with '_'). To pass flag values, set the variable to '1' or " "'TRUE'. For example, to disable the splash screen: "), 0) + u'\n' + u"QBT_NO_SPLASH=1 " + prgName + u'\n' - + wrapText(QObject::tr("Command line parameters take precedence over environment variables"), 0) + u'\n'; + + wrapText(QCoreApplication::translate("CMD Options", "Command line parameters take precedence over environment variables"), 0) + u'\n'; return text; } @@ -558,7 +559,7 @@ QString makeUsage(const QString &prgName) void displayUsage(const QString &prgName) { #if defined(Q_OS_WIN) && !defined(DISABLE_GUI) - QMessageBox msgBox(QMessageBox::Information, QObject::tr("Help"), makeUsage(prgName), QMessageBox::Ok); + QMessageBox msgBox(QMessageBox::Information, QCoreApplication::translate("CMD Options", "Help"), makeUsage(prgName), QMessageBox::Ok); msgBox.show(); // Need to be shown or to moveToCenter does not work msgBox.move(Utils::Gui::screenCenter(&msgBox)); msgBox.exec(); diff --git a/src/app/main.cpp b/src/app/main.cpp index 86350dcf1..1ed90cfe3 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -45,6 +45,7 @@ #include #endif +#include #include #include @@ -132,7 +133,7 @@ int main(int argc, char *argv[]) const QBtCommandLineParameters params = app->commandLineArgs(); if (!params.unknownParameter.isEmpty()) { - throw CommandLineParameterError(QObject::tr("%1 is an unknown command line parameter.", + throw CommandLineParameterError(QCoreApplication::translate("Main", "%1 is an unknown command line parameter.", "--random-parameter is an unknown command line parameter.") .arg(params.unknownParameter)); } @@ -144,7 +145,7 @@ int main(int argc, char *argv[]) displayVersion(); return EXIT_SUCCESS; } - throw CommandLineParameterError(QObject::tr("%1 must be the single command line parameter.") + throw CommandLineParameterError(QCoreApplication::translate("Main", "%1 must be the single command line parameter.") .arg(u"-v (or --version)"_qs)); } #endif @@ -155,7 +156,7 @@ int main(int argc, char *argv[]) displayUsage(QString::fromLocal8Bit(argv[0])); return EXIT_SUCCESS; } - throw CommandLineParameterError(QObject::tr("%1 must be the single command line parameter.") + throw CommandLineParameterError(QCoreApplication::translate("Main", "%1 must be the single command line parameter.") .arg(u"-h (or --help)"_qs)); } @@ -187,7 +188,7 @@ int main(int argc, char *argv[]) #if defined(DISABLE_GUI) && !defined(Q_OS_WIN) if (params.shouldDaemonize) { - throw CommandLineParameterError(QObject::tr("You cannot use %1: qBittorrent is already running for this user.") + throw CommandLineParameterError(QCoreApplication::translate("Main", "You cannot use %1: qBittorrent is already running for this user.") .arg(u"-d (or --daemon)"_qs)); } #endif @@ -295,15 +296,15 @@ void displayVersion() void displayBadArgMessage(const QString &message) { - const QString help = QObject::tr("Run application with -h option to read about command line parameters."); + const QString help = QCoreApplication::translate("Main", "Run application with -h option to read about command line parameters."); #if defined(Q_OS_WIN) && !defined(DISABLE_GUI) - QMessageBox msgBox(QMessageBox::Critical, QObject::tr("Bad command line"), + QMessageBox msgBox(QMessageBox::Critical, QCoreApplication::translate("Main", "Bad command line"), (message + u'\n' + help), QMessageBox::Ok); msgBox.show(); // Need to be shown or to moveToCenter does not work msgBox.move(Utils::Gui::screenCenter(&msgBox)); msgBox.exec(); #else - const QString errMsg = QObject::tr("Bad command line: ") + u'\n' + const QString errMsg = QCoreApplication::translate("Main", "Bad command line: ") + u'\n' + message + u'\n' + help + u'\n'; fprintf(stderr, "%s", qUtf8Printable(errMsg)); @@ -316,10 +317,10 @@ bool userAgreesWithLegalNotice() Q_ASSERT(!pref->getAcceptedLegal()); #ifdef DISABLE_GUI - const QString eula = u"\n*** %1 ***\n"_qs.arg(QObject::tr("Legal Notice")) - + QObject::tr("qBittorrent is a file sharing program. When you run a torrent, its data will be made available to others by means of upload. Any content you share is your sole responsibility.") + u"\n\n" - + QObject::tr("No further notices will be issued.") + u"\n\n" - + QObject::tr("Press %1 key to accept and continue...").arg(u"'y'"_qs) + u'\n'; + const QString eula = u"\n*** %1 ***\n"_qs.arg(QCoreApplication::translate("Main", "Legal Notice")) + + QCoreApplication::translate("Main", "qBittorrent is a file sharing program. When you run a torrent, its data will be made available to others by means of upload. Any content you share is your sole responsibility.") + u"\n\n" + + QCoreApplication::translate("Main", "No further notices will be issued.") + u"\n\n" + + QCoreApplication::translate("Main", "Press %1 key to accept and continue...").arg(u"'y'"_qs) + u'\n'; printf("%s", qUtf8Printable(eula)); const char ret = getchar(); // Read pressed key @@ -331,10 +332,10 @@ bool userAgreesWithLegalNotice() } #else QMessageBox msgBox; - msgBox.setText(QObject::tr("qBittorrent is a file sharing program. When you run a torrent, its data will be made available to others by means of upload. Any content you share is your sole responsibility.\n\nNo further notices will be issued.")); - msgBox.setWindowTitle(QObject::tr("Legal notice")); - msgBox.addButton(QObject::tr("Cancel"), QMessageBox::RejectRole); - const QAbstractButton *agreeButton = msgBox.addButton(QObject::tr("I Agree"), QMessageBox::AcceptRole); + msgBox.setText(QCoreApplication::translate("Main", "qBittorrent is a file sharing program. When you run a torrent, its data will be made available to others by means of upload. Any content you share is your sole responsibility.\n\nNo further notices will be issued.")); + msgBox.setWindowTitle(QCoreApplication::translate("Main", "Legal notice")); + msgBox.addButton(QCoreApplication::translate("Main", "Cancel"), QMessageBox::RejectRole); + const QAbstractButton *agreeButton = msgBox.addButton(QCoreApplication::translate("Main", "I Agree"), QMessageBox::AcceptRole); msgBox.show(); // Need to be shown or to moveToCenter does not work msgBox.move(Utils::Gui::screenCenter(&msgBox)); msgBox.exec(); diff --git a/src/app/upgrade.cpp b/src/app/upgrade.cpp index 2cdb52260..ed5e2cae9 100644 --- a/src/app/upgrade.cpp +++ b/src/app/upgrade.cpp @@ -29,6 +29,7 @@ #include "upgrade.h" #include +#include #include #include "base/bittorrent/torrentcontentlayout.h" @@ -54,7 +55,7 @@ namespace SettingsStorage *settingsStorage {SettingsStorage::instance()}; const auto oldData {settingsStorage->loadValue(oldKey)}; const auto newData {settingsStorage->loadValue(newKey)}; - const QString errorMsgFormat {QObject::tr("Migrate preferences failed: WebUI https, file: \"%1\", error: \"%2\"")}; + const QString errorMsgFormat {QCoreApplication::translate("Upgrade", "Migrate preferences failed: WebUI https, file: \"%1\", error: \"%2\"")}; if (!newData.isEmpty() || oldData.isEmpty()) return; @@ -69,7 +70,7 @@ namespace settingsStorage->storeValue(newKey, savePath); settingsStorage->removeValue(oldKey); - LogMsg(QObject::tr("Migrated preferences: WebUI https, exported data to file: \"%1\"").arg(savePath.toString()) + LogMsg(QCoreApplication::translate("Upgrade", "Migrated preferences: WebUI https, exported data to file: \"%1\"").arg(savePath.toString()) , Log::INFO); }; @@ -161,7 +162,7 @@ namespace settingsStorage->storeValue(key, Scheduler::Days::Sunday); break; default: - LogMsg(QObject::tr("Invalid value found in configuration file, reverting it to default. Key: \"%1\". Invalid value: \"%2\".") + LogMsg(QCoreApplication::translate("Upgrade", "Invalid value found in configuration file, reverting it to default. Key: \"%1\". Invalid value: \"%2\".") .arg(key, QString::number(number)), Log::WARNING); settingsStorage->removeValue(key); break; @@ -192,7 +193,7 @@ namespace settingsStorage->storeValue(key, DNS::Service::NoIP); break; default: - LogMsg(QObject::tr("Invalid value found in configuration file, reverting it to default. Key: \"%1\". Invalid value: \"%2\".") + LogMsg(QCoreApplication::translate("Upgrade", "Invalid value found in configuration file, reverting it to default. Key: \"%1\". Invalid value: \"%2\".") .arg(key, QString::number(number)), Log::WARNING); settingsStorage->removeValue(key); break; @@ -223,7 +224,7 @@ namespace settingsStorage->storeValue(key, TrayIcon::Style::MonoLight); break; default: - LogMsg(QObject::tr("Invalid value found in configuration file, reverting it to default. Key: \"%1\". Invalid value: \"%2\".") + LogMsg(QCoreApplication::translate("Upgrade", "Invalid value found in configuration file, reverting it to default. Key: \"%1\". Invalid value: \"%2\".") .arg(key, QString::number(number)), Log::WARNING); settingsStorage->removeValue(key); break; @@ -361,7 +362,7 @@ namespace settingsStorage->storeValue(key, Net::ProxyType::SOCKS4); break; default: - LogMsg(QObject::tr("Invalid value found in configuration file, reverting it to default. Key: \"%1\". Invalid value: \"%2\".") + LogMsg(QCoreApplication::translate("Upgrade", "Invalid value found in configuration file, reverting it to default. Key: \"%1\". Invalid value: \"%2\".") .arg(key, QString::number(number)), Log::WARNING); settingsStorage->removeValue(key); break;