Cockatrice/libcockatrice_utility/libcockatrice/utility/translation_loader.h
BruebachL 0934380a5c
[Translations] Load Qt's own translations and build ours with the binary (#7384)
* [Translations] Load Qt's own translations and build ours with the binary

Three separate bugs kept translations from working.

installNewTranslator() loaded the application translation into
qtTranslator. QTranslator discards its previous contents on every load, so
that wiped the qt_<lang>.qm loaded one line earlier and installed an empty
translator instead. Qt's own strings were never translated on any platform.
Give each file its own translator.

QLibraryInfo::path(TranslationsPath) was the only directory searched for
Qt's translations. A relocated Qt can report a path that does not exist: the
KDE Flatpak runtime reports /usr/translations while its files live in
/usr/share/qt6/translations. Probe the usual install layouts and only hand
back the ones that exist.

The .qm files were attached to the default build target only, so building
the executable on its own left it without any translations and the language
selector empty. Attach them to the target as well.

Move the shared lookup into TranslationLoader so client and Oracle cannot
drift again, and have it report a missing translation at debug level. An
unset language is the default for English, which has no file to begin with,
and warning about it only added noise to every fresh debug log.

Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>

* [Tests] Define main() in translation_loader_test

Without a GTest installation the fallback in tests/CMakeLists.txt links
only gtest, not gtest_main, so the test has to bring its own main(). Every
other test already does; only this one relied on the distribution package
and failed to link on CI.

Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>

* [Tests] Put the translation loader fixture next to the test executable

A multi-config generator (MSBuild, Xcode) writes the executable to a
per-config subdirectory while the .qm stays in the current binary
directory, so applicationDirPath() never saw the fixture and
ABuildTreeFindsTheTranslationsNextToTheExecutable failed on Windows.

* [Translations] Depend on Qt's lrelease target and skip the loader test without lrelease

Attaching the .qm custom commands to a second custom target races with
Qt's own <Project>_lrelease target on MSBuild/Makefiles and is rejected
by Xcode, so depend on Qt's target instead. Only build the translation
loader test when Qt6::lrelease is available.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
Co-authored-by: ZeldaZach <zahalpern+github@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-03 15:04:30 +02:00

92 lines
3.8 KiB
C++

#ifndef TRANSLATION_LOADER_H
#define TRANSLATION_LOADER_H
#include <QList>
#include <QLoggingCategory>
#include <QString>
class QTranslator;
Q_DECLARE_LOGGING_CATEGORY(TranslationLoaderLog)
/** @brief The outcome of a translation load attempt. */
enum class TranslationLoadResult
{
Loaded, /**< A translation file was found and loaded into the translator. */
Missing, /**< The search directories exist but hold no file for this language. */
NoDirectory /**< Not a single search directory exists, so no lookup could be attempted. */
};
namespace TranslationLoader
{
/**
* @brief Every directory that may hold Qt's own translations, in order of preference.
*
* More than one entry is needed because a relocated Qt (Flatpak, AppImage, a bundled SDK) can report
* a prefix-relative translations path that does not exist: the KDE runtime reports /usr/translations
* while its qt_*.qm files live in /usr/share/qt6/translations.
*/
QList<QString> qtTranslationCandidates();
/**
* @brief The candidates that actually exist, most likely first, so no caller searches a path that is
* not there.
*/
QList<QString> qtTranslationPaths();
/**
* @brief The directories to search for an application's own translation files, in order of preference.
*
* @param _installedPath The location the platform installs the translation files in, derived from the
* executable's path.
* @return The installed location, followed by the executable's own directory: a build tree keeps the
* .qm files next to the binary instead of in the installed layout, so a developer build would
* otherwise find no translations at all.
*/
QList<QString> applicationTranslationPaths(const QString &_installedPath);
/**
* @brief Loads the first _nameHint + language translation file found in _directories into _translator.
*
* QTranslator discards whatever it held before every load, so each translation file needs its own
* translator; loading a second file into the first one silently drops the first.
*
* @param _translator The translator to load into.
* @param _nameHint The translation file name without its language and suffix, e.g. "qt_de" or "cockatrice_de".
* @param _directories The directories to search, in order of preference.
* @return What the search found.
*/
TranslationLoadResult loadFrom(QTranslator &_translator, const QString &_nameHint, const QList<QString> &_directories);
/**
* @brief Loads Qt's own translation for _lang, which covers the strings of Qt's own dialogs.
*
* @param _translator The translator to load into; must not also be used for an application translation.
* @param _lang The language code, e.g. "de". An empty language loads nothing.
* @return What the search found.
*/
TranslationLoadResult loadQt(QTranslator &_translator, const QString &_lang);
/**
* @brief The language codes of the <_prefix>_<lang>.qm files in _directories, sorted by code.
*
* Lets a language selector offer exactly the languages that can be loaded.
*/
QList<QString> availableLanguages(const QString &_prefix, const QList<QString> &_directories);
/**
* @brief Loads the <_prefix>_<_lang>.qm translation file from _directories into _translator.
*
* @param _translator The translator to load into; must not also be used for Qt's own translation.
* @param _prefix The translation file prefix, e.g. "cockatrice" or "oracle".
* @param _lang The language code, e.g. "de". An empty language loads nothing.
* @param _directories The directories to search, in order of preference.
* @return What the search found.
*/
TranslationLoadResult loadApplication(QTranslator &_translator,
const QString &_prefix,
const QString &_lang,
const QList<QString> &_directories);
} // namespace TranslationLoader
#endif // TRANSLATION_LOADER_H