QGuiApplication::installTranslator fails
-
I use the following Language to install a translator:
bool AppInfoController::switchToLanguage(const QString& language) { if(!m_translator.isEmpty()) QGuiApplication::removeTranslator(&m_translator); auto success = m_translator.load(":/i18n/librum_" + language); if(!success) { qWarning() << "Failed switching to language: " << language; return false; } if(!QGuiApplication::installTranslator(&m_translator)) { qWarning() << "Failed installing translator for language: " << language; return false; } emit languageChanged(); // Use "English" for all kinds of English variants like American English if(QLocale(language).language() == QLocale::Language::English) m_language = "English"; else m_language = QLocale(language).nativeLanguageName(); QSettings settings; settings.setValue("language", language); m_engine->retranslate(); return true; }
Which works all right when building it on linux, windows and mac but when deploying it to flatpak QGuiApplication::installTranslator returns false and nothing happens.
Does someone have an idea why this might happen? Does it require some kind of locale information from the OS that flatpak might not be able to provide? -
Hi,
It seems you are trying to use the full path to your translation files and that is the issue. If you look at the class documentation, you'll see that the folder where to find the file is set in a different parameter.
-
Hi @Creaperdown,
@SGaist said in QGuiApplication::installTranslator fails:
It seems you are trying to use the full path to your translation files and that is the issue.
I don't believe that's an issue. As per the doc's you linked:
Loads filename ... which may be an absolute file name or relative to directory.
Assuming that
m_translator.load()
did indeed succeed, then, just poking around theQCoreApplication::installTranslator()
source, here's a few reasons I can see it might returnfalse
:&m_translator
isnullptr
(seems impossible in your case)m_translator.isEmpty()
istrue
- easy enough for you to test.- the current application instance is
nullptr
. ie have you made sure this code only runs after the app instance is constructed? Try checking ifQCoreApplication::instance()
isnullptr
? Mind you, in this scenario, you should see aqWarning
logged.
Which Qt version are you using?
-
@Paul-Colby good point, I forgot that the extension was implicit and thus thought about the other variants.
@Creaperdown On that matter, are you sure that your resources properly embeds the translation file ?
-
@Paul-Colby I have changed my code to:
bool AppInfoController::switchToLanguage(const QString& language) { if(!m_translator.isEmpty()) QGuiApplication::removeTranslator(&m_translator); auto success = m_translator.load(":/i18n/librum_" + language); if(!success) { qWarning() << "Failed switching to language: " << language; return false; } if(m_translator.isEmpty()) { qWarning() << "Translator is empty after successfully loading"; return false; } if(QCoreApplication::instance() == nullptr) { qWarning() << "QCoreApplication is nullptr"; return false; } if(!QGuiApplication::installTranslator(&m_translator)) { qWarning() << "Failed installing translator for language: " << language; return false; } emit languageChanged(); // Use "English" for all kinds of English variants like American English if(QLocale(language).language() == QLocale::Language::English) m_language = "English"; else m_language = QLocale(language).nativeLanguageName(); QSettings settings; settings.setValue("language", language); m_engine->retranslate(); return true; }
And I can confirm that the
translator.isEmpty()
evaluates to true -
I have added debug output to the flatpack app to check the file size of the translation files and it seems like they do not get build correctly in that environment for some reason. They are only ~40 bytes in size.
-
This is the build output. The .qm file generation looks fine from what I see.
-- Build files have been written to: /run/build/librum-testing [1/95] Generating librum_de.qm Updating '/run/build/librum-testing/src/presentation/librum_de.qm'... Generated 359 translation(s) (359 finished and 0 unfinished) [2/95] Generating librum_ru.qm Updating '/run/build/librum-testing/src/presentation/librum_ru.qm'... Generated 359 translation(s) (359 finished and 0 unfinished) [3/95] Generating librum_en.qm Updating '/run/build/librum-testing/src/presentation/librum_en.qm'... Generated 0 translation(s) (0 finished and 0 unfinished) Ignored 359 untranslated source text(s) [5/95] cd /run/build/librum-testing/src/presentation && /usr/bin/cmake -DIN_FILE=/run/build/librum-test.../usr/bin/lupdate -project /run/build/librum-testing/src/presentation/.lupdate/presentation_project.json lupdate warning: Some files have been ignored due to missing qml/javascript support /usr/include/QtQml/qqmlprivate.h:343: Ignoring definition of undeclared qualified class /usr/include/QtQml/qqmlprivate.h:842: Ignoring definition of undeclared qualified class /usr/include/QtQml/qqmlprivate.h:856: Ignoring definition of undeclared qualified class /usr/include/QtQml/qqmlprivate.h:874: Ignoring definition of undeclared qualified class /usr/include/QtQml/qqmlprivate.h:888: Ignoring definition of undeclared qualified class /usr/include/QtQml/qqmlprivate.h:902: Ignoring definition of undeclared qualified class /usr/include/QtQml/qqmlprivate.h:917: Ignoring definition of undeclared qualified class /usr/include/QtQml/qqmlprivate.h:931: Ignoring definition of undeclared qualified class /usr/include/QtQml/qqmlprivate.h:945: Ignoring definition of undeclared qualified class Updating 'translations/librum_en.ts'... Found 0 source text(s) (0 new and 0 already existing) Kept 359 obsolete entries Updating 'translations/librum_de.ts'... Found 0 source text(s) (0 new and 0 already existing) Kept 359 obsolete entries Updating 'translations/librum_ru.ts'... Found 0 source text(s) (0 new and 0 already existing) Kept 359 obsolete entries
-
@Creaperdown said in QGuiApplication::installTranslator fails:
The .qm file generation looks fine from what I see.
But there are only translations for de and ru, the others are empty (= 40 bytes)
-
@Christian-Ehrlicher thats right, there are only translations to de and ru, there also is one en file which is supposed to be empty
-
If I stop the flatpak build process and inspect the files I can see this generally good looking translation file: https://ufile.io/yrvwy7sn (this is librum_de.qm)
-
@Christian-Ehrlicher I have found something weird later on in the build process.
It seems to do the following:
- Generate the qm files from the ts files correctly
- Incorrectly updating the ts files (for what ever reason)
- Building the qm files again
This is the shortened output of the build process:
[Downloading the github repos...] -- The CXX compiler identification is GNU 12.2.0 -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: /usr/bin/c++ - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Performing Test CMAKE_HAVE_LIBC_PTHREAD -- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success -- Found Threads: TRUE -- Performing Test HAVE_STDATOMIC -- Performing Test HAVE_STDATOMIC - Success -- Found WrapAtomic: TRUE -- Found OpenGL: /usr/lib/x86_64-linux-gnu/libOpenGL.so -- Found WrapOpenGL: TRUE -- Found XKB: /usr/lib/x86_64-linux-gnu/libxkbcommon.so (found suitable version "1.5.0", minimum required is "0.5.0") -- Found WrapVulkanHeaders: /usr/include -- Configuring done (0.7s) -- Generating done (0.0s) -- Build files have been written to: /run/build/librum-testing [1/95] Generating librum_de.qm Updating '/run/build/librum-testing/src/presentation/librum_de.qm'... Generated 359 translation(s) (359 finished and 0 unfinished) [2/95] Generating librum_en.qm Updating '/run/build/librum-testing/src/presentation/librum_en.qm'... Generated 0 translation(s) (0 finished and 0 unfinished) Ignored 359 untranslated source text(s) [3/95] Generating librum_ru.qm Updating '/run/build/librum-testing/src/presentation/librum_ru.qm'... Generated 359 translation(s) (359 finished and 0 unfinished) [4/95] Running qmlimportscanner for librum [5/95] cd /run/build/librum-testing/src/presentation && /usr/bin/cmake -DIN_FILE=/run/build/librum-testing/src/presentation/.lupdate/presentation_project.cmake -DOUT_FILE=/run/build/librum-testing/src/presentation/.lupdate/presentation_project.json -P /usr/lib/x86_64-linux-gnu/cmake/Qt6LinguistTools/GenerateLUpdateProject.cmake && /usr/bin/lupdate -project /run/build/librum-testing/src/presentation/.lupdate/presentation_project.json lupdate warning: Some files have been ignored due to missing qml/javascript support /usr/include/QtQml/qqmlprivate.h:343: Ignoring definition of undeclared qualified class /usr/include/QtQml/qqmlprivate.h:842: Ignoring definition of undeclared qualified class /usr/include/QtQml/qqmlprivate.h:856: Ignoring definition of undeclared qualified class /usr/include/QtQml/qqmlprivate.h:874: Ignoring definition of undeclared qualified class /usr/include/QtQml/qqmlprivate.h:888: Ignoring definition of undeclared qualified class /usr/include/QtQml/qqmlprivate.h:902: Ignoring definition of undeclared qualified class /usr/include/QtQml/qqmlprivate.h:917: Ignoring definition of undeclared qualified class /usr/include/QtQml/qqmlprivate.h:931: Ignoring definition of undeclared qualified class /usr/include/QtQml/qqmlprivate.h:945: Ignoring definition of undeclared qualified class Updating 'translations/librum_en.ts'... Found 0 source text(s) (0 new and 0 already existing) Kept 359 obsolete entries Updating 'translations/librum_de.ts'... Found 0 source text(s) (0 new and 0 already existing) Kept 359 obsolete entries Updating 'translations/librum_ru.ts'... Found 0 source text(s) (0 new and 0 already existing) Kept 359 obsolete entries [6/95] Automatic MOC and UIC for target domain [7/95] Building CXX object src/domain/CMakeFiles/domain.dir/value_objects/login_model.cpp.o [8/95] Building CXX object src/domain/CMakeFiles/domain.dir/value_objects/register_model.cpp.o [9/95] Building CXX object src/domain/CMakeFiles/domain.dir/entities/tag.cpp.o [10/95] Building CXX object src/domain/CMakeFiles/domain.dir/entities/bookmark.cpp.o [11/95] Building CXX object src/domain/CMakeFiles/domain.dir/domain_autogen/mocs_compilation.cpp.o [12/95] Building CXX object src/domain/CMakeFiles/domain.dir/entities/rectf.cpp.o [13/95] Building CXX object src/domain/CMakeFiles/domain.dir/entities/user.cpp.o [14/95] Building CXX object src/domain/CMakeFiles/domain.dir/entities/highlight.cpp.o [15/95] Building CXX object src/domain/CMakeFiles/domain.dir/entities/book.cpp.o [16/95] Linking CXX shared library libdomain.so [17/95] Building mupdf (This takes a while) ... [A lot of output from building a dependency...] [18/95] Automatic MOC and UIC for target application [Building librum...] [86/95] Building CXX object src/presentation/CMakeFiles/presentation.dir/presentation_autogen/mocs_compilation.cpp.o [87/95] Building CXX object src/presentation/CMakeFiles/presentation.dir/modules/CppElements/page_view.cpp.o [88/95] Building CXX object src/presentation/CMakeFiles/presentation.dir/presentation_autogen/YCDLW3T4OG/qrc_resources.cpp.o [89/95] Linking CXX shared library libpresentation.so [90/95] Automatic MOC and UIC for target librum [91/95] Automatic RCC for ../fonts.qrc [92/95] Building CXX object src/CMakeFiles/librum.dir/librum_autogen/mocs_compilation.cpp.o [93/95] Building CXX object src/CMakeFiles/librum.dir/librum_autogen/YCDLW3T4OG/qrc_fonts.cpp.o [94/95] Building CXX object src/CMakeFiles/librum.dir/main.cpp.o [95/95] Linking CXX executable librum [1/13] Generating librum_en.qm Updating '/run/build/librum-testing/src/presentation/librum_en.qm'... Generated 0 translation(s) (0 finished and 0 unfinished) [2/13] Generating librum_ru.qm Updating '/run/build/librum-testing/src/presentation/librum_ru.qm'... Generated 0 translation(s) (0 finished and 0 unfinished) [3/13] Generating librum_de.qm Updating '/run/build/librum-testing/src/presentation/librum_de.qm'... Generated 0 translation(s) (0 finished and 0 unfinished) [4/13] cd /run/build/librum-testing/src/presentation && /usr/bin/cmake -DIN_FILE=/run/build/librum-testing/src/presentation/.lupdate/presentation_project.cmake -DOUT_FILE=/run/build/librum-testing/src/presentation/.lupdate/presentation_project.json -P /usr/lib/x86_64-linux-gnu/cmake/Qt6LinguistTools/GenerateLUpdateProject.cmake && /usr/bin/lupdate -project /run/build/librum-testing/src/presentation/.lupdate/presentation_project.json lupdate warning: Some files have been ignored due to missing qml/javascript support /usr/include/QtQml/qqmlprivate.h:343: Ignoring definition of undeclared qualified class /usr/include/QtQml/qqmlprivate.h:842: Ignoring definition of undeclared qualified class /usr/include/QtQml/qqmlprivate.h:856: Ignoring definition of undeclared qualified class /usr/include/QtQml/qqmlprivate.h:874: Ignoring definition of undeclared qualified class /usr/include/QtQml/qqmlprivate.h:888: Ignoring definition of undeclared qualified class /usr/include/QtQml/qqmlprivate.h:902: Ignoring definition of undeclared qualified class /usr/include/QtQml/qqmlprivate.h:917: Ignoring definition of undeclared qualified class /usr/include/QtQml/qqmlprivate.h:931: Ignoring definition of undeclared qualified class /usr/include/QtQml/qqmlprivate.h:945: Ignoring definition of undeclared qualified class Updating 'translations/librum_en.ts'... Found 0 source text(s) (0 new and 0 already existing) Updating 'translations/librum_de.ts'... Found 0 source text(s) (0 new and 0 already existing) Updating 'translations/librum_ru.ts'... Found 0 source text(s) (0 new and 0 already existing) [5/13] Building mupdf (This takes a while) ... [Building the dependency that we have built before again...] [6/10] Running rcc for resource presentation_translations [7/10] Building CXX object src/presentation/CMakeFiles/presentation.dir/.rcc/qrc_presentation_translations.cpp.o [8/10] Linking CXX shared library libpresentation.so [9/10] Linking CXX executable librum [9/10] Install the project... -- Install configuration: "Release" -- Installing: /app/lib/libdomain.so -- Installing: /app/lib/libapplication.so -- Set runtime path of "/app/lib/libapplication.so" to "" -- Installing: /app/lib/libmupdfcpp.so -- Installing: /app/lib/libmupdf.so -- Installing: /app/lib/libmupdfcpp.so.24.0 -- Installing: /app/lib/libmupdf.so.24.0 -- Installing: /app/lib/libinfrastructure.so -- Set runtime path of "/app/lib/libinfrastructure.so" to "" -- Installing: /app/lib/libadapters.so -- Set runtime path of "/app/lib/libadapters.so" to "" -- Installing: /app/lib/libpresentation.so -- Set runtime path of "/app/lib/libpresentation.so" to "" -- Installing: /app/bin/librum -- Set runtime path of "/app/bin/librum" to "" -- Installing: /app/share/pixmaps/librum.svg -- Installing: /app/share/applications/librum.desktop [Some cleanup...]
I have no idea why it starts at [1/13] again, but I suppose that this causes the problem since it overwrites the seemingly correct qm files.