Chapter 30. Application Internationalization

This chapter reveals why internationalization isn’t an “optional feature” but a strategic decision. Here you’ll discover how a few correct steps allow transforming a local Qt application into a global product. The secret is revealed of how to build support for dozens of languages and regions without complicating architecture and without losing performance.

The chapter covers working with tr(), shows practical scenarios for using lupdate and Qt Linguist, and examines 3 approaches to language switching—including dynamic switching without application restart. Professional teams use exactly these patterns to scale products to the global market.

Skip this chapter—and leave up to 98% of users behind.

This chapter includes ready-to-use code examples.

Chapter Self-Check

Why does the tr() method perform two functions simultaneously in the internationalization process?Answer
Correct answer: First, it marks strings for the lupdate utility, which extracts them into TS files. Second, it performs actual translation of these strings at runtime based on the loaded QTranslator.
What do the abbreviations i18n and l10n mean, and what is the fundamental difference between them?Answer
Correct answer: i18n (internationalization)—preparing an application to work in different languages, l10n (localization)—adaptation to a specific locale (date, number, currency formats, text direction). Internationalization is the technical foundation, localization is cultural adaptation.
Why is it strongly recommended to use English as the source language for translations?Answer
Correct answer: First, English characters are part of standard Unicode, ensuring full compatibility. Second, English is the international programming language, facilitating collaboration and getting help from developers worldwide.
Why does the tr() method provide a second comment parameter for the translator?Answer
Correct answer: The comment clarifies context for ambiguous words, ensuring correct translation. For example, the word “Date” can mean a calendar date or a romantic date—the comment eliminates ambiguity.
Why does the lupdate utility never delete existing translations from TS files?Answer
Correct answer: This protects translators’ already completed work. lupdate only adds new strings and marks obsolete ones but preserves all existing translations, allowing gradual updates without data loss.
What is the role of TS and QM files, and why are both formats used?Answer
Correct answer: TS files (XML)—editable source format for translators. QM files—optimized binary format for fast loading into the application. Separation ensures work convenience and performance.
Why can’t you use string concatenation when creating translatable texts?Answer
Correct answer: Different languages have different grammar and word order. For example, concatenation of “Dear” + name won’t work with feminine names. The translator needs the full sentence context for correct translation considering grammatical rules.
How do you correctly load a translation file into the application and apply it?Answer
Correct answer: Create a QTranslator object, load the QM file into it using the load() method, then install the translator in the application via qApp->installTranslator(). All strings wrapped in tr() will automatically be translated.
Why is it recommended to embed QM files in application resources?Answer
Correct answer: Embedding in resources reduces the number of files in the package, eliminates the risk of accidental loss or deletion of translation files by the user, and simplifies application deployment.
What three methods of runtime language switching exist, and which is most preferable?Answer
Correct answer: 1) Save choice and restart; 2) Destroy and recreate main window; 3) Override changeEvent() and call retranslateUi(). The third method is most preferable—it instantly updates the interface without restart.
How does the QLocale class help in application localization?Answer
Correct answer: QLocale ensures correct formatting of numbers, dates, times, and currency according to regional standards. For example, the number 2876.56 displays as “2,876.56” in English locale and “2.876,56” in German.
Why is the QT_TR_NOOP macro needed for deferred translation?Answer
Correct answer: It marks a string for translation but defers actual translation to a later moment. This is useful when strings are defined in one place in the program (e.g., in arrays or constants) but used and translated in another.
What event is generated when changing language, and how to use it to update the interface?Answer
Correct answer: When calling installTranslator(), a QEvent::LanguageChange event is generated. You need to override the changeEvent() method in widgets, check the event type, and call the retranslateUi() method, which reassigns current translations to all strings.

Practical Exercises

Easy Level

Calculator with Two Languages
Create a simple calculator with buttons for basic operations (+, -, *, /). Prepare it for internationalization: wrap all strings in tr(), create translation files for Russian and English. Translate button labels and captions (“Result:”, “Calculator”, etc.).
Hints: Use QPushButton for operation buttons. Add the line TRANSLATIONS = calc_ru.ts calc_en.ts to the .pro file. Run lupdate, translate strings in Qt Linguist, then lrelease. Load translation via QTranslator in main.cpp.

Medium Level

Application with Dynamic Language Switching
Develop an application with menu, text labels, and settings dialog. Implement the ability to switch between three languages (Russian, English, German) right during operation without restart. Add a “Language” menu item with submenu for selection. Use QLocale to display current date according to selected locale.
Hints: Override changeEvent() in the main window to handle QEvent::LanguageChange. Create a retranslateUi() method to update all strings. When changing language, call qApp->removeTranslator() for the old translator, then installTranslator() for the new one. Use QLocale::setDefault() and QDate::currentDate().

Hard Level

Multilingual Text Editor with Localization
Create a text editor with full feature set: file opening/saving, editing, find/replace, text statistics. Implement full internationalization and localization for 3+ languages including Arabic (with RTL support). Use contextual comments for ambiguous terms (“Open” as “Open file” vs “Open status”). Handle plural forms for statistics (“1 word”, “2 words”, “5 words”). Integrate Qt standard dialog translations. Save selected language between launches.
Hints: Use QSettings to save language. Load Qt translations from QLibraryInfo::path(QLibraryInfo::TranslationsPath). For RTL use qApp->setLayoutDirection(Qt::RightToLeft). Third parameter of tr() for plural forms. Create a centralized LanguageManager class to manage translations. Embed all QM files in resources (:/translations/).

💬 Join the Discussion!

Figured out Qt application internationalization? Have questions about working with lupdate/lrelease or dynamic language switching?

Encountered localization specifics for different cultures? Share experience working with RTL languages or date and number formatting!

Your experience can help other developers avoid typical internationalization mistakes.

Leave a Reply

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