// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: BSD-2-Clause // // The language list the settings page offers, which is read off the catalog // directory rather than written out in QML. Nothing here touches a homeserver or // a window: a directory of empty files is a catalog directory as far as the // search is concerned, and that is the whole of what it reads. #include "core/i18n/TranslationCatalog.h" #include #include #include #include namespace { // a catalog is a file of a name in a place; what is inside it is not this // search's business bool makeCatalog(const QString &dir, const QString &code, const QString &subdir) { const QDir root(dir); const QString path = root.absoluteFilePath(code + subdir + QStringLiteral("koutnet.mo")); if (!QFileInfo(path).absoluteDir().mkpath(QStringLiteral("."))) return false; QFile file(path); return file.open(QIODevice::WriteOnly) && file.write("x") > 0; } } // namespace class TranslationCatalogTest : public QObject { Q_OBJECT private Q_SLOTS: void initTestCase() { // Test mode moves the cache and the rest out of the user's home, and the // fallback path is built off the executable's own directory, so the // search cannot be left pointing at a real installation. QCoreApplication::setOrganizationDomain(QStringLiteral("koutnet.test")); QStandardPaths::setTestModeEnabled(true); } // A code with a catalog under it is a language on the list, and a code // without one is not: this is the whole difference between scanning a // directory and writing the list out by hand. void listsOnlyCodesWithACatalog() { QTemporaryDir dir; QVERIFY(dir.isValid()); const QString root = dir.path(); QVERIFY(makeCatalog(root, QStringLiteral("ru"), QStringLiteral("/LC_MESSAGES/"))); QVERIFY(makeCatalog(root, QStringLiteral("de"), QStringLiteral("/LC_MESSAGES/"))); QVERIFY(QDir(root).mkdir(QStringLiteral("fr"))); // a directory, no catalog const QStringList codes = TranslationCatalog::localeCodesIn(root); QCOMPARE(codes, QStringList({QStringLiteral("de"), QStringLiteral("ru")})); } // A catalog put straight under the code, with no LC_MESSAGES in the way, is // still a catalog: the layout is what gettext happens to use, not a rule // worth refusing a translation over. void acceptsCatalogWithoutTheGettextSubdirectory() { QTemporaryDir dir; QVERIFY(dir.isValid()); QVERIFY(makeCatalog(dir.path(), QStringLiteral("uk"), QStringLiteral("/"))); QCOMPARE(TranslationCatalog::localeCodesIn(dir.path()), QStringList({QStringLiteral("uk")})); } void aDirectoryThatIsNotThereHasNothingInIt() { QCOMPARE(TranslationCatalog::localeCodesIn(QStringLiteral("/nonexistent/koutnet/locale")), QStringList()); } // The name a code is offered under is the language's own, in its own // spelling and with a capital, and not this program's: a person choosing a // language looks for their own name for it. English is one language with one // name, so it is not the American variant of it - QLocale("en") resolves to // en_US, whose own name is "American English", and that is a country // talking about itself rather than a language in the list. void namesAreTheLanguagesOwn() { QCOMPARE(TranslationCatalog::displayNameFor(QStringLiteral("ru")), QString::fromUtf8("Русский")); QCOMPARE(TranslationCatalog::displayNameFor(QStringLiteral("de")), QStringLiteral("Deutsch")); QCOMPARE(TranslationCatalog::displayNameFor(QStringLiteral("uk")), QString::fromUtf8("Українська")); QCOMPARE(TranslationCatalog::displayNameFor(QStringLiteral("en")), QStringLiteral("English")); QCOMPARE(TranslationCatalog::displayNameFor(QStringLiteral("fr")), QString::fromUtf8("Français")); // Lowercase in the CLDR data, and a name is written with a capital. QCOMPARE(TranslationCatalog::displayNameFor(QStringLiteral("pt")), QString::fromUtf8("Português")); } // A code naming a country says which, in that country's own spelling - and // a country named inside the language name comes off it, because the // language is what is being chosen: CLDR's "español de España" is Spanish. void aCountryIsSaidInItsOwnSpelling() { QCOMPARE(TranslationCatalog::displayNameFor(QStringLiteral("pt_BR")), QString::fromUtf8("Português (Brasil)")); QCOMPARE(TranslationCatalog::displayNameFor(QStringLiteral("es")), QString::fromUtf8("Español")); // A language whose name is its own country is not truncated into // nothing: the two are the same string and the name wins. QVERIFY(!TranslationCatalog::displayNameFor(QStringLiteral("th")).isEmpty()); } // An unknown code is shown as itself rather than as nothing at all: an empty // row in a list of languages is a bug report waiting to happen. void anUnknownCodeIsItsOwnName() { QCOMPARE(TranslationCatalog::displayNameFor(QStringLiteral("zz")), QStringLiteral("zz")); } }; QTEST_MAIN(TranslationCatalogTest) #include "TranslationCatalogTest.moc"