[Translation] Name languages without reading the translation files (#7662)
Some checks are pending
CodeQL / Analyze (cpp) (push) Waiting to run
CodeQL / Analyze (actions) (push) Waiting to run
Build Desktop / Configure (push) Waiting to run
Build Desktop / Debian 13 (push) Blocked by required conditions
Build Desktop / Debian 12 (push) Blocked by required conditions
Build Desktop / Fedora 44 (push) Blocked by required conditions
Build Desktop / Fedora 43 (push) Blocked by required conditions
Build Desktop / Servatrice_Debian 12 (push) Blocked by required conditions
Build Desktop / Ubuntu 24.04 (push) Blocked by required conditions
Build Desktop / Ubuntu 26.04 (push) Blocked by required conditions
Build Desktop / Arch (push) Blocked by required conditions
Build Desktop / Ubuntu 26.04 cover (push) Blocked by required conditions
Build Desktop / macOS 13 Intel (push) Blocked by required conditions
Build Desktop / macOS 14 (push) Blocked by required conditions
Build Desktop / macOS 15 (push) Blocked by required conditions
Build Desktop / macOS 26 Debug (push) Blocked by required conditions
Build Desktop / Windows 10 (push) Blocked by required conditions
Build Desktop / Prune caches (push) Waiting to run
Build Docker / Servatrice (arm) (push) Waiting to run
Build Docker / Servatrice (x86) (push) Waiting to run
Build Docker / Publish multi-platform Servatrice image (push) Blocked by required conditions

* [Translation] Name languages without reading the translation files

The language selector derived every label from the language's own .qm file:
it loaded the file and translated the literal "English" in the "i18n"
context. lupdate could never extract that literal from the callers, which
pass the DEFAULT_LANG_NAME macro, so a dummy QT_TRANSLATE_NOOP in
cache_settings.cpp existed to give it something to extract. #7050 deleted
that dummy, the next lupdate push dropped the string from Transifex, and
the following pull rewrote the .ts files without it. lrelease only emits
translated messages, so 22 of 25 .qm files returned an empty name.

The names are data, not translations: a selector has to be readable before
any translation is loaded, and a label must not depend on the translation
of the language it names. TranslationLoader::languageDisplayName now holds
them next to availableLanguages, falling back to Qt's native language name
and then to the code, so a language added later still gets a label.

Also select the configured language by item data instead of by matching the
translated "English" string, which pointed at the wrong row whenever the
lookup came back empty, and restore that English entry's own name: en_US is
"English (United States)" so it can no longer be confused with the
untranslated default.

Oracle shared the code path, so its wizard language page is fixed too.

* [Translation] Name the default language selection once

Review follow-up: log the unresolvable language through the page's own
category, and move the en_US entry both selectors fall back to for an unset
setting into TranslationLoader::defaultLanguageCode.

---------

Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
This commit is contained in:
BruebachL 2026-10-26 02:39:26 +01:00 • committed by GitHub
parent 858b270fa9
commit 675e1f7447
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 157 additions and 51 deletions

View file

@ -2,6 +2,7 @@
#include <QByteArray>
#include <QCoreApplication>
#include <QDir>
#include <QFileInfo>
#include <QLibraryInfo>
#include <QList>
#include <QSet>
@ -19,6 +20,11 @@
#define TRANSLATION_LOADER_TEST_DIR ""
#endif
// The client's translation files, whose language codes are the languages the language selector offers.
#ifndef COCKATRICE_TRANSLATIONS_DIR
#define COCKATRICE_TRANSLATIONS_DIR ""
#endif
namespace
{
const QString testContext = QStringLiteral("TranslationLoaderTest");
@ -233,6 +239,49 @@ TEST(TranslationLoaderTest, AvailableLanguagesListsTheTranslationsThatCanBeLoade
EXPECT_TRUE(TranslationLoader::availableLanguages(QStringLiteral("translation_loader_test"), {}).isEmpty());
}
TEST(TranslationLoaderTest, LanguageDisplayNamesDoNotNeedATranslationFile)
{
// The regression this guards: the selector took every label from the language's own .qm file, so it
// went blank as soon as translators had not translated the "English" placeholder in the i18n context.
EXPECT_EQ(QStringLiteral("Deutsch (German)"), TranslationLoader::languageDisplayName(QStringLiteral("de")));
EXPECT_EQ(QStringLiteral("简体中文 (Chinese Simplified)"),
TranslationLoader::languageDisplayName(QStringLiteral("zh-Hans")));
EXPECT_EQ(QStringLiteral("English, arr! (Pirate English)"),
TranslationLoader::languageDisplayName(QStringLiteral("en@pirate")));
}
TEST(TranslationLoaderTest, EveryLanguageTheClientShipsHasItsOwnDisplayName)
{
// The shipped translation files are what the selector offers, so each of them has to end up with a
// label: an empty one is what made the dropdown look broken, and two equal ones are a coin flip.
const QDir translations(QStringLiteral(COCKATRICE_TRANSLATIONS_DIR));
const QStringList files = translations.entryList({QStringLiteral("cockatrice_*.ts")}, QDir::Files);
ASSERT_FALSE(files.isEmpty());
QStringList languages;
QSet<QString> displayNames;
for (const QString &file : files) {
const QString language = QFileInfo(file).completeBaseName().section(QLatin1Char('_'), 1);
const QString displayName = TranslationLoader::languageDisplayName(language);
EXPECT_FALSE(displayName.isEmpty()) << language.toStdString();
languages << language;
displayNames.insert(displayName);
}
EXPECT_EQ(files.size(), displayNames.size());
// The selector preselects this one for an unset setting, so it has to be offered at all.
EXPECT_TRUE(languages.contains(TranslationLoader::defaultLanguageCode()));
}
TEST(TranslationLoaderTest, UnknownLanguagesFallBackToTheNativeNameThenTheCode)
{
// A language added after the table, and a code Qt does not know either.
EXPECT_FALSE(TranslationLoader::languageDisplayName(QStringLiteral("sv_FI")).isEmpty());
EXPECT_EQ(QStringLiteral("zz_QQ"), TranslationLoader::languageDisplayName(QStringLiteral("zz_QQ")));
EXPECT_TRUE(TranslationLoader::languageDisplayName(QString()).isEmpty());
}
TEST(TranslationLoaderTest, LoadApplicationReportsAMissingTranslationDirectory)
{
QTemporaryDir emptyDir;