// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL #include "TranslationCatalog.h" #include #include #include #include #include #include #include #include QStringList TranslationCatalog::localeCodesIn(const QString &dir) { const QDir root(dir); if (!root.exists()) return {}; QStringList codes; const QStringList entries = root.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name); for (const QString &code : entries) { if (QFile::exists(root.absoluteFilePath(code + QStringLiteral("/LC_MESSAGES/koutnet.mo"))) || QFile::exists(root.absoluteFilePath(code + QStringLiteral("/koutnet.mo")))) { codes << code; } } return codes; } QString TranslationCatalog::localeDir() { const QString exePath = QCoreApplication::applicationDirPath(); const QStringList candidates = { exePath + QStringLiteral("/share/locale"), exePath + QStringLiteral("/locale"), exePath + QStringLiteral("/../src/locale"), exePath + QStringLiteral("/../share/locale"), qEnvironmentVariable("APPDIR") + QStringLiteral("/usr/share/locale"), QStringLiteral("/usr/share/locale"), }; for (const QString &candidate : candidates) { if (!localeCodesIn(candidate).isEmpty()) return candidate; } // Nothing to load from. Said as a path rather than as an empty one so that a // catalog put there later is found without the search being run again. return exePath + QStringLiteral("/share/locale"); } QString TranslationCatalog::displayNameFor(const QString &code) { // The language part on its own, and QLocale built from the code rather than // from QLocale::codeToLanguage(): given "pt_BR" that call answers English in // Qt 6, and a language list that calls Portuguese English is worse than one // with no names in it at all. const QString languageCode = code.section(QLatin1Char('_'), 0, 0); const QLocale language(languageCode); if (language.language() == QLocale::C) { // A code Qt has no name for. Shown as itself, because a row with nothing // in it is a language nobody can pick, and this one may still load. return code; } // English is one language with one name. CLDR calls the American variant // "American English", which is a country talking about itself rather than a // language, and a list of languages is not a list of countries. QString name = language.language() == QLocale::English ? QStringLiteral("English") : language.nativeLanguageName(); if (name.isEmpty()) name = QLocale::languageToString(language.language()); // A code that names a country says which, in that country's own spelling. // A few languages name their country inside the language name as well - CLDR // calls Spanish "español de España", and QLocale("es") resolves to es_ES // without being asked - and the language is what is being chosen here, so // the country comes off that spelling, along with the little word that was // holding it on. It goes back in brackets when the code named it and the // name did not. const QString territory = QLocale(code).nativeTerritoryName(); if (territory.isEmpty()) { // Nothing to take off and nothing to put back. } else if (name.length() > territory.length() && name.endsWith(territory)) { name.truncate(name.length() - territory.length()); name = name.trimmed(); const int lastSpace = name.lastIndexOf(QLatin1Char(' ')); if (lastSpace > 0 && name.length() - lastSpace <= 4) name.truncate(lastSpace); } else if (code.contains(QLatin1Char('_'))) { name = name.trimmed() + QStringLiteral(" (") + territory + QLatin1Char(')'); } name = name.trimmed(); // Capitalized, which the CLDR data is not: a language is a name, and names // are written with a capital. In a script that has no case this does // nothing, which is right. if (!name.isEmpty()) name[0] = name.at(0).toUpper(); return name; } TranslationCatalog::TranslationCatalog(QObject *parent) : QObject(parent) { build(); } TranslationCatalog *TranslationCatalog::create(QQmlEngine *, QJSEngine *) { // Constructed here rather than by the engine, so the search for the catalog // directory happens once, and before anything asks the list. return new TranslationCatalog; } void TranslationCatalog::build() { QVariantList names; names << QVariantMap{ {QStringLiteral("code"), QString()}, {QStringLiteral("displayName"), i18nc("@item:inlistbox interface language", "Follow the system")}, }; QVariantList found; for (const QString &code : localeCodesIn(localeDir())) { // "en" is the source strings and not a translation, so it is not a // directory entry to be discovered - and the entry for it is added // below either way, with a name the reader recognizes. if (code.compare(QLatin1String("en"), Qt::CaseInsensitive) == 0 || code.startsWith(QLatin1String("en_"))) continue; found << QVariantMap{ {QStringLiteral("code"), code}, {QStringLiteral("displayName"), displayNameFor(code)}, }; } // In the language's own spelling rather than in this program's, and with a // collator that knows how to order them: a byte comparison puts every // Cyrillic name after every Latin one. QCollator collator(QLocale::system()); std::sort(found.begin(), found.end(), [&collator](const QVariant &a, const QVariant &b) { return collator.compare(a.toMap().value(QStringLiteral("displayName")).toString(), b.toMap().value(QStringLiteral("displayName")).toString()) < 0; }); names << QVariantMap{ {QStringLiteral("code"), QStringLiteral("en")}, {QStringLiteral("displayName"), displayNameFor(QStringLiteral("en"))}, }; names += found; m_languages = names; }