Detecting Un-marked Strings for Translation in Qt Applications

Internationalization and localization are important aspects of UI/UX design for user-facing applications. Qt offers a consistent and performant API for internationalizing source code, as well as tools to easily configure and deploy translations (read more about Internationalization with Qt). 

For C++ code, there are several methods to mark strings for translation: 

  • QT_TR_NOOP macro for global data strings 
  • QCoreApplication::translate() function outside of QObject sub-classes
  • tr() function inside QObject sub-classes

The two functions QCoreApplication::translate() and tr() also perform a runtime look-up to return an appropriate translation (read more about Writing Source Code for Translation). 

In large applications, with many windows, dialogs and widgets, it can be difficult to ensure that all user-visible strings are correctly marked for translation. For this reason, Axivion Static Code Analysis provides the Qt-Generic-MissingTrCall static analysis rule to detect un-marked strings. 

Within a Qt application, user-visible strings are typically provided to the constructors of QWidget sub-classes like QPushButton and QLabel or to utility functions like QMessageBox::information(). Using this as a starting point and combined with Axivion Static Analysis’ understanding of Qt semantics, we are able to find locations where strings are used without a call to tr()

QtUnmarkedStrings-1

Warnings can even be provided when the offending strings are passed through several layers of calls: 

QtUnmarkedStrings-2

In addition to this rule, Axivion Static Code Analysis provides many other useful checks for Qt code including the string related Qt-QStringArg to detect incorrect usages of QString::arg() and Qt-QLatin1StringNonAscii to find usages of QLatin1String with non-ASCII characters. 

Static Code Analysis is often thought of as a tool for safety and security critical applications, but as seen here, it can also be used to improve the code quality and user-experience of desktop and mobile applications. 

 

Please visit Axivion to learn more about Axivion Static Code Analysis and Axivion Architecture Verification.

To schedule an appointment for a free demo, please contact us.

 

Comments