Important: Please read the Qt Code of Conduct -

Translate texts read from a file

  • Hi,
    I'm trying to find a way of handling all the errors and messages my application can output for the user

    My target machine has a list of possible errors to show to the user, i want to put all the error strings in a text file, then when an error will occure the machine will ask to my application to display that error using an identifier.

    How should i translate these errors strings read from the file ?
    Is my only option to have one text file per language ?

  • Moderators

    So your plan is to create a file with error messages, right? In such case, I suggest creating a C++ file and using QObject::tr(). This way you will be able to handle your errors in exactly the same way you treat your application translations.

  • Moderators

    @LeLev if you do not want to use the Qt Translation system but rather use an external file.

    Then you have to read either a different file when a different language is selected or a different section of your file, depending on how you build this.

    The probably easiest way is it to use Qt Translation.
    For that move all those texts in a different file (header only, or a full fletched class, depends on your liking) and mark them for translation. (tr("text here"))

    If you don't want a QObject based class, for that, I could understand, you can also use QT_TRANSLATE_NOOP or QT_TR_NOOP and the static QCoreApplication::translate() or QCoreApplication::tr() respectively, to runtime translate the char array

  • @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

  • Moderators

    @LeLev something like this

    #include <QCoreApplication>
    #include <array>
    class ErrorMessageTranslations
        enum MyErrors{
        }; Q_ENUM(MyErrors)
        ErrorMessageTranslations() = default;
        QString errorToString(MyErrors error)
            static const std::array<const char*, 6> translations = {
            return  QCoreApplication::tr(translations[static_cast<int>(error)]);

    a map would probably better, or at least a range check, but you get the idea :D

  • Very nice thank you for your answers!

  • @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 ?

  • Moderators

    @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

  • @J-Hilk i mean if my texts are in a c++ class.

    if i had texts in a separate text file i could add / delete new messages without recompiling the application

  • Moderators

    @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

  • Ok thanks everybody

  • @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.

Log in to reply