Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Brainstorm
  4. Translate texts read from a file
QtWS25 Last Chance

Translate texts read from a file

Scheduled Pinned Locked Moved Solved Brainstorm
13 Posts 4 Posters 2.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    ODБOï
    wrote on 2 Sept 2020, 11:18 last edited by
    #1

    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 ?
    thx

    J P 3 Replies Last reply 2 Sept 2020, 11:45
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 2 Sept 2020, 11:39 last edited by
      #2

      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.

      (Z(:^

      1 Reply Last reply
      4
      • O ODБOï
        2 Sept 2020, 11:18

        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 ?
        thx

        J Offline
        J Offline
        J.Hilk
        Moderators
        wrote on 2 Sept 2020, 11:45 last edited by J.Hilk 9 Feb 2020, 11:46
        #3

        @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


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        3
        • O Offline
          O Offline
          ODБOï
          wrote on 2 Sept 2020, 12:13 last edited by
          #4

          @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

          J 1 Reply Last reply 2 Sept 2020, 12:20
          0
          • O ODБOï
            2 Sept 2020, 12:13

            @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

            J Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 2 Sept 2020, 12:20 last edited by J.Hilk 9 Feb 2020, 12:21
            #5

            @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


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            2
            • O Offline
              O Offline
              ODБOï
              wrote on 2 Sept 2020, 12:30 last edited by
              #6

              Very nice thank you for your answers!

              1 Reply Last reply
              1
              • O ODБOï
                2 Sept 2020, 11:18

                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 ?
                thx

                P Offline
                P Offline
                Pablo J. Rogina
                wrote on 2 Sept 2020, 12:35 last edited by
                #7

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

                Upvote the answer(s) that helped you solve the issue
                Use "Topic Tools" button to mark your post as Solved
                Add screenshots via postimage.org
                Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                O 1 Reply Last reply 2 Sept 2020, 12:45
                1
                • P Pablo J. Rogina
                  2 Sept 2020, 12:35

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

                  O Offline
                  O Offline
                  ODБOï
                  wrote on 2 Sept 2020, 12:45 last edited by
                  #8

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

                  J 1 Reply Last reply 2 Sept 2020, 12:46
                  0
                  • O ODБOï
                    2 Sept 2020, 12:45

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

                    J Offline
                    J Offline
                    J.Hilk
                    Moderators
                    wrote on 2 Sept 2020, 12:46 last edited by
                    #9

                    @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


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    O 1 Reply Last reply 2 Sept 2020, 12:48
                    0
                    • J J.Hilk
                      2 Sept 2020, 12:46

                      @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

                      O Offline
                      O Offline
                      ODБOï
                      wrote on 2 Sept 2020, 12:48 last edited by
                      #10

                      @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

                      J 1 Reply Last reply 2 Sept 2020, 12:51
                      0
                      • O ODБOï
                        2 Sept 2020, 12:48

                        @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

                        J Offline
                        J Offline
                        J.Hilk
                        Moderators
                        wrote on 2 Sept 2020, 12:51 last edited by
                        #11

                        @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


                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        1 Reply Last reply
                        1
                        • O Offline
                          O Offline
                          ODБOï
                          wrote on 2 Sept 2020, 13:03 last edited by ODБOï 9 Feb 2020, 13:03
                          #12

                          Ok thanks everybody

                          1 Reply Last reply
                          0
                          • O ODБOï
                            2 Sept 2020, 11:18

                            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 ?
                            thx

                            P Offline
                            P Offline
                            Pablo J. Rogina
                            wrote on 2 Sept 2020, 13:21 last edited by
                            #13

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

                            Upvote the answer(s) that helped you solve the issue
                            Use "Topic Tools" button to mark your post as Solved
                            Add screenshots via postimage.org
                            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                            1 Reply Last reply
                            1

                            9/13

                            2 Sept 2020, 12:46

                            • Login

                            • Login or register to search.
                            9 out of 13
                            • First post
                              9/13
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved