Mastering Translation Workflows in Qt Linguist

Getting Started with Qt Linguist: A Beginner’s GuideQt Linguist is the official tool from the Qt project for translating user-visible text in Qt applications. It works with Qt’s translation system (the tr() mechanism), helping developers and translators collaborate to produce localized applications. This guide walks you through the basic concepts, installation, preparing a project for translation, working with Qt Linguist, and common workflows for integrating translations into your build and release process.


What Qt Linguist does and why it matters

  • Qt Linguist extracts translatable strings from source code and resource files into TS files (XML-based translation source files).
  • Translators edit TS files in a user-friendly GUI, providing translations, notes, and status for each string.
  • Qt tools convert TS files into machine-friendly QM files used at runtime by Qt-based applications.

Using Qt Linguist separates translation from code changes, enabling non-developers to manage localization and allowing developers to focus on code.


Installing Qt Linguist

Qt Linguist is distributed as part of the Qt toolset. You have several options:

  • Install via the official Qt online installer from the Qt website (recommended for full Qt development environment).
  • On many Linux distributions, Qt Linguist is available from package managers (package names may vary; look for qt-linguist or qttools5-linguist).
  • For Windows and macOS, install the Qt tools bundle or use Qt Creator (which includes Linguist tools).

After installation, you should have tools such as lupdate, lrelease, and linguist available. lupdate extracts strings to TS; linguist edits them; lrelease compiles TS to QM.


Preparing your project for translation

  1. Mark strings in code:

    • In C++ use QObject::tr(“Text”) or tr(“Text”) inside QObject-derived classes.
    • For global or non-QObject functions use QCoreApplication::translate(“Context”, “Text”).
    • In QML use qsTr(“Text”) or qsTranslate(“Context”, “Text”).
  2. Add a .pro or CMake setup to invoke lupdate:

    • For qmake (.pro), ensure TRANSLATIONS += myapp_en.ts myapp_fr.ts etc.
    • For CMake, use the Qt5/Qt6 translation helpers (update TS files with lupdate or use the qt_add_translation helper if available).
  3. Keep context meaningful: Context (usually the class name) helps translators understand where text appears.


Extracting translatable strings with lupdate

Run lupdate to scan your sources and update TS files. Example commands:

  • Using qmake project:

    lupdate myproject.pro 
  • Using file lists:

    lupdate src/*.cpp qml/*.qml -ts translations/myapp_en.ts 

lupdate merges new strings into existing TS files, preserves existing translations, and marks obsolete strings.


Working in Qt Linguist (the GUI)

Open a TS file in Qt Linguist. The interface typically includes:

  • A list/tree of contexts and source strings.
  • Source text, comments, and translator notes.
  • Translation entry field and status controls (e.g., Finished, Unfinished).
  • Tools for searching, filtering, and batch operations.
  • Validation and linguistics checks (e.g., placeholders, accelerators, plurals).

Key steps:

  1. Select a string; read the source text and any developer comments.
  2. Enter the translation into the target field. Respect placeholders like %1, %n, and markup.
  3. Mark the entry as Finished when complete.
  4. Use the Preview to see how the translation might render.
  5. If multiple plural forms exist, supply them all and test with different counts.

Tips:

  • Preserve formatting tags and placeholders exactly.
  • For accelerator keys (e.g., “&File”), maintain a unique accelerator per menu and keep the ampersand in the translated string, moving it where appropriate for the language.
  • Add translator comments in code with QT_TR_NOOP or by passing comments to tr() so translators understand context.

Handling plural forms

Qt supports plural forms using tr() variants and QML’s qsTrPlural. In Qt Linguist, plural entries present multiple target fields for each plural case. Ensure your translation covers every required plural form for the target language.


Validating translations

Use Linguist’s built-in checks to catch problems:

  • Missing or mismatched placeholders (%1, %2).
  • Untranslated strings left as source text.
  • Duplicate accelerators or missing accelerators.
  • Untranslated plural forms.

Run lrelease only after resolving critical warnings.


Compiling translations (lrelease)

Convert TS files to binary QM files used by the application:

lrelease translations/myapp_fr.ts -qm translations/myapp_fr.qm 

Or, for multiple files:

lrelease translations/*.ts 

Integrate lrelease into your build system so QM files are produced during build or packaging.


Loading translations at runtime

In C++:

QTranslator translator; translator.load(":/translations/myapp_fr.qm"); qApp->installTranslator(&translator); 

For QML, you can expose translations via QTranslator and retranslate UI when language changes. Watch for retranslation patterns and use retranslateUi for widgets created from .ui files.


Continuous localization workflows

  • Keep TS files under version control.
  • Use CI to run lupdate and flag new/changed strings so translators are notified.
  • Automate lrelease in CI to produce QM artifacts for nightly builds.
  • Consider using translation management platforms that consume TS files if your team is large.

Common pitfalls and solutions

  • Missing translator context: add comments in code (third parameter to tr) or use QT_TR_NOOP for non-immediate strings.
  • Broken placeholders: always check placeholder order and formats.
  • Hard-coded strings: audit code to ensure all user-visible text is wrapped in tr()/qsTr().
  • Out-of-date TS files: run lupdate regularly and merge carefully.

Example minimal workflow

  1. Developer marks strings with tr()/qsTr().
  2. Run lupdate to update TS files.
  3. Translators use Qt Linguist to edit TS files.
  4. Run lrelease to produce QM files.
  5. Application loads QM files at startup or on language change.

Resources

  • Qt documentation (search for Qt Linguist, lupdate, lrelease).
  • Qt examples in the Qt installation that show translation workflows.
  • Community guides and forums for language-specific plural and formatting advice.

If you want, I can: provide a ready-to-run minimal example project (CMake + simple UI) with translations, or translate specific UI strings into a target language. Which would you prefer?

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *