[mythtv] [mythtv-commits] mythtv commit: r18328 by nigel

Daniel Kristjansson danielk at cuymedia.net
Fri Sep 19 04:41:22 UTC 2008

On Fri, 2008-09-19 at 02:22 +0000, mythtv at cvs.mythtv.org wrote:
> A few more Qt3isms gone. See #5596. I hadn't realised that QObject::tr(QString)
> is now unsafe. Its one of the few Qt methods that uses a char * instead of a
> QString.  TrollTech should provide a QString one?

They probably don't provide a QString one because they frown upon code
like this:
  QString group = (run) ? "Run" : "Walk";
  QString group_translated = QObject::tr(group);

The reason is because the i18n tools won't know that "Run" and "Walk"
are two words that need to be translated. So unless you have some other
process in place to keep track of this code group_translated will always
contain the English language version.

The Trolltech is encouraging you to write this instead:
  QString group = (run) ? "Run" : "Walk";
  QString group_translated =
      (run) ? QObject::tr("Run") : QObject::tr("Walk");

Or if group can take on a number of values, including user defined ones:
  QMap<QString,QString> group_map;
  group_map["Run"]  = QObject::tr("Run");
  group_map["Walk"] = QObject::tr("Walk");
  QString group_translated = group_map[group];

The idea is to enumerate all the strings that need translating in the
code itself, so that the i18n tools can pick the strings up from the
code and ask the person doing the translation to provide translations.

In theory this code is just as bad:
  const char *group = (run) ? "Run" : "Walk";
  QString group_translated = QObject::tr(group);
  QString group = (run) ? "Run" : "Walk";
  QString group_translated = QObject::tr(group.toLatin1().constData());

But they are just trying to prevent you from accidentally breaking
i18n. By not providing a QString version you usually have to really
work hard to shoot yourself in the foot :^)

-- Daniel

More information about the mythtv-dev mailing list