Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QObject::tr() not translating dynamically generated strings

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.

like image 902
Kartik Avatar asked Oct 14 '25 09:10

Kartik


1 Answers

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.

like image 61
peppe Avatar answered Oct 17 '25 23:10

peppe



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!