Translate texts read from a file
-
@sierdzio , @J-Hilk thanks for answers
i'm not sure i understand exactly what should i do, i want to use Qt Translation system.
So do you mean i have to use a .h header file instead of a .txt file, and put all my texts inside it ?Could you please show me a simple example of the .h file ? And how to access its content
-
@LeLev something like this
#ifndef ERRORMESSAGETRANSLATIONS_H #define ERRORMESSAGETRANSLATIONS_H #include <QCoreApplication> #include <array> class ErrorMessageTranslations { Q_GADGET public: enum MyErrors{ MyError1, MyError2, MyError3, MyError4, MyError5, MyError6 }; Q_ENUM(MyErrors) ErrorMessageTranslations() = default; QString errorToString(MyErrors error) { static const std::array<const char*, 6> translations = { QT_TR_NOOP("MyError1"), QT_TR_NOOP("MyError2"), QT_TR_NOOP("MyError3"), QT_TR_NOOP("MyError4"), QT_TR_NOOP("MyError5"), QT_TR_NOOP("MyError6") }; return QCoreApplication::tr(translations[static_cast<int>(error)]); } }; #endif // ERRORMESSAGETRANSLATIONS_H
a map would probably better, or at least a range check, but you get the idea :D
-
@LeLev said in Translate texts read from a file:
i want to put all the error strings in a text file
And why not to put the error messages within the source code itself, in the usual way of the Qt tr() translation approach?
Is that text file already create? does it to be maintained by somebody else without access to the code?
If the answers are no. I don't see the need to put the error messages into a separate container. -
@Pablo-J-Rogina hi
I wanted to put everything in a separate text file to easily add / delete new errors without opening the project source files.
@J-Hilk in the approach you provided i also have to recompile the application right ? -
@LeLev said in Translate texts read from a file:
@J-Hilk in the approach you provided i also have to recompile the application right ?
well, you generally have to recompile your project, what situation do you mean exactly :D
-
@LeLev true enough,
If you add/remove something from your enum, you have to recompile anyway, make sure to expand the String translation before hand.
If you want to change text/wording, or add new languages, then no, you do not necessarily need to recompile your application.
You can edit your translation files (.ts) via QLinguist and simply exchange the resulting *.qm files
-
@LeLev said in Translate texts read from a file:
Re-reading your requirements I guess you could be able to maintain (add/delete) your error strings without the need of recompiling the application.
My target machine has a list of possible errors to show to the user,
If those possible errors have a code (i.e. ABC123 or 123456) then you don't need the enum from @J-Hilk example, you'll use such codes as index for such std::array in the example or the key in the suggested map or whatever data structure you end up using.
In that way, you can maintain the .h file with the error strings separate from the source code, and just updating the .qm file(s) whenever the strings change. Since your source code will always use an index for the error string, no re-compilation of app is needed.