PDA

View Full Version : Translate Strings sent by a device



mikrocat
28th April 2016, 13:45
Hey,

I read various articles about this problems. But still didn't get how to figure it out.

A connected device is sending me different names like "temperature", "pressure". I want to translate this names depending on the selected language of the app. I have a list of all the names that can be send.

I thought about something writing all names in a .cpp and create a -.ts file but how do I tell that I want to translate all "temperature"-Strings on my ui?



QT_TRANSLATE_NOOP("Names", "temperature");




QString TempName;
for (int j = 7; j < response.size(); ++j){
TempName.append(response.at(j));
}
tempdata->setGroupName(QT_TR_NOOP(TempName));

anda_skoa
28th April 2016, 16:42
Q_TR_NOOP and Q_TRANSLATE_NOOP mark a string for translation which is not passed literally through QObject::tr() or QCoreApplication::translate().
E.g. for then the string value at runtime is generated or external, like in your case.

The actual runtime translation lookup is just the same mechanism as for fixed string, so e.g. a QObject::tr() call with the value you get from the device.

Cheers,
_

mikrocat
29th April 2016, 08:02
But when compiling I get following Error:
"no matching function for call to 'MonitorWindow::tr(const QString&)"


ui->verticalLayoutGroupBox->addWidget( new QLabel( QString("%1: %2").arg( tr(LabelAndInfo.at(0)) ).arg( tr(LabelAndInfo.at(1)) ), this ) );



^

anda_skoa
29th April 2016, 09:43
Yes, tr() expects a const char*, in the encoding you are using for your source files, most likely UTF-8.
See QString::toUtf8() and friends.

Cheers,
_