Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt convert translated text to english

Tags:

qt

My Qt application supports dynamic translation IE the user can change languages whilst the application is running

Now I have a need to find the English equivalent of a translated string and don't seem to be able to find a way

For example Given QString s = tr("Hello"); I need to be able to get "Hello" from s after translation has taken place.

Has anyone done this before or have any ideas on how (if) it can be achieved

Thanks

like image 822
user1414413 Avatar asked Nov 16 '25 09:11

user1414413


1 Answers

In my application I needed to write translated messages to UI and original messages to program log file.

My solution was to create wrapper class that could both translated and original data.

class TS {
public:
    TS(const char* str) {
        init(str);
    }

    TS(const QString& str) {
        init(str.toStdString().c_str());
    }

    TS& arg(const QString& arg1, const bool translate = true) {
        this->orig = this->orig.arg(arg1);
        if (translate) {
            this->tran = this->tran.arg(qApp->translate("", arg1.toStdString().c_str()));
        } else {
            this->tran = this->tran.arg(arg1);
        }
        return *this;
    }

    TS& arg(const int arg1) {
        this->orig = this->orig.arg(arg1);
        this->tran = this->tran.arg(arg1);
        return *this;
    }

    inline const QString& getOrig() const {
        return this->orig;
    }

    inline const QString& getTran() const {
        return this->tran;
    }

private:
    inline void init(const char* str) {
        this->orig = str;
        this->tran = qApp->translate("", str);
    }

private:
    QString orig;
    QString tran;
};

Usage:

void info(const TS& rsMsg);

...

m_rLog.info(QT_TRANSLATE_NOOP("", "Plain Text"));
m_rLog.info(TS(QT_TRANSLATE_NOOP("", "Text with argument : %1")).arg( 123 ));
like image 154
Mykhailo Bryzhak Avatar answered Nov 18 '25 05:11

Mykhailo Bryzhak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!