Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* The MeshMC News section is now displayed via KF6 Syndication.
* KFormat migration has been completed.
* KMainWindow migration has been completed.
* Support for KF6 translations has been added to the Windows and macOS installations.

## Removed

Expand Down
82 changes: 82 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,43 @@ endif()
file(RELATIVE_PATH MeshMC_LOCALE_RELATIVE_DIR
"/${BINARY_DEST_DIR}" "/${MeshMC_LOCALE_DEST_DIR}")

# KDE Frameworks translate their own strings (the buttons and menus of KXmlGui,
# the names in KColorScheme, the durations of KFormat) in catalogs of their own.
# These are the ones of the frameworks MeshMC links, in two kinds: gettext
# (<domain>.mo, found by KI18n through the domain) and Qt's (<domain>.qm, loaded
# by a QTranslator). They are installed next to meshmc.mo, below, and
# Translations::registerFrameworkCatalogs() uses the same lists: it gets them as
# comma separated definitions of MeshMC_logic.
# A framework that is linked and is not here is a framework that speaks
# English; the first place to look is the .mo and _qt.qm files in
# <KF prefix>/share/locale/de/LC_MESSAGES.
set(MeshMC_KF_GETTEXT_DOMAINS
kcolorscheme6
kconfigwidgets6
ki18n6
kiconthemes6
kxmlgui6
)
set(MeshMC_KF_QT_DOMAINS
karchive6_qt
kcodecs6_qt
kconfig6_qt
kcoreaddons6_qt
kitemviews6_qt
kwidgetsaddons6_qt
)

# On Linux the frameworks come with the system, and with their translations,
# and a distribution's package should not ship a second copy.
if(WIN32 OR APPLE)
set(_meshmc_bundle_kf_translations ON)
else()
set(_meshmc_bundle_kf_translations OFF)
endif()
option(MeshMC_BUNDLE_KF_TRANSLATIONS
"Install the translations of KDE Frameworks with MeshMC's own"
${_meshmc_bundle_kf_translations})

################################ Included Libs ################################

include(ExternalProject)
Expand Down Expand Up @@ -623,3 +660,48 @@ feature_summary(
# portable install.
set(KDE_INSTALL_LOCALEDIR "${MeshMC_LOCALE_DEST_DIR}")
ki18n_install(po)

# The translations of KDE Frameworks, for the languages MeshMC has (one folder
# of po/ each), next to meshmc.mo. KF does not know "zh" as MeshMC does: its
# folder for Chinese as written in China is zh_CN.
if(MeshMC_BUNDLE_KF_TRANSLATIONS)
find_path(MeshMC_KF_LOCALE_DIR
NAMES de/LC_MESSAGES/kxmlgui6.mo
PATH_SUFFIXES share/locale
DOC "Where KDE Frameworks installed its translations")
if(NOT MeshMC_KF_LOCALE_DIR)
message(WARNING
"The translations of KDE Frameworks were not found (looked for "
"share/locale/de/LC_MESSAGES/kxmlgui6.mo in CMAKE_PREFIX_PATH), so "
"they are not installed and the frameworks speak English. Set "
"MeshMC_KF_LOCALE_DIR, or MeshMC_BUNDLE_KF_TRANSLATIONS to OFF.")
else()
message(STATUS "Translations of KDE Frameworks: ${MeshMC_KF_LOCALE_DIR}")
file(GLOB _meshmc_po_entries LIST_DIRECTORIES true
RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/po"
"${CMAKE_CURRENT_SOURCE_DIR}/po/*")
foreach(_lang IN LISTS _meshmc_po_entries)
if(NOT IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/po/${_lang}")
continue()
endif()
set(_kf_lang "${_lang}")
if(_lang STREQUAL "zh")
set(_kf_lang "zh_CN")
endif()
foreach(_domain IN LISTS MeshMC_KF_GETTEXT_DOMAINS)
set(_catalog "${MeshMC_KF_LOCALE_DIR}/${_kf_lang}/LC_MESSAGES/${_domain}.mo")
if(EXISTS "${_catalog}")
install(FILES "${_catalog}"
DESTINATION "${MeshMC_LOCALE_DEST_DIR}/${_kf_lang}/LC_MESSAGES")
endif()
endforeach()
foreach(_domain IN LISTS MeshMC_KF_QT_DOMAINS)
set(_catalog "${MeshMC_KF_LOCALE_DIR}/${_kf_lang}/LC_MESSAGES/${_domain}.qm")
if(EXISTS "${_catalog}")
install(FILES "${_catalog}"
DESTINATION "${MeshMC_LOCALE_DEST_DIR}/${_kf_lang}/LC_MESSAGES")
endif()
endforeach()
endforeach()
endif()
endif()
9 changes: 9 additions & 0 deletions launcher/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,15 @@ target_compile_definitions(MeshMC_logic PUBLIC QT_MESSAGELOGCONTEXT)
target_compile_definitions(MeshMC_logic PRIVATE
MESHMC_LOCALE_RELATIVE_DIR="${MeshMC_LOCALE_RELATIVE_DIR}")

# The catalogs of KDE Frameworks that are installed with meshmc.mo, which the
# top-level CMakeLists decides, as comma separated lists. See
# Translations::registerFrameworkCatalogs().
string(REPLACE ";" "," _kf_gettext_domains "${MeshMC_KF_GETTEXT_DOMAINS}")
string(REPLACE ";" "," _kf_qt_domains "${MeshMC_KF_QT_DOMAINS}")
target_compile_definitions(MeshMC_logic PRIVATE
MESHMC_KF_GETTEXT_DOMAINS="${_kf_gettext_domains}"
MESHMC_KF_QT_DOMAINS="${_kf_qt_domains}")

target_link_libraries(MeshMC_logic
Qt6::Core
Qt6::Xml
Expand Down
74 changes: 74 additions & 0 deletions launcher/translations/Translations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <QStandardPaths>
#include <QTranslator>

#include <algorithm>

#include <KLocalizedString>

#include "settings/SettingsObject.h"
Expand All @@ -47,6 +49,72 @@ QString Translations::bundledCatalogDir()
QStringLiteral("/" MESHMC_LOCALE_RELATIVE_DIR));
}

namespace
{
// The domains are decided in the top-level CMakeLists, which also
// installs the catalogs: comma separated.
#ifndef MESHMC_KF_GETTEXT_DOMAINS
#define MESHMC_KF_GETTEXT_DOMAINS ""
#endif
#ifndef MESHMC_KF_QT_DOMAINS
#define MESHMC_KF_QT_DOMAINS ""
#endif
} // namespace

QStringList Translations::frameworkGettextDomains()
{
return QString::fromLatin1(MESHMC_KF_GETTEXT_DOMAINS)
.split(QLatin1Char(','), Qt::SkipEmptyParts);
}

QStringList Translations::frameworkQtDomains()
{
return QString::fromLatin1(MESHMC_KF_QT_DOMAINS)
.split(QLatin1Char(','), Qt::SkipEmptyParts);
}

QStringList Translations::frameworkQtCatalogs(const QString& dir,
const QStringList& languages,
const QStringList& domains)
{
QStringList found;
// Most preferred first...
for (const QString& language : languages) {
for (const QString& domain : domains) {
const QString path = QStringLiteral("%1/%2/LC_MESSAGES/%3.qm")
.arg(dir, language, domain);
if (QFileInfo::exists(path) && !found.contains(path)) {
found.append(path);
}
}
}
// ...and installed last.
std::reverse(found.begin(), found.end());
return found;
}

int Translations::registerFrameworkCatalogs(const QString& dir,
const QStringList& languages)
{
for (const QString& domain : frameworkGettextDomains()) {
KLocalizedString::addDomainLocaleDir(domain.toLatin1(), dir);
}

int installed = 0;
for (const QString& path :
frameworkQtCatalogs(dir, languages, frameworkQtDomains())) {
auto* translator = new QTranslator(QCoreApplication::instance());
if (translator->load(path) &&
QCoreApplication::installTranslator(translator)) {
++installed;
} else {
qWarning() << "Could not load the translation" << path;
delete translator;
}
}
return installed;
}

QString Translations::overridesFilePath()
{
// where KSwitchLanguageDialog keeps it
Expand Down Expand Up @@ -132,6 +200,12 @@ void Translations::initialize(SettingsObject* settings)
QCoreApplication::sendEvent(QCoreApplication::instance(),
&languageChange);

// The catalogs of KDE Frameworks, which are in the same directory.
const int frameworks =
registerFrameworkCatalogs(bundledCatalogDir(), KLocalizedString::languages());
qDebug() << "Installed" << frameworks
<< "translations of KDE Frameworks that are Qt's own.";

// The language the application is shown in: the first of the list that
// there is a catalog for. English, the language of the source strings,
// needs none.
Expand Down
33 changes: 33 additions & 0 deletions launcher/translations/Translations.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#pragma once

#include <QString>
#include <QStringList>

class SettingsObject;

Expand Down Expand Up @@ -73,4 +74,36 @@ namespace Translations

/// The file the language dialog writes to.
QString overridesFilePath();

/*!
* KDE Frameworks translate their own strings (the buttons and menus of
* KXmlGui, "Default" in the colour schemes, the durations of KFormat) in
* catalogs of their own, which are installed next to meshmc.mo (see the
* top-level CMakeLists, which decides what; this is the same list). A
* framework either uses gettext, a ".mo" file KI18n finds by its domain,
* or Qt's own, a "_qt.qm" file that a QTranslator has to load.
*/
QStringList frameworkGettextDomains();
QStringList frameworkQtDomains();

/*!
* The "_qt.qm" files of \p domains that there are in \p dir for \p
* languages (the languages in the order of the user's preference):
* <dir>/<language>/LC_MESSAGES/<domain>.qm, in the order they are to be
* installed. That is the reverse: the translator that was installed last
* is asked first, and has to be the one of the language the user prefers.
*/
QStringList frameworkQtCatalogs(const QString& dir,
const QStringList& languages,
const QStringList& domains);

/*!
* Tells KI18n where the gettext catalogs of the frameworks are, and
* installs the Qt ones for \p languages. Nothing is loaded for a catalog
* that is not there: a framework then speaks English.
*
* \return how many Qt catalogs were installed.
*/
int registerFrameworkCatalogs(const QString& dir,
const QStringList& languages);
} // namespace Translations
39 changes: 39 additions & 0 deletions launcher/translations/Translations_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <QTest>

#include <QCoreApplication>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QSettings>
Expand Down Expand Up @@ -159,6 +160,44 @@ class TranslationsTest : public QObject
QCOMPARE(overrides.value(QStringLiteral("dolphin")).toByteArray(),
QByteArray("sv"));
}

