how to embed translation files?
-
Hi All,
I'm trying to embed a translation (qm) file in the wasm app using the --embed-file option which I successfully used earlier in pure c++ projects. But I don't even know if it gets embedded at all since nothing complains about not being able to find the file. However, when loading it via translator.load(), I get back false as result. I suspect that the relative path may come in to play here. The emscripten doc (https://emscripten.org/docs/porting/files/packaging_files.html) says at the embed option:
"By default, the files to be packaged should be nested in or below the compile-time command prompt directory. At runtime the same nested file structure is mapped to the virtual file system, with the root corresponding to the command prompt directory."
How do I know what's the "compile-time command prompt directory" when I hit the build button? Currently, I just add these two lines to my project file:
QMAKE_CFLAGS += --embed-file test_en.qm
QMAKE_CXXFLAGS += --embed-file test_en.qmThe file is currently in the project directory but according to the emscripten docs, it will only be picked up if emcc/em++ is called from the project directory. I also tried copying the translation file over to the build directory hoping that it gets picked up but that does not work either. Thanks for any hints in advance!
Best regards,
r0ller -
Ok, managed to figure it out:
- The embedding flag must be passed to the linker (makes sense actually) like: QMAKE_LFLAGS += --embed-file test_en.qm
- The compile-time directory taken into account is not the project but the build directory so in the project file (.pro) the path for --embed-file must be specified relative to that. In 1. above it will expect that test_en.qm resides in the build directory. When calling QTranslator::load(), the path shall be the same as in the project file. So in my case it's just translator.load("test_en") as I simply copied the qm file to the build directory for the test.
-
Hi,
Out of curiosity, why not use Qt's resource system ?
-
I'm pretty new to Qt and did not know anything about the resource system :) So I simply googled for qml internationalization and in the results this page seemed appropriate:
https://doc.qt.io/qt-5/qtquick-internationalization.html
That one pointed to the Qt Linguist Manual: https://doc.qt.io/qt-5/qtlinguist-index.html
On that page the developers section seemed relevant: https://doc.qt.io/qt-5/linguist-programmers.html
Which referenced a simple example: https://doc.qt.io/qt-5/qtlinguist-hellotr-example.html
So that's the reason how I ended up with this :) I can't recall that those pages mention the resource system. Thanks for the link by the way! I'll read that page to see if it's a better choice. But I guess the file embedding lesson learned here will come handy in that case as well.