3
QObject::tr("%1").arg(_value);

Here _value is of QString type, which is dynamically generated. Is the above way correct to translate dynamically generated strings as in my code it doesn't seem to work.

Kartik
  • 45
  • 1
  • 4
  • Of course not, that's going to request and produce a translation for the string `"%1"`. – peppe Jul 28 '14 at 20:03
  • So what exactly is the correct way? Any examples? – Kartik Jul 28 '14 at 20:05
  • I'm not even sure what you're trying to do: is `_value` the value you'd like to translate? – peppe Jul 28 '14 at 20:12
  • Yes I would like to translate _value – Kartik Jul 28 '14 at 20:13
  • @Kartik How do you ensure that the set of values that `_value` can take ends up in the translation file and is translated by your translators? Remember that `tr` simply does a lookup in a list. The string to be translated must be on that list *first*. – Kuba hasn't forgotten Monica Jul 28 '14 at 20:18
  • @KubaOber `tr()` looks for translations in a list, and if not found then it uses the string given in the code. For example `tr("this string is not in the list")` produces *"this string is not in the list"*. So I don't see why the example in the question won't work. I'm surprised that there's no way to look for the value of `_value` in the list. – Alaa M. Jan 03 '16 at 13:26
  • @AlaaM. You should translate whatever you put into the `_value`, at its source. Those are necessarily literals, otherwise you'd get unmanageable mess. Since `_value` contains literals, the proper use of `tr` is to translate them at their location, not after they've been passed through variables. – Kuba hasn't forgotten Monica Jan 04 '16 at 13:37

3 Answers3

6

There are two steps:

1. Make Qt extract the strings for translation.

This means using one of

  • tr() in a QObject subclass
  • QCoreApplication::translate()
  • QT_TR_NOOP / QT_TRANSLATE_NOOP

lupdate will extract the strings passed to those functions/macros, and make them available to linguist for translation.

2. Performing the translation (i.e. the "lookup")

This is again done by tr() and QCoreApplication::translate(). So for instance:

// marking the strings for extraction
static const char *strings[] = { 
    QT_TRANSLATE_NOOP("MyContext", "hello"), 
    QT_TRANSLATE_NOOP("MyContext", "world"); 
};
// performing the translation at runtime
qApp->translate("MyContext", strings[0]);

There's a ton of documentation about the whole process, see here.

peppe
  • 21,934
  • 4
  • 55
  • 70
2

You perhaps meant to do:

QObject::trUtf8(QString("%1").arg(_value).toUtf8(), "dynamic1");

You must ensure that your translation file contains all values that _value can take with the dynamic1 for the disambiguation value, iff you wish to disambiguate them, that is.

Of course, the _value must be selected from a fixed list of strings anyway - since tr isn't a human translator, it simply does a lookup of the string in a translation list.

So, you should really do this:

QString value;

select (variant) {
  case VarA: value = QObject::tr("foo"); break;
  case VarB: value = QObject::tr("bar"); break;
  ...
}

That way the relevant strings will be included in the translation list.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
0

You're trying to translate _value in the wrong place. As stated in other answers, QObject::tr() can't guess by itself how to translate anything. It works only on fixed strings. You should mark constants you're setting _value to for translation, not _value itself.

Marc Plano-Lesay
  • 6,808
  • 10
  • 44
  • 75