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. Localization of strings defined in QtDesigner
QtWS25 Last Chance

Localization of strings defined in QtDesigner

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtdesignerlocalization
7 Posts 2 Posters 2.6k 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.
  • E Offline
    E Offline
    enmaniac
    wrote on last edited by enmaniac
    #1

    Hello all,

    I have a dialog with some QLabel created with the QtDesigner. The label contains some text with few placeholders, something like:

    This is item %1. You have %n references to it.

    Now, in the source code I would like to supply values for both placeholders. Unfortunetely, I have no clue how I can do this.
    I could move the whole label text into the code and use tr() but I would prefer to have it defined via QtDesigner.

    Is it possible to achieve what I am looking for ?

    Thank you all in advance!

    Cheers!

    kshegunovK 1 Reply Last reply
    0
    • E enmaniac

      Hello all,

      I have a dialog with some QLabel created with the QtDesigner. The label contains some text with few placeholders, something like:

      This is item %1. You have %n references to it.

      Now, in the source code I would like to supply values for both placeholders. Unfortunetely, I have no clue how I can do this.
      I could move the whole label text into the code and use tr() but I would prefer to have it defined via QtDesigner.

      Is it possible to achieve what I am looking for ?

      Thank you all in advance!

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by kshegunov
      #2

      @enmaniac
      Hello,
      How are you loading your form?
      Usually you can access the label by its object name (specified in the creator) in your Ui::FormName generated class' instance. Let's say you have the form Ui class Ui::MyFormUiClass and you have MyDialog dialog holding an instance of the generated class. (Commonly) In the constructor you call the ui.setupUi() function of Ui::MyFormUiClass, and after that your label is available through that instance. An example:

      class MyDialog : public QDialog
      {
      public:
          MyDialog(QWidget * parent = NULL);
          // ... Some other code
      private:
          Ui::MyFormUiClass ui;
      };
      
      MyDialog::MyDialog(QWidget * parent)
          : QDialog(parent)
      {
          ui.setupUi(this);    // This initializes your dialog with the form
          // Here you can now access the label
          QString labelText = ui.myLabelObjectName->text().arg(...).arg(...);   // Put your strings in
          ui.myLabelObjectName->setText(labelText);   // Update the text in the label
      }
      

      Kind regards.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • E Offline
        E Offline
        enmaniac
        wrote on last edited by
        #3

        Good morning Kshegunov,

        Yes, this is exactly how I load my label. I can access it via UI object in my class.
        Now MyDialog has a method to supply the arguments for placeholders, for instance:

        void updateDescription(const QString& itemName, int itemReferences);
        

        First parameter should substitute %1 in the label text and second %n.
        I can retrieve a text from label and substitute first argument easily:

        QString finalDescription = m_ui->myLabel->text().arg(itemName);
        

        But how can I apply second parameter to replace %n ?

        If I continue with the args, ie:

        QString finalDescription = m_ui->myLabel->text().arg(itemName).arg(itemReferences);
        

        %n does not get replaced and I am seeing the warning message:

        QString::arg: Argument missing: "<html><head/><body><p>You are about to remove <span style=" font-weight:600;">avatar.PNG</span>. This item is <span style=" font-weight:600;">referenced %n times</span>.<br/><br/>What do you want to do with reference items ?</p></body></html>" , 1

        If I had the same string present in the source code of my dialog class I could use tr() like the following:

        QString finalDescr = tr("This is %1 and has %n references", 0, referenceCount).arg(itemName);
        

        But I wonder if it is also possible to somehow retrieve the label's original text with placeholders and apply tr().arg() to it.

        Cheers!

        Cheers!

        1 Reply Last reply
        0
        • kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          @enmaniac
          Hello,
          %n is not a valid placeholder for arg(). Use %1, %2 and so on to indicate the order in which arg() should replace the placeholders. In your case simply replacing the %n with %2 should solve it. Additionally text put in the QLabel with QtCreator is translatable by default, so you shouldn't in principle worry about that.

          Kind regards.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0
          • E Offline
            E Offline
            enmaniac
            wrote on last edited by
            #5

            Hi there.

            Indeed, arg() is not valid for %n. I simply tried it out to see what will happen.
            Replacing %n with %2 wont make a trick since in the translation I need to properly handle plural forms, ie.

            for %n == 1, the text should look like:

            This is SOMETHING and has 1 reference

            and for %n > 1:

            This is SOMETHING and has 25 references

            For other languages these rules are even more complicated.

            So it boils down to how I can provide the value for %n if the text which contains it is created via QtDesigner.

            Cheers!

            Cheers!

            kshegunovK 1 Reply Last reply
            0
            • E enmaniac

              Hi there.

              Indeed, arg() is not valid for %n. I simply tried it out to see what will happen.
              Replacing %n with %2 wont make a trick since in the translation I need to properly handle plural forms, ie.

              for %n == 1, the text should look like:

              This is SOMETHING and has 1 reference

              and for %n > 1:

              This is SOMETHING and has 25 references

              For other languages these rules are even more complicated.

              So it boils down to how I can provide the value for %n if the text which contains it is created via QtDesigner.

              Cheers!

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by kshegunov
              #6

              @enmaniac
              Hello,
              I see now what you mean. Unfortunately, I'm not quite sure how you can achieve that. Maybe looking up the lingust manual or the internationalization documentation might help you.

              Kind regards.

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              0
              • E Offline
                E Offline
                enmaniac
                wrote on last edited by
                #7

                Thank you, I will.

                For the time being I am gonna move the label text into source code.

                Cheers for you help!

                Cheers!

                1 Reply Last reply
                0

                • Login

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