1
0
Fork 0

Launcher respects --language arg if set

This commit is contained in:
James Turner 2018-10-12 11:37:39 +01:00
parent 2b1913e118
commit f819750287
3 changed files with 47 additions and 4 deletions

View file

@ -281,7 +281,7 @@ void initApp(int& argc, char** argv, bool doInitQSettings)
QStringLiteral("org.flightgear.FlightGear.desktop")); QStringLiteral("org.flightgear.FlightGear.desktop"));
#endif #endif
QTranslator* fallbackTranslator = new QTranslator(static_qApp.get()); QTranslator* fallbackTranslator = new QTranslator(static_qApp.get());
if (!fallbackTranslator->load(QLatin1String(":/FlightGear_en_US.qm"))) { if (!fallbackTranslator->load(QLatin1String(":/FlightGear_en.qm"))) {
qWarning() << "Failed to load default (en) translations"; qWarning() << "Failed to load default (en) translations";
delete fallbackTranslator; delete fallbackTranslator;
} else { } else {
@ -289,7 +289,19 @@ void initApp(int& argc, char** argv, bool doInitQSettings)
} }
QTranslator* translator = new QTranslator(static_qApp.get()); QTranslator* translator = new QTranslator(static_qApp.get());
if (translator->load(QLocale(), QLatin1String("FlightGear"), QLatin1String("_"), QLatin1String(":/"))) { // check for --langauge=xx option and prefer that over QLocale
// detection of the locale if it exists
auto lang = Options::getArgValue(argc, argv, "--language");
if (!lang.empty()) {
QString localeFile = "FlightGear_" + QString::fromStdString(lang);
if (translator->load(localeFile, QLatin1String(":/"))) {
qInfo() << "Loaded translations based on --language from:" << localeFile;
static_qApp->installTranslator(translator);
} else {
qInfo() << "--langauge was set, but no translations found at:" << localeFile;
delete translator;
}
} else if (translator->load(QLocale(), QLatin1String("FlightGear"), QLatin1String("_"), QLatin1String(":/"))) {
// QLocale().name() looks like ' "it_IT" ' (without the outer // QLocale().name() looks like ' "it_IT" ' (without the outer
// quotes) when running FG on Linux with LANG=it_IT.UTF-8. // quotes) when running FG on Linux with LANG=it_IT.UTF-8.
qInfo() << "Loaded translations for locale" << QLocale().name(); qInfo() << "Loaded translations for locale" << QLocale().name();

View file

@ -251,7 +251,7 @@ void fgSetDefaults ()
v->setValueReadOnly("build-type", FG_BUILD_TYPE); v->setValueReadOnly("build-type", FG_BUILD_TYPE);
char* envp = ::getenv( "http_proxy" ); char* envp = ::getenv( "http_proxy" );
if( envp != NULL ) if( envp != nullptr )
fgSetupProxy( envp ); fgSetupProxy( envp );
} }
@ -2960,7 +2960,7 @@ bool Options::checkForArg(int argc, char* argv[], const char* checkArg)
{ {
for (int i = 0; i < argc; ++i) { for (int i = 0; i < argc; ++i) {
char* arg = argv[i]; char* arg = argv[i];
if (arg == 0) { if (arg == nullptr) {
continue; continue;
} }
@ -2981,4 +2981,24 @@ bool Options::checkForArg(int argc, char* argv[], const char* checkArg)
return false; return false;
} }
std::string Options::getArgValue(int argc, char* argv[], const char* checkArg)
{
const auto len = strlen(checkArg);
for (int i = 0; i < argc; ++i) {
char* arg = argv[i];
if (arg == nullptr) {
continue;
}
if (strncmp(arg, checkArg, len) == 0) {
const auto alen = strlen(arg);
if ((alen - len) < 2)
return {}; // no value after the =, or missing = entirely
return std::string(arg + len + 1);
}
} // of args iteration
return {};
}
} // of namespace flightgear } // of namespace flightgear

View file

@ -155,6 +155,17 @@ public:
*/ */
static bool checkForArg(int argc, char* argv[], const char* arg); static bool checkForArg(int argc, char* argv[], const char* arg);
/**
* @brief getArgValue - get the value of an argument if it exists, or
* an empty string otherwise
* @param argc
* @param argv
* @param checkArg : arg to look for, with '--' prefix
* @return value following '=' until the next white space
*/
static std::string getArgValue(int argc, char* argv[], const char* checkArg);
SGPath platformDefaultRoot() const; SGPath platformDefaultRoot() const;
/** /**