void test_FrameworkQtCatalogsAreInTheOrderOfPreference()
{
QTemporaryDir dir;
for (const char* path :
{"de/LC_MESSAGES/a_qt.qm", "de/LC_MESSAGES/b_qt.qm",
"tr/LC_MESSAGES/a_qt.qm", "fr/LC_MESSAGES/a_qt.qm"}) {
const QString full = dir.filePath(QString::fromLatin1(path));
QVERIFY(QDir().mkpath(QFileInfo(full).absolutePath()));
QFile file(full);
QVERIFY(file.open(QIODevice::WriteOnly));
}

// German is preferred to Turkish; there is no Spanish; French is not
// asked for. The one that is preferred is installed last.
const QStringList found = Translations::frameworkQtCatalogs(
dir.path(), {"de", "es", "tr", "de"},
{"a_qt", "b_qt", "c_qt"});
QCOMPARE(found,
(QStringList{dir.filePath("tr/LC_MESSAGES/a_qt.qm"),
dir.filePath("de/LC_MESSAGES/b_qt.qm"),
dir.filePath("de/LC_MESSAGES/a_qt.qm")}));
}

void test_NothingIsRegisteredWhereThereIsNothing()
{
QTemporaryDir dir;
QCOMPARE(Translations::registerFrameworkCatalogs(dir.path(), {"de"}),
0);
}

void test_TheFrameworkDomainsComeFromTheBuild()
{
// the top-level CMakeLists decides them and installs the catalogs
QVERIFY(Translations::frameworkGettextDomains().contains("kxmlgui6"));
QVERIFY(
Translations::frameworkQtDomains().contains("kcoreaddons6_qt"));
}
};

QTEST_MAIN(TranslationsTest)
Expand Down
Loading
Loading