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
Forum Updated to NodeBB v4.3 + New Features

Translate texts read from a file

Scheduled Pinned Locked Moved Solved Brainstorm
13 Posts 4 Posters 2.4k Views 3 Watching
  • 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.
  • ODБOïO Offline
    ODБOïO Offline
    ODБOï
    wrote on 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.HilkJ 1 Reply Last reply
    0
    • ODБOïO ODБOï

      @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.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by J.Hilk
      #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
      • ODБOïO Offline
        ODБOïO Offline
        ODБOï
        wrote on last edited by
        #6

        Very nice thank you for your answers!

        1 Reply Last reply
        1
        • ODБOïO ODБOï

          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

          Pablo J. RoginaP Offline
          Pablo J. RoginaP Offline
          Pablo J. Rogina
          wrote on 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

          ODБOïO 1 Reply Last reply
          1
          • Pablo J. RoginaP Pablo J. Rogina

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

            ODБOïO Offline
            ODБOïO Offline
            ODБOï
            wrote on 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.HilkJ 1 Reply Last reply
            0
            • ODБOïO ODБOï

              @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.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on 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.

              ODБOïO 1 Reply Last reply
              0
              • J.HilkJ J.Hilk

                @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

                ODБOïO Offline
                ODБOïO Offline
                ODБOï
                wrote on 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.HilkJ 1 Reply Last reply
                0
                • ODБOïO ODБOï

                  @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.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on 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
                  • ODБOïO Offline
                    ODБOïO Offline
                    ODБOï
                    wrote on last edited by ODБOï
                    #12

                    Ok thanks everybody

                    1 Reply Last reply
                    0
                    • ODБOïO ODБOï

                      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

                      Pablo J. RoginaP Offline
                      Pablo J. RoginaP Offline
                      Pablo J. Rogina
                      wrote on 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

                      • Login

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