pure-cpp 1.0.0
A C++ physics simulation benchmark comparing performance with Python implementations
translation_loader.hpp
Go to the documentation of this file.
1#ifndef TRANSLATION_LOADER_HPP
2#define TRANSLATION_LOADER_HPP
3
4/**
5 * \file translation_loader.hpp
6 * \brief Helper class for loading application and Qt translations.
7 * \author Le Bars, Yoann
8 * \ingroup UtilsTranslation
9 *
10 * This file is part of the pure C++ benchmark.
11 */
12
13#include <QApplication>
14#include <QDebug>
15#include <QLibraryInfo>
16#include <QLocale>
17#include <QString>
18#include <QTranslator>
19
20/// \brief Namespace for application-wide utility functions.
21namespace AppUtils {
22
23 /**
24 * \brief Provides a static method to load translation files for the
25 * application.
26 * \ingroup UtilsTranslation
27 */
29 public:
30 /**
31 * \brief Loads and installs the best-matching translation for the
32 * system’s locale.
33 *
34 * \param in_diagnosticsEnabled If true, prints detailed debug
35 * information during loading.
36 */
37 static void loadTranslations(bool in_diagnosticsEnabled) {
38 /* Helper lambdas to avoid repeated if(in_diagnosticsEnabled)
39 checks. */
40 const auto logInfo = [in_diagnosticsEnabled](const auto&... args) {
41 if (in_diagnosticsEnabled) (qDebug().noquote() << ... << args);
42 };
43 // Lambda for logging successful operations.
44 const auto logSuccess =
45 [in_diagnosticsEnabled](const auto&... args) {
46 if (in_diagnosticsEnabled) (qDebug() << ... << args);
47 };
48 // Lambda for logging warnings.
49 const auto logWarning =
50 [in_diagnosticsEnabled](const auto&... args) {
51 if (in_diagnosticsEnabled)
52 (qWarning().noquote() << ... << args);
53 };
54
55 const QStringList uiLanguages = QLocale::system().uiLanguages();
56 logSuccess("--- Translation Debug ---");
57 logSuccess("System UI Languages:", uiLanguages);
58
59 for (const QString& locale : uiLanguages) {
60 logInfo("Attempting to load locale:", locale);
61 const QLocale qLocale(locale);
62
63 // Load application's translator.
64 const QString appTranslationPath = QLatin1String(":/i18n");
65 // The translator is parented to qApp, so its lifetime is
66 // managed by Qt.
67 auto appTranslator = new QTranslator(qApp);
68 logInfo(" - App translator: loading 'pure-cpp_", locale,
69 "' from", appTranslationPath);
70 if (appTranslator->load(qLocale, QLatin1String("pure-cpp"),
71 QLatin1String("_"),
72 appTranslationPath)) {
73 logSuccess(
74 " -> Success: Application translator installed.");
75 QApplication::installTranslator(appTranslator);
76
77 // Load Qt's base translator for the same locale.
78 auto qtTranslator = new QTranslator(qApp);
79 const QString qtTranslationPath =
80 QLibraryInfo::path(QLibraryInfo::TranslationsPath);
81 logInfo(" - Qt translator: loading 'qt_", locale, "' from",
82 qtTranslationPath);
83 if (qtTranslator->load(qLocale, QLatin1String("qt"),
84 QLatin1String("_"),
85 qtTranslationPath)) {
86 logSuccess(
87 " -> Success: Qt base translator installed.");
88 QApplication::installTranslator(qtTranslator);
89 } else {
90 logWarning(
91 " -> Failed: Could not load Qt base "
92 "translator.");
93 }
94 return; // Stop after the first successful load.
95 } else {
96 logWarning(
97 " - App translator: Failed to load translation for",
98 locale);
99 // Clean up if load failed before next iteration.
100 delete appTranslator;
101 }
102 }
103 }
104 };
105
106} // namespace AppUtils
107
108#endif // TRANSLATION_LOADER_HPP
Provides a static method to load translation files for the application.
static void loadTranslations(bool in_diagnosticsEnabled)
Loads and installs the best-matching translation for the system’s locale.
Namespace for application-wide utility functions, primarily for exception handling.