Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Localization of strings defined in QtDesigner



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


  • Qt Champions 2017

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



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


  • Qt Champions 2017

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



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


  • Qt Champions 2017

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



  • Thank you, I will.

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

    Cheers for you help!


Log in to reply