File indexing completed on 2024-05-12 04:44:33
0001 // SPDX-FileCopyrightText: Lukas Sommer <sommerluk@gmail.com> 0002 // SPDX-License-Identifier: BSD-2-Clause OR MIT 0003 0004 #ifndef INITIALIZETRANSLATION_H 0005 #define INITIALIZETRANSLATION_H 0006 0007 #include <optional> 0008 0009 #include <qglobal.h> 0010 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) 0011 #include <qcontainerfwd.h> 0012 #else 0013 class QStringList; 0014 #endif 0015 0016 class QCoreApplication; 0017 0018 /** @internal @file 0019 * 0020 * Provides the @ref PerceptualColor::initializeTranslation() function. 0021 * 0022 * @internal 0023 * 0024 * @todo Call <tt> 0025 * <a href="https://doc.qt.io/qt-5/qmetatype.html#qRegisterMetaType-1"> 0026 * qRegisterMetaType()</a></tt> for all our data types during initialization!? 0027 * 0028 * @todo Provide an own documentation page for initialization and localization. 0029 * 0030 * @todo Investigate automatic initialization. We definitely 0031 * need to call the @ref PerceptualColor::initializeTranslation() 0032 * function, but having to call it manually might be 0033 * cumbersome. It would be more comfortable if the initialization 0034 * would happen automatically. Apparently, if we want to support both, SHARED 0035 * <a href="https://www.volkerkrause.eu/2018/10/13/kf5-static-builds.html"> 0036 * and STATIC libraries, we have to</a> 0037 * - either <a href="https://phabricator.kde.org/D13816">request the library 0038 * user to call manually</a> @ref PerceptualColor::initializeTranslation() 0039 * on both, STATIC and SHARED libraries. This gives us uniform behavior 0040 * between STATIC and SHARED builds and makes bug-tracking easier. 0041 * - or use <tt> 0042 * <a href="http://doc.qt.io/qt-5/qcoreapplication.html#Q_COREAPP_STARTUP_FUNCTION"> 0043 * Q_COREAPP_STARTUP_FUNCTION</a></tt> to call 0044 * @ref PerceptualColor::initializeTranslation() on SHARED 0045 * libraries automatically when QCoreApplication starts (works 0046 * only for SHARED libraries, and not for STATIC libraries, and 0047 * and <a href="https://stackoverflow.com/questions/43333151"> there 0048 * seems to be no workaround</a>) and request the library user to 0049 * call @ref PerceptualColor::initializeTranslation() manually 0050 * only on STATIC libraries. 0051 * - or make @ref PerceptualColor::initializeTranslation() private and call it 0052 * <a href="https://github.com/KDE/kitinerary/commit/72326ed"> 0053 * in our library code whenever our code is about to do 0054 * something that requires previous initialization</a>. To not call it 0055 * too often or call it within functions that are executed often, it 0056 * might be better to call it in the constructor of <em>all</em> our classes 0057 * that need previous initialization. This approach would be more comfortable 0058 * for the library user. The disadvantage is that we have to pay attention 0059 * to never use <tt>tr()</tt> without calling the initialization first, and 0060 * forgetting about this might introduce subtile and difficult-to-discover 0061 * bugs. 0062 * - or make @ref PerceptualColor::initializeTranslation() private and 0063 * use the solution that <a href="https://stackoverflow.com/a/1420261"> 0064 * instantiates a special class as global variable in every translation 0065 * unit</a>, and this class makes sure that the initialization 0066 * happens also for static libraries. But wouldn’t this mean that 0067 * @ref PerceptualColor::initializeTranslation() is called at program 0068 * startup, while still no QApplication object is available, and 0069 * therefore our @ref PerceptualColor::initializeTranslation() 0070 * function will crash? And anyway, when the QApplication object is 0071 * destroyed and re-created, this pattern will not help. (Might 0072 * https://doc.qt.io/qt-5/qglobalstatic.html#Q_GLOBAL_STATIC help?) See 0073 * also https://isocpp.org/wiki/faq/ctors#static-init-order for useful 0074 * information about static initialization. 0075 * 0076 * @todo Do not reload translations in one thread while another thread 0077 * uses tr(). But how to make this sure, also because the library user 0078 * could also reload its own translations while we are using tr(). At least, 0079 * we could make sure our classes that use translations (widget classes but 0080 * also @ref PerceptualColor::RgbColorSpace) cannot be instantiated outside 0081 * of the main thread. But @ref PerceptualColor::RgbColorSpace is 0082 * meant as thread-safe, so it would be strange if its constructor 0083 * (and maybe the properties that return translated text) is 0084 * the own function that is not thread-safe… 0085 * 0086 * @todo LocaleChange could be important for spin boxes to accept the new 0087 * QLocale’s number format. 0088 * 0089 * @todo Catch QEvent::LayoutDirectionChange in all widgets? Or the signal 0090 * QGuiApplication::layoutDirectionChanged()? */ 0091 0092 namespace PerceptualColor 0093 { 0094 0095 void initializeTranslation(QCoreApplication *instance, std::optional<QStringList> newUiLanguages); 0096 0097 } // namespace PerceptualColor 0098 0099 #endif // INITIALIZETRANSLATION_H