简体   繁体   中英

Translate String Representation of Enum Values Qt

Is it possible to translate the string representation of an enum (ie via QMetaEnum ) using Qt's translation system?

What I think I need is for some way to get lupdate to pick up the enum strings for translation, either by emitting some QT_TR_NOOP() code if and only if the file is being processed by lupdate, or by modifying the behavior of lupdate / moc themselves.

For example, my application presents "settings" options to users via a dialog box. All of the settings are defined as Q_PROPERTY 's. Options which use an enum are presented as a combo box and the text options for the combo box use the QMetaEnum::key() as the displayed text. Below is some pseudo-code to get the point across.

Widgets will have some enum property defined like:

class SomeWidget : public QWidget
{
    Q_OBJECT
    Q_ENUMS( Configuration );

    enum Configuration
    {
        Config_Blue = 0,
        Config_Green,
        Config_Invisible,
        Config_Backwards
    };

    Q_PROPERTY( Configuration READ Configuration WRITE SetConfiguration );
};

The combo widget creation is done by a separate settings manager and goes something like this:

QWidget* SettingsItem::CreateWidget()
{
    const QMetaObject* pMetaObj = this->m_pWidget->metaObject();
    const QMetaProperty& rcProp = pMetaObj->property( this->m_iProp );

    QMetaEnum cEnum = rcProp.enumerator();
    if( cEnum.isValid() )
    {
        QComboBox* pRetWidget = new QComboBox;
        for( int i = 0; i < cEnum.keyCount(); ++i )
        {
            int iVal = cEnum.value( i );
            QString strKey = cEnum.key( i );  // Translate here?
            pRetWidget->addItem( strKey, iVal );
        }
    } 

    ...
    return pRetWidget;
}

The combo box in the settings dialog shows "Config_Blue", "Config_Green", etc. I would like it to just say "Blue", "Green", etc. in whatever the current language is.

For the rest of the code we use Qt's translation system and the linguist utility which works really nice. I was hoping that I could enable this type of translation for the enum 's as well without having to add string literals and tr() 's into the code manually. Basically all of the information that a translator would need is already there in the code, I just need linguist to be able to identify these enum values as translatable. I can then invoke tr() when the combo box is populated.

What I think I need is for some way to get lupdate to pick up the enum strings for translation, either by emitting some QT_TR_NOOP() code if and only if the file is being processed by lupdate, or by modifying the behavior of lupdate / moc themselves.

I was thinking maybe a macro would work, might not be possible though. Perhaps modifying some Qt code is necessary?

An enum identifier shouldn't be user-visible to begin with. These are identifiers that should be meaningful to the developer, not to the user. Exposing them directly to the user adds a strong coupling between the internal design of the code and user-visible behavior. That is best avoided.

You need to map between enum values and user-visible strings. The values in the map should be subject to translation, and the map should be re-populated upon retranslation. Eg:

class Object : public QObject
{
    Q_OBJECT
    Q_ENUMS(Configuration);
    Q_PROPERTY(Configuration READ Configuration WRITE SetConfiguration);

    enum Configuration {
        Config_Blue, Config_Green, Config_Invisible, Config_Backwards
    };
public:
    static QString toString(Configuration c) {
      switch (c) {
      case Config_Blue: return tr("Blue", "Configuration");
      case Config_Green: return tr("Green", "Configuration");
      case Config_Invisible: return tr("Invisible", "Configuration");
      case Config_Backwards: return tr("Backwards", "Configuration");
      }
    }
};

You can now translate the configuration values.

I'm using a dedicated function within the class in which I define my enum. Is it helpful? (Qt5.13).

I call it like MyClass::toTrString(myEnumVal);

class MyClass : public QObject
{
    Q_OBJECT
public:
    explicit MyClass(QObject *parent = nullptr);
    enum MyEnum {
            myKey1,
            myKey2,
            ...
    };
    Q_ENUM(MyEnum)
    static QString toTrString(MyEnum t) {
        return tr(qPrintable(QVariant::fromValue(t).toString()));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM