Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using gettext instead of Qt translation with UI files
Forum Updated to NodeBB v4.3 + New Features

Using gettext instead of Qt translation with UI files

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 5 Posters 2.5k Views 2 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.
  • L Offline
    L Offline
    La Bamba
    wrote on 30 May 2018, 13:52 last edited by La Bamba
    #1

    I'm working on an old application which uses gettext to translate string into different languages. Now this application has Qt5 GUI built using *.ui files.

    I managed to use gettext but with workarounds:

    load(uic)
    uic.commands +=  "--tr qt5gettext --include qt5gettext.h"
    

    where qt5gettext.h is the following:

    #include <libintl.h>
    #define qt5gettext(s,i) gettext(s)
    

    This workaround is required because uic creates the following code:

    label_3->setText(qt5gettext("Time source:", Q_NULLPTR));
    

    Is there any way to have normal gettext calls without workarounds? I.e.:

    label_3->setText(gettext("Time source:"));
    
    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 30 May 2018, 15:51 last edited by
      #3

      Just derive from QTranslator, overwrite translate() ( http://doc.qt.io/qt-5/qtranslator.html#translate ) and call gettext there.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      P L 2 Replies Last reply 30 May 2018, 16:50
      3
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 30 May 2018, 13:57 last edited by mrjj
        #2

        Hi and welcome to the forums
        You mean to change how it generate code from the UI files ?
        Outside of change it and compile a custom Qt version.
        Not as far as i know.

        Ahh, Mr. @Christian-Ehrlicher had a much better idea.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 30 May 2018, 15:51 last edited by
          #3

          Just derive from QTranslator, overwrite translate() ( http://doc.qt.io/qt-5/qtranslator.html#translate ) and call gettext there.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          P L 2 Replies Last reply 30 May 2018, 16:50
          3
          • C Christian Ehrlicher
            30 May 2018, 15:51

            Just derive from QTranslator, overwrite translate() ( http://doc.qt.io/qt-5/qtranslator.html#translate ) and call gettext there.

            P Offline
            P Offline
            Pablo J. Rogina
            wrote on 30 May 2018, 16:50 last edited by
            #4

            @La-Bamba said in Using gettext instead of Qt translation with UI files:

            old application

            I assume old application is frozen, I mean no new strings/translations will be added and if so, they'll be added to new Qt's UI

            which uses gettext to translate string into different languages

            I assume that you have a bunch of .po files for each language you have a translation, i.e. for a Spanish translation file test_es.po:

            #: locationdialog.cpp:228
            msgid "Do you want to quit?"
            msgstr "¿Quiere salir?"
            

            So I'd suggest you use the normal Qt way enclosing your strings to translate with tr() calls (Qt Designer even does it for you) keeping in mind to use all the same original strings contained in .po files (all from msgid lines). From previous example:

            label_3->setText(tr("Do you want to quit?"));
            

            Next step is to convert all your .po files into Qt XML translation files (.ts) with following command (you do this only once per .po file):

            lconvert -locations none test_es.po -o test_es.ts
            

            Example output (test_es.ts):

            <?xml version="1.0" encoding="utf-8"?>
            <!DOCTYPE TS>
            <TS version="2.0" language="es_ES">
            <context>
                <name></name>
                <message>
                    <source>Do you want to quit?</source>
                    <translation>¿Quiere salir?</translation>
                </message>
            </context>
            </TS>
            

            Last step is to "compile" your Qt .ts file into binary format .qm with lrelease. And from now on, all new strings you need will be added to your Qt app source code using tr(), extracted for translation with lupdate and once translated (by using QLinguist for example) converted into binary form with lrelease.

            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
            4
            • C Christian Ehrlicher
              30 May 2018, 15:51

              Just derive from QTranslator, overwrite translate() ( http://doc.qt.io/qt-5/qtranslator.html#translate ) and call gettext there.

              L Offline
              L Offline
              La Bamba
              wrote on 1 Jun 2018, 13:56 last edited by SGaist 6 Jan 2018, 21:04
              #5

              @Christian-Ehrlicher Thanks! Your proposal works great!

              Edit: I can't mark your reply as solution, so I've marked my own to close the thread.

              [edit: fixed the answer to me marked SGaist]

              1 Reply Last reply
              2
              • H Offline
                H Offline
                hyhhyh
                wrote on 24 Jun 2022, 07:13 last edited by hyhhyh
                #6

                HI, How to rewrite translator() , (.ui file); please answer,thanks

                M 1 Reply Last reply 24 Jun 2022, 14:40
                0
                • H hyhhyh
                  24 Jun 2022, 07:13

                  HI, How to rewrite translator() , (.ui file); please answer,thanks

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 24 Jun 2022, 14:40 last edited by
                  #7

                  @hyhhyh
                  Hi and welcome to the forums.
                  Do you mean how to use gettext inside the function ?

                  Else simply inherited from QTranslator and add a new
                  QString QTranslator::translate(const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1) const
                  to it where you use gettext.

                  H 1 Reply Last reply 28 Jun 2022, 09:20
                  0
                  • M mrjj
                    24 Jun 2022, 14:40

                    @hyhhyh
                    Hi and welcome to the forums.
                    Do you mean how to use gettext inside the function ?

                    Else simply inherited from QTranslator and add a new
                    QString QTranslator::translate(const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1) const
                    to it where you use gettext.

                    H Offline
                    H Offline
                    hyhhyh
                    wrote on 28 Jun 2022, 09:20 last edited by
                    #8

                    @mrjj
                    I have solved
                    great, and thank you for sharing the solution to your issue.

                    QString translate(const char *context, const char *sourceText,
                    const char *disambiguation = nullptr, int n = -1) const override;
                    {
                    return QString::fromUtf8(dgettext("ovfui", sourceText));
                    }
                    So you can extract all of your translation with:
                    lupdate
                    Then use lconvert to obtain a po file:
                    lconvert -of po -o file.po file.ts

                    1 Reply Last reply
                    2

                    • Login

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