SlideShare a Scribd company logo
1 of 49
Download to read offline
Integrated Computer Solutions Inc. www.ics.com
Qt Internationalization
Localizing with Qt and QML
1
Visit us at http://www.ics.com
Produced by Integrated Computer Solutions
Material based on Qt 5.12
Copyright 2019, Integrated Computers Solutions, Inc.
This work may not be reproduced in whole or in part without the express written consent of Integrated Computer Solutions, Inc.
Integrated Computer Solutions Inc. www.ics.com
Qt Internationalization
2
➢ What is Internationalization?
➢ Traditional Qt (C++)
➢ Qt Quick (QML)
➢ Generating and Loading Translations
➢ Qt Linguist
➢ Advanced Topics
➢ Common Errors and Useful Tips
➢ Demo
2
Integrated Computer Solutions Inc. www.ics.com
What is Internationalization?
3
● internationalization, i18n:
● localization, l10n :
3
"Localization is the process of adapting software for a specific region or
language by adding locale-specific components and translating text."*
"Internationalization is the process of designing a software application so that
it can be adapted to various languages and regions without engineering
Changes."*
*Source Wikipedia
Integrated Computer Solutions Inc. www.ics.com
What is a Localization?
4
Not just the language for UI strings, but also:
● Time and Date
● Currency, Number Formatting, Paper Sizes
● Measurement Units (metric versus imperial)
● Fonts
● Icons, Images, other Multimedia files
● Text encoding format
● Text direction
● Widget appearance (size and layout)
● Cultural differences
4
Integrated Computer Solutions Inc. www.ics.com
Costs of a Localization?
5
● Translations can be expensive @ 20¢+/word.
● More Localizations == More chances for QA issues.
● Translation is a skill, people typically don't make good or efficient
translators even if they are fluent in the relevant language.
● Don't underestimate the effort involved in the translation portion of
localization
● Plan! It can be costly and more time consuming to add support later.
● String Freezes give translators time to work, and test.
● Use precise language.
● UX and Localization work together to prevent QA for locales
5
Integrated Computer Solutions Inc. www.ics.com
Localization with Qt
6
Qt makes localization painless for developers:
● Unicode throughout
● QtCreator creates Unicode source files by default.
● Provides localization classes
● Layout managers will adjust based on text string lengths
● Qt can handle right-to-left and left-to-right languages.
● Includes Qt Linguist for managing UI strings
○ .ts files are XML and can be edited by hand if desired
○ .qm files are binary and more efficient to load at run time
6
Integrated Computer Solutions Inc. www.ics.com
Qt localization workflow
7
myapp.cpp
myapp_fr.ts
myapp_ru.ts
myapp_fr.qm
myapp_ru.qm
myapp
Strings extracted
with lupdate
Translators update files
Compiled with lrelease
Loaded with QTranslator.load()
myui.ui
myapp_ru.ts
myapp_fr.ts
7
Integrated Computer Solutions Inc. www.ics.com
Qt Internationalization
8
➢ What is Internationalization?
➢ Traditional Qt (C++)
➢ Qt Quick (QML)
➢ Generating and Loading Translations
➢ Qt Linguist
➢ Advanced Topics
➢ Common Errors and Useful Tips
➢ Demo
8
Integrated Computer Solutions Inc. www.ics.com
Localization with Qt - UI Strings
9
● Distinguish user-visible localizable strings from non UI strings
● Use QString / QChar, they are Unicode
● Avoid using std::string / char they are not Unicode.
● To Prevent automatic casts between QString and char*.
DEFINES += QT_NO_CAST_TO_ASCII QT_NO_CAST_FROM_ASCII
● Casting to char* or char may be unavoidable when interfacing
with other libraries. You can use the QString conversion
operators: QString::utf8(), QString::toUtf8() etc...
9
Integrated Computer Solutions Inc. www.ics.com
Localization with Qt - UI Strings
10
● All literal UI strings are wrapped in localization
functions
● Replaces string with translation at run-time based on
locale
● Usually use tr() function
● Example: QPushButton *b = new QPushButton(tr("Save"));
10
Integrated Computer Solutions Inc. www.ics.com
Localization with Qt - QObject::tr()
11
● Returns a translated version of the source text by searching
the loaded qm files.
● Flag the string for inclusion in generated ts files.
● Uses the owning QObject as its context.
● QCoreApplication::translate() is similar to tr() but requires a
context to be set.
What does it do?
11
Integrated Computer Solutions Inc. www.ics.com
Localization with Qt - QObject::tr()
12
● Can use structured comments to annotate a string with
information for translators
● This works in both C++ and QML
//: This refers to a network address.
addressLabel->setText(tr("Address:"));
/*: This refers to the age of a network packet. */
QString packetAge = tr("Age");
//= freeLibre
//: free as in freedom
QString fileText = tr("Free");
//= freeBeer
//: free as in beer
QString fileText = tr("Free");
12
Integrated Computer Solutions Inc. www.ics.com
Localization with Qt - QObject::tr()
13
● Optional second argument can be used to disambiguate
identical strings that have different meanings in different
contexts
● For example:
○ tr("free", "as in beer")
○ tr("free", "as in freedom")
● Above are handled as two distinct strings to translate (in
French the first might be translated as "gratuit" and the second
"libre")
● Optional third argument used to handle plurals (example later)
13
Integrated Computer Solutions Inc. www.ics.com
Localization with Qt - Examples
14
BAD, string cannot be localized
QString s = "Hello, world!";
GOOD
QString s = tr("Hello, world!");
14
Integrated Computer Solutions Inc. www.ics.com
Localization with Qt - Examples
15
int i = 58; int j = 5000;
Good, However order may be incorrect for some languages
tr("Copying File ") + i + tr(" of ") + j;
Better, The Whole phrase is provided to translator
tr("Copying File %1 of %2").arg(i).arg(j);
Best, The “L” modifier inserts a localized number
tr("Copying File %L1 of %L2").arg(i).arg(j);
Output(en_US): “Copying File 58 of 5,000”
Output(en_CA): “Copying File 58 of 5 000”
15
Integrated Computer Solutions Inc. www.ics.com
Localization with Qt - Examples
16
int n = messages.count();
Bad, rules for plural forms are different in some languages
if (n == 1)
msg = tr("You have 1 new message.");
else
msg = tr("You have %1 new messages.").arg(n);
Good
msg = tr("You have %n new message(s).", nullptr, n);
16
Integrated Computer Solutions Inc. www.ics.com
Localization with Qt - Examples
1717
Integrated Computer Solutions Inc. www.ics.com
Localization with Qt - Keyboard Accelerators
18
● Keyboard Accelerators can be Localized
● QKeySequence contains common ones.
● Choice of accelerator keys in other languages are left to the
translator
● E.g. CTRL+Q may not exist on Japanese or Chinese user's
keyboard
quitAction = new QAction(tr("&Quit"), this);
quitAction->setShortcut(tr("Ctrl+Q"));
quitAction->setShortcut(QKeySequence::Quit);
18
Integrated Computer Solutions Inc. www.ics.com
Localization with Qt - Static Strings
19
● Need to handle literal strings differently when they are outside of a class, such
as constant strings
● The macro QT_TR_NOOP marks the strings as needing translation so they
will be extracted by lupdate and put in .ts file
● The macro QT_TRANSLATE_NOOP takes a string and a context, typically a
classname, which can be used when calling QCoreApplication::translate().
They need to match to translate
● The macros return the source string, so you still need to call tr() when using
the string in order to translate it
● Non Qt classes can use the Q_DECLARE_TR_FUNCTIONS(classname)
macro to enable tr calls.
19
Integrated Computer Solutions Inc. www.ics.com
Localization with Qt - Static Strings
20
Example:
static const char *baseNames = {
QT_TR_NOOP("adenine"),
QT_TR_NOOP("cytosine"),
QT_TR_NOOP("guanine"),
QT_TR_NOOP("thymine")
};
QString base(int num) {
return tr(baseNames[num]);
}
20
Integrated Computer Solutions Inc. www.ics.com
Qt Internationalization
21
➢ What is Internationalization?
➢ Traditional Qt (C++)
➢ Qt Quick (QML)
➢ Generating and Loading Translations
➢ Qt Linguist
➢ Advanced Topics
➢ Common Errors and Useful Tips
➢ Demo
21
Integrated Computer Solutions Inc. www.ics.com
Localization with Qt Quick (QML)
22
● Wrap all strings in qsTr() function
● Like tr(), supports "%n" replacement and .arg()
○ e.g. qsTr("You are %1 years old.").arg(age())
● Doesn't accept multiple arguments but can use .arg() multiple
times:
● qsTr() can also be used in JavaScript code
var dateHeader =
qsTr("Downloading file %1 of %2").arg(fileNumber()).arg(totalNumberOfFiles())
22
Integrated Computer Solutions Inc. www.ics.com
Localization with Qt Quick (QML)
23
• Can test a translation with qmlviewer or qmlscene using the -translation option,
e.g.
qmlviewer -translation myapp.qm myapp.qml
• See the Internationalization and Localization with Qt Quick section in the Qt
documentation
• In addition to qsTr(), QML also supports
○ qsTranslate()
○ qsTrid()
○ QT_TR_NOOP()
○ QT_TRANSLATE_NOOP()
○ QT_TRID_NOOP()
23
Integrated Computer Solutions Inc. www.ics.com
Localization with Qt Quick (QML)
24
C++ Function QML/JavaScript Function
tr() qsTr()
translate() qsTranslate()
qtTrId() qsTrId()
QT_TR_NOOP() QT_TR_NOOP()
QT_TRANSLATE_NOOP() QT_TRANSLATE_NOOP()
QT_TRID_NOOP() QT_TRID_NOOP()
24
Integrated Computer Solutions Inc. www.ics.com
Qt Internationalization
25
➢ What is Internationalization?
➢ Traditional Qt (C++)
➢ Qt Quick (QML)
➢ Generating and Loading Translations
➢ Qt Linguist
➢ Advanced Topics
➢ Common Errors and Useful Tips
➢ Demo
25
Integrated Computer Solutions Inc. www.ics.com
Localization With Qt: Generating Ts Files QMAKE
26
• List the .ts files in your Qt project file .pro using
TRANSLATIONS variable, e.g.
TRANSLATIONS += myapp_de.ts myapp_fr.ts myapp_it.ts
• Run lupdate to extract/merge strings when UI strings change.
system (lupdate myapp.pro)
$ lupdate myapp.pro
$ lupdate main.cpp main.qml myapp_ru.ts
• lrelease is used to compile .ts files to .qm files
system (lrelease-pro myapp.pro)
$ lrelease-pro myapp.pro
$ lrelease myapp_ru.ts
26
Integrated Computer Solutions Inc. www.ics.com
Localization With Qt: Generating Ts Files CMAKE
27
find_package(Qt5LinguistTools)
set(needsTranslation
Mainwindow.cpp
… More source files
)
set(languageFiles
myApp_en.ts
myApp_fr.ts
myApp_de.ts
)
qt5_create_translation(TSFiles
needsTranslation
languageFiles
)
add_custom_target(TRS ALL DEPENDS ${TSFiles})
Qt5LinguistTools Notes:
● CMake 3.1.0 +
● Provides two macros
qt5_create_translation
qt5_add_translation
find_package(Qt5LinguistTools)
qt5_add_translation(QMFiles
mainwindow.cpp
myApp_en.ts myApp_fr.ts myApp_dr.ts
)
add_custom_target(QMS ALL DEPENDS ${QMFiles})
27
Integrated Computer Solutions Inc. www.ics.com
Localization With Qt: Loading Translations - Runtime
28
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QTranslator qtTranslator;
qtTranslator.load(QString("qtCore_%1")
.arg(QLocale::system().name(),
QLibraryInfo::location(QLibraryInfo::TranslationsPath));
);
app.installTranslator(&qtTranslator);
QTranslator myappTranslator;
myappTranslator.load("myapp_" + QLocale::system().name());
app.installTranslator(&myappTranslator);
...
return app.exec();
}
28
Integrated Computer Solutions Inc. www.ics.com
Localization With Qt: Deploying Translations
29
You can store .qm files in resources.
Advantages:
● Fewer files to distribute with your application
● Always easy to load files with a resource path
Disadvantages:
● Cannot change or add new translations without rebuilding application
● Cannot distribute translations separately from application
You can distribute .qm files as external files.
Advantages:
● Can distribute without the application
● Can add new translations without rebuilding the application
Disadvantages:
● More files to distribute with your application
● Need to get on disk path to load at runtime
29
Integrated Computer Solutions Inc. www.ics.com
Qt Internationalization
30
➢ What is Internationalization?
➢ Traditional Qt (C++)
➢ Qt Quick (QML)
➢ Generating and Loading Translations
➢ Qt Linguist
➢ Advanced Topics
➢ Common Errors and Useful Tips
➢ Demo
30
Integrated Computer Solutions Inc. www.ics.com
Qt Linguist:
31
● Cross Platform GUI Application for working with Qt Translations.
● Translators can get standalone copies of Qt Linguist.
31
Integrated Computer Solutions Inc. www.ics.com
Qt Linguist: Phrase Books
32
● Phrase books Contain Common Phrases used in a language.
● Used as a tool to help speed up translation.
● Provides a translation that Qt Linguist can then suggest when those
phrases are used in your application.
32
Integrated Computer Solutions Inc. www.ics.com
Qt Internationalization
33
➢ What is Internationalization?
➢ Traditional Qt (C++)
➢ Qt Quick (QML)
➢ Generating and Loading Translations
➢ Qt Linguist
➢ Advanced Topics
➢ Common Errors and Useful Tips
➢ Demo
33
Integrated Computer Solutions Inc. www.ics.com
Localization Advanced: Localizers use placeholders
34
● Placeholders:
○ Arg Placeholders:
■ Format: %[L]<number> ex: %1 %L1
■ Can have multiple in a string, always start with %1
■ String order doesn’t matter. “%2, %1” and “%1, %2”
○ Translation function provided
■ Format: %[L]n ex: %n %Ln
■ %n is a number, used with plurals.
○ Optional L Modifier will localize inserted number.
● Accelerators
○ Format: &<character> ex: “e&dit”
○ Localizers are responsible for them.
○ Character that follows the & becomes the accelerated key.
34
Integrated Computer Solutions Inc. www.ics.com
Localization Advanced: Multimedia Files
35
● Use the QResource system, resource prefixes have a
language tag
● Matching prefixes / alias will be replaced based on
language tag
● The best practice is to make your multimedia resources
universal
Image, video, sound files
35
Integrated Computer Solutions Inc. www.ics.com
Localization Advanced: Cultural Differences
36
● Symbols used in images and icons may not be universal to all
cultures (e.g. Red Cross, check mark)
● Colors may not have the same meaning (e.g. red may not
signify danger). Use the Palette to get your colors.
● Some images may be offensive to some cultures (e.g. animals)
● Embedded Fonts may be missing characters
36
Integrated Computer Solutions Inc. www.ics.com
Localization Advanced: Text Codecs
37
● One day hopefully everything will use Unicode.
● File contents may not be encoded in Unicode or 8-bit characters
● May need to use appropriate text codec for locale
● Qt provides most common ones in QTextCodec
● Default text codec is set based on locale
● You may need to set a different text codec (or have the user
select from the UI)
● Note: your source code should be encoded in Unicode.
37
Integrated Computer Solutions Inc. www.ics.com
Localization Advanced: Input Methods
38
● Some languages require input method editor (IME)
● E.g. Chinese has thousands of characters, one to one mapping
of keyboard keys to characters is not possible
● Usually transparent to developer, Qt through standard input
widgets interfaces with input method editors such as Scim when
present
● Typically set up at OS or desktop level
● Just make sure you don't use custom made input widgets
38
Integrated Computer Solutions Inc. www.ics.com
Localization Advanced: Id Based Translations
39
● Strings can be translated by id with qrTsId(“ID”) and qsTrId(“ID”)
● Static Strings are marked with QT_TRID_NOOP(“ID”)
● Id based strings do not get a context.
● Use //% comments for parameters //% “Message Count: %1”
● Good for strings that come from outside your code.
● Must use -idbased option for lrelease when generating .qm files
● It is not recommended to mix ID based and Context based
translations in one application, Mixing them can result in
incomplete translations being generated.
39
Integrated Computer Solutions Inc. www.ics.com
Localization Advanced: Dynamic Language Changes
40
● May want to change language at run time, e.g. from user input
rather from locale
● Requires some extra effort to handle LanguageChange event to
update text
● See "Dynamic Translation" section in Qt documentation
"Internationalization with Qt"
● See section "Dynamic Language Switching" in Blanchette and
Summerfield’ book “C++ GUI Programming with Qt4”
40
Integrated Computer Solutions Inc. www.ics.com
Localization Advanced: Other l10n Methods
41
● You don't have to use Qt localization support
● There are other localization libraries and tools, e.g. GNU gettext
and .po files
● KDE uses their own scheme based on gettext
● Essentially just use different wrapper functions for strings
● For uic generated code, you can change the function used for
translating strings with -tr option
● lconvert tool can convert files between .ts, .po, and .xlf formats
41
Integrated Computer Solutions Inc. www.ics.com
Localization Advanced: lconvert
42
The lconvert tool works with common l10n file types
● Supports the following formats
○ Qts Own formats: Qt Message File (*.qm), Translation Source (*.ts), Qt Phrasebook (*.qph)
○ GNU Gettext formats: Templates (*.pot), GNU Gettext files (*.po)
○ XLIFF Files (*.xlf)
● Concatenate files
○ lconvert - o DESTDIR/myapp_ru.qm qtbase_ru.qm myapp_ru.qm
● Convert Between formats
○ lconvert -of qph -o myapp_fr.qph myapp_fr.ts
● Other options to sort, remove, change language, prune ui file strings , etc…
42
Integrated Computer Solutions Inc. www.ics.com
Qt Internationalization
43
➢ What is Internationalization?
➢ Traditional Qt (C++)
➢ Qt Quick (QML)
➢ Generating and Loading Translations
➢ Advanced Topics
➢ Common Errors and Useful Tips
➢ Demo
43
Integrated Computer Solutions Inc. www.ics.com
Localization With Qt: Common Errors and Problems
44
● Concatenating strings
● Forgetting to localize dates, currency, etc.
● Forgetting to add translator comments for non-obvious strings,
e.g. "%1/%2"
● Not using disambiguation when using the same string for
different roles
● Using tr() on strings that change at run-time
● If directly editing ts files, watch encoding format used by your
editor. Can also be an issue with source files.
● Not Using Locale Aware compare methods when needed.
44
Integrated Computer Solutions Inc. www.ics.com
Localization With Qt: Useful Tips
45
● Generate dummy translations for test purposes (i.e. all
uppercase, additional Length)
○ Can be done with, e.g. a Perl script
○ Google Language Tools (don't expect quality)
● ts files can be edited by hand or scripts as they are XML
● Always, Always Use Layouts.
● QLocale stable but continues to improve
45
Integrated Computer Solutions Inc. www.ics.com
Localization With Qt: Summary
46
Qt makes it easy to localize your application
With a few simple steps:
● Use QString for all user-visible text
● Use tr() or qsTr() for all user-visible literal text
● Use layout managers in dialogs
● Extract strings using lupdate and produce .ts files for each
language
● Load translation files at run-time
46
Integrated Computer Solutions Inc. www.ics.com
Qt Internationalization
47
➢ What is Internationalization?
➢ Traditional Qt (C++)
➢ Qt Quick (QML)
➢ Generating and Loading Translations
➢ Advanced Topics
➢ Common Errors and Useful Tips
➢ Demo
47
Integrated Computer Solutions Inc. www.ics.com 4848
Integrated Computer Solutions Inc. www.ics.comIntegrated Computer Solutions Inc.
Thank You for attending!
Any Questions?
49
webinars@ics.com

More Related Content

What's hot

What's hot (20)

Best Practices in Qt Quick/QML - Part I
Best Practices in Qt Quick/QML - Part IBest Practices in Qt Quick/QML - Part I
Best Practices in Qt Quick/QML - Part I
 
QVariant, QObject — Qt's not just for GUI development
QVariant, QObject — Qt's not just for GUI developmentQVariant, QObject — Qt's not just for GUI development
QVariant, QObject — Qt's not just for GUI development
 
Introduction to QML
Introduction to QMLIntroduction to QML
Introduction to QML
 
Best Practices in Qt Quick/QML - Part 4
Best Practices in Qt Quick/QML - Part 4Best Practices in Qt Quick/QML - Part 4
Best Practices in Qt Quick/QML - Part 4
 
Software Development Best Practices: Separating UI from Business Logic
Software Development Best Practices: Separating UI from Business LogicSoftware Development Best Practices: Separating UI from Business Logic
Software Development Best Practices: Separating UI from Business Logic
 
Qt Qml
Qt QmlQt Qml
Qt Qml
 
Qt for beginners
Qt for beginnersQt for beginners
Qt for beginners
 
Best Practices in Qt Quick/QML - Part III
Best Practices in Qt Quick/QML - Part IIIBest Practices in Qt Quick/QML - Part III
Best Practices in Qt Quick/QML - Part III
 
Best Practices in Qt Quick/QML - Part IV
Best Practices in Qt Quick/QML - Part IVBest Practices in Qt Quick/QML - Part IV
Best Practices in Qt Quick/QML - Part IV
 
Qt for Beginners Part 3 - QML and Qt Quick
Qt for Beginners Part 3 - QML and Qt QuickQt for Beginners Part 3 - QML and Qt Quick
Qt for Beginners Part 3 - QML and Qt Quick
 
Qt Workshop
Qt WorkshopQt Workshop
Qt Workshop
 
Best Practices in Qt Quick/QML - Part 3
Best Practices in Qt Quick/QML - Part 3Best Practices in Qt Quick/QML - Part 3
Best Practices in Qt Quick/QML - Part 3
 
Introduction to the Qt State Machine Framework using Qt 6
Introduction to the Qt State Machine Framework using Qt 6Introduction to the Qt State Machine Framework using Qt 6
Introduction to the Qt State Machine Framework using Qt 6
 
IPC with Qt
IPC with QtIPC with Qt
IPC with Qt
 
UI Programming with Qt-Quick and QML
UI Programming with Qt-Quick and QMLUI Programming with Qt-Quick and QML
UI Programming with Qt-Quick and QML
 
Introduction to the Qt Quick Scene Graph
Introduction to the Qt Quick Scene GraphIntroduction to the Qt Quick Scene Graph
Introduction to the Qt Quick Scene Graph
 
Qt for beginners part 1 overview and key concepts
Qt for beginners part 1   overview and key conceptsQt for beginners part 1   overview and key concepts
Qt for beginners part 1 overview and key concepts
 
Hello, QML
Hello, QMLHello, QML
Hello, QML
 
Qt Application Programming with C++ - Part 1
Qt Application Programming with C++ - Part 1Qt Application Programming with C++ - Part 1
Qt Application Programming with C++ - Part 1
 
Introduction to Qt Creator
Introduction to Qt CreatorIntroduction to Qt Creator
Introduction to Qt Creator
 

Similar to Qt Internationalization

Qt everywhere a c++ abstraction platform
Qt everywhere   a c++ abstraction platformQt everywhere   a c++ abstraction platform
Qt everywhere a c++ abstraction platform
Develer S.r.l.
 
Untitled presentation(4)
Untitled presentation(4)Untitled presentation(4)
Untitled presentation(4)
chan20kaur
 

Similar to Qt Internationalization (20)

Porting Motif Applications to Qt - Webinar
Porting Motif Applications to Qt - WebinarPorting Motif Applications to Qt - Webinar
Porting Motif Applications to Qt - Webinar
 
Porting Motif Applications to Qt - Webinar
Porting Motif Applications to Qt - WebinarPorting Motif Applications to Qt - Webinar
Porting Motif Applications to Qt - Webinar
 
Use C++ and Intel® Threading Building Blocks (Intel® TBB) for Hardware Progra...
Use C++ and Intel® Threading Building Blocks (Intel® TBB) for Hardware Progra...Use C++ and Intel® Threading Building Blocks (Intel® TBB) for Hardware Progra...
Use C++ and Intel® Threading Building Blocks (Intel® TBB) for Hardware Progra...
 
GCF
GCFGCF
GCF
 
Qt everywhere a c++ abstraction platform
Qt everywhere   a c++ abstraction platformQt everywhere   a c++ abstraction platform
Qt everywhere a c++ abstraction platform
 
Building Conclave: a decentralized, real-time collaborative text editor
Building Conclave: a decentralized, real-time collaborative text editorBuilding Conclave: a decentralized, real-time collaborative text editor
Building Conclave: a decentralized, real-time collaborative text editor
 
Qt for beginners part 4 doing more
Qt for beginners part 4   doing moreQt for beginners part 4   doing more
Qt for beginners part 4 doing more
 
Open cl programming using python syntax
Open cl programming using python syntaxOpen cl programming using python syntax
Open cl programming using python syntax
 
OpenCL programming using Python syntax
OpenCL programming using Python syntax OpenCL programming using Python syntax
OpenCL programming using Python syntax
 
Network-Connected Development with ZeroMQ
Network-Connected Development with ZeroMQNetwork-Connected Development with ZeroMQ
Network-Connected Development with ZeroMQ
 
Kubernetes @ Squarespace: Kubernetes in the Datacenter
Kubernetes @ Squarespace: Kubernetes in the DatacenterKubernetes @ Squarespace: Kubernetes in the Datacenter
Kubernetes @ Squarespace: Kubernetes in the Datacenter
 
Crossing the border with Qt: the i18n system
Crossing the border with Qt: the i18n systemCrossing the border with Qt: the i18n system
Crossing the border with Qt: the i18n system
 
c.ppt
c.pptc.ppt
c.ppt
 
.Net introduction by Quontra Solutions
.Net introduction by Quontra Solutions.Net introduction by Quontra Solutions
.Net introduction by Quontra Solutions
 
Qt 6 Chat - Are You Ready?
Qt 6 Chat - Are You Ready?Qt 6 Chat - Are You Ready?
Qt 6 Chat - Are You Ready?
 
Translating Qt Applications
Translating Qt ApplicationsTranslating Qt Applications
Translating Qt Applications
 
Qt for Python
Qt for PythonQt for Python
Qt for Python
 
Qt
QtQt
Qt
 
Untitled presentation(4)
Untitled presentation(4)Untitled presentation(4)
Untitled presentation(4)
 
Qt coin3d soqt
Qt coin3d soqtQt coin3d soqt
Qt coin3d soqt
 

More from ICS

Software Update Mechanisms: Selecting the Best Solutin for Your Embedded Linu...
Software Update Mechanisms: Selecting the Best Solutin for Your Embedded Linu...Software Update Mechanisms: Selecting the Best Solutin for Your Embedded Linu...
Software Update Mechanisms: Selecting the Best Solutin for Your Embedded Linu...
ICS
 

More from ICS (20)

The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Practical Advice for FDA’s 510(k) Requirements.pdf
Practical Advice for FDA’s 510(k) Requirements.pdfPractical Advice for FDA’s 510(k) Requirements.pdf
Practical Advice for FDA’s 510(k) Requirements.pdf
 
Accelerating Development of a Safety-Critical Cobot Welding System with Qt/QM...
Accelerating Development of a Safety-Critical Cobot Welding System with Qt/QM...Accelerating Development of a Safety-Critical Cobot Welding System with Qt/QM...
Accelerating Development of a Safety-Critical Cobot Welding System with Qt/QM...
 
Overcoming CMake Configuration Issues Webinar
Overcoming CMake Configuration Issues WebinarOvercoming CMake Configuration Issues Webinar
Overcoming CMake Configuration Issues Webinar
 
Enhancing Quality and Test in Medical Device Design - Part 2.pdf
Enhancing Quality and Test in Medical Device Design - Part 2.pdfEnhancing Quality and Test in Medical Device Design - Part 2.pdf
Enhancing Quality and Test in Medical Device Design - Part 2.pdf
 
Designing and Managing IoT Devices for Rapid Deployment - Webinar.pdf
Designing and Managing IoT Devices for Rapid Deployment - Webinar.pdfDesigning and Managing IoT Devices for Rapid Deployment - Webinar.pdf
Designing and Managing IoT Devices for Rapid Deployment - Webinar.pdf
 
Quality and Test in Medical Device Design - Part 1.pdf
Quality and Test in Medical Device Design - Part 1.pdfQuality and Test in Medical Device Design - Part 1.pdf
Quality and Test in Medical Device Design - Part 1.pdf
 
Creating Digital Twins Using Rapid Development Techniques.pdf
Creating Digital Twins Using Rapid Development Techniques.pdfCreating Digital Twins Using Rapid Development Techniques.pdf
Creating Digital Twins Using Rapid Development Techniques.pdf
 
Secure Your Medical Devices From the Ground Up
Secure Your Medical Devices From the Ground Up Secure Your Medical Devices From the Ground Up
Secure Your Medical Devices From the Ground Up
 
Cybersecurity and Software Updates in Medical Devices.pdf
Cybersecurity and Software Updates in Medical Devices.pdfCybersecurity and Software Updates in Medical Devices.pdf
Cybersecurity and Software Updates in Medical Devices.pdf
 
MDG Panel - Creating Expert Level GUIs for Complex Medical Devices
MDG Panel - Creating Expert Level GUIs for Complex Medical DevicesMDG Panel - Creating Expert Level GUIs for Complex Medical Devices
MDG Panel - Creating Expert Level GUIs for Complex Medical Devices
 
How to Craft a Winning IOT Device Management Solution
How to Craft a Winning IOT Device Management SolutionHow to Craft a Winning IOT Device Management Solution
How to Craft a Winning IOT Device Management Solution
 
Bridging the Gap Between Development and Regulatory Teams
Bridging the Gap Between Development and Regulatory TeamsBridging the Gap Between Development and Regulatory Teams
Bridging the Gap Between Development and Regulatory Teams
 
IoT Device Fleet Management: Create a Robust Solution with Azure
IoT Device Fleet Management: Create a Robust Solution with AzureIoT Device Fleet Management: Create a Robust Solution with Azure
IoT Device Fleet Management: Create a Robust Solution with Azure
 
Basic Cmake for Qt Users
Basic Cmake for Qt UsersBasic Cmake for Qt Users
Basic Cmake for Qt Users
 
Software Update Mechanisms: Selecting the Best Solutin for Your Embedded Linu...
Software Update Mechanisms: Selecting the Best Solutin for Your Embedded Linu...Software Update Mechanisms: Selecting the Best Solutin for Your Embedded Linu...
Software Update Mechanisms: Selecting the Best Solutin for Your Embedded Linu...
 
Qt Installer Framework
Qt Installer FrameworkQt Installer Framework
Qt Installer Framework
 
Bridging the Gap Between Development and Regulatory Teams
Bridging the Gap Between Development and Regulatory TeamsBridging the Gap Between Development and Regulatory Teams
Bridging the Gap Between Development and Regulatory Teams
 
Overcome Hardware And Software Challenges - Medical Device Case Study
Overcome Hardware And Software Challenges - Medical Device Case StudyOvercome Hardware And Software Challenges - Medical Device Case Study
Overcome Hardware And Software Challenges - Medical Device Case Study
 
User Experience Design for IoT
User Experience Design for IoTUser Experience Design for IoT
User Experience Design for IoT
 

Recently uploaded

Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 

Recently uploaded (20)

WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 

Qt Internationalization

  • 1. Integrated Computer Solutions Inc. www.ics.com Qt Internationalization Localizing with Qt and QML 1 Visit us at http://www.ics.com Produced by Integrated Computer Solutions Material based on Qt 5.12 Copyright 2019, Integrated Computers Solutions, Inc. This work may not be reproduced in whole or in part without the express written consent of Integrated Computer Solutions, Inc.
  • 2. Integrated Computer Solutions Inc. www.ics.com Qt Internationalization 2 ➢ What is Internationalization? ➢ Traditional Qt (C++) ➢ Qt Quick (QML) ➢ Generating and Loading Translations ➢ Qt Linguist ➢ Advanced Topics ➢ Common Errors and Useful Tips ➢ Demo 2
  • 3. Integrated Computer Solutions Inc. www.ics.com What is Internationalization? 3 ● internationalization, i18n: ● localization, l10n : 3 "Localization is the process of adapting software for a specific region or language by adding locale-specific components and translating text."* "Internationalization is the process of designing a software application so that it can be adapted to various languages and regions without engineering Changes."* *Source Wikipedia
  • 4. Integrated Computer Solutions Inc. www.ics.com What is a Localization? 4 Not just the language for UI strings, but also: ● Time and Date ● Currency, Number Formatting, Paper Sizes ● Measurement Units (metric versus imperial) ● Fonts ● Icons, Images, other Multimedia files ● Text encoding format ● Text direction ● Widget appearance (size and layout) ● Cultural differences 4
  • 5. Integrated Computer Solutions Inc. www.ics.com Costs of a Localization? 5 ● Translations can be expensive @ 20¢+/word. ● More Localizations == More chances for QA issues. ● Translation is a skill, people typically don't make good or efficient translators even if they are fluent in the relevant language. ● Don't underestimate the effort involved in the translation portion of localization ● Plan! It can be costly and more time consuming to add support later. ● String Freezes give translators time to work, and test. ● Use precise language. ● UX and Localization work together to prevent QA for locales 5
  • 6. Integrated Computer Solutions Inc. www.ics.com Localization with Qt 6 Qt makes localization painless for developers: ● Unicode throughout ● QtCreator creates Unicode source files by default. ● Provides localization classes ● Layout managers will adjust based on text string lengths ● Qt can handle right-to-left and left-to-right languages. ● Includes Qt Linguist for managing UI strings ○ .ts files are XML and can be edited by hand if desired ○ .qm files are binary and more efficient to load at run time 6
  • 7. Integrated Computer Solutions Inc. www.ics.com Qt localization workflow 7 myapp.cpp myapp_fr.ts myapp_ru.ts myapp_fr.qm myapp_ru.qm myapp Strings extracted with lupdate Translators update files Compiled with lrelease Loaded with QTranslator.load() myui.ui myapp_ru.ts myapp_fr.ts 7
  • 8. Integrated Computer Solutions Inc. www.ics.com Qt Internationalization 8 ➢ What is Internationalization? ➢ Traditional Qt (C++) ➢ Qt Quick (QML) ➢ Generating and Loading Translations ➢ Qt Linguist ➢ Advanced Topics ➢ Common Errors and Useful Tips ➢ Demo 8
  • 9. Integrated Computer Solutions Inc. www.ics.com Localization with Qt - UI Strings 9 ● Distinguish user-visible localizable strings from non UI strings ● Use QString / QChar, they are Unicode ● Avoid using std::string / char they are not Unicode. ● To Prevent automatic casts between QString and char*. DEFINES += QT_NO_CAST_TO_ASCII QT_NO_CAST_FROM_ASCII ● Casting to char* or char may be unavoidable when interfacing with other libraries. You can use the QString conversion operators: QString::utf8(), QString::toUtf8() etc... 9
  • 10. Integrated Computer Solutions Inc. www.ics.com Localization with Qt - UI Strings 10 ● All literal UI strings are wrapped in localization functions ● Replaces string with translation at run-time based on locale ● Usually use tr() function ● Example: QPushButton *b = new QPushButton(tr("Save")); 10
  • 11. Integrated Computer Solutions Inc. www.ics.com Localization with Qt - QObject::tr() 11 ● Returns a translated version of the source text by searching the loaded qm files. ● Flag the string for inclusion in generated ts files. ● Uses the owning QObject as its context. ● QCoreApplication::translate() is similar to tr() but requires a context to be set. What does it do? 11
  • 12. Integrated Computer Solutions Inc. www.ics.com Localization with Qt - QObject::tr() 12 ● Can use structured comments to annotate a string with information for translators ● This works in both C++ and QML //: This refers to a network address. addressLabel->setText(tr("Address:")); /*: This refers to the age of a network packet. */ QString packetAge = tr("Age"); //= freeLibre //: free as in freedom QString fileText = tr("Free"); //= freeBeer //: free as in beer QString fileText = tr("Free"); 12
  • 13. Integrated Computer Solutions Inc. www.ics.com Localization with Qt - QObject::tr() 13 ● Optional second argument can be used to disambiguate identical strings that have different meanings in different contexts ● For example: ○ tr("free", "as in beer") ○ tr("free", "as in freedom") ● Above are handled as two distinct strings to translate (in French the first might be translated as "gratuit" and the second "libre") ● Optional third argument used to handle plurals (example later) 13
  • 14. Integrated Computer Solutions Inc. www.ics.com Localization with Qt - Examples 14 BAD, string cannot be localized QString s = "Hello, world!"; GOOD QString s = tr("Hello, world!"); 14
  • 15. Integrated Computer Solutions Inc. www.ics.com Localization with Qt - Examples 15 int i = 58; int j = 5000; Good, However order may be incorrect for some languages tr("Copying File ") + i + tr(" of ") + j; Better, The Whole phrase is provided to translator tr("Copying File %1 of %2").arg(i).arg(j); Best, The “L” modifier inserts a localized number tr("Copying File %L1 of %L2").arg(i).arg(j); Output(en_US): “Copying File 58 of 5,000” Output(en_CA): “Copying File 58 of 5 000” 15
  • 16. Integrated Computer Solutions Inc. www.ics.com Localization with Qt - Examples 16 int n = messages.count(); Bad, rules for plural forms are different in some languages if (n == 1) msg = tr("You have 1 new message."); else msg = tr("You have %1 new messages.").arg(n); Good msg = tr("You have %n new message(s).", nullptr, n); 16
  • 17. Integrated Computer Solutions Inc. www.ics.com Localization with Qt - Examples 1717
  • 18. Integrated Computer Solutions Inc. www.ics.com Localization with Qt - Keyboard Accelerators 18 ● Keyboard Accelerators can be Localized ● QKeySequence contains common ones. ● Choice of accelerator keys in other languages are left to the translator ● E.g. CTRL+Q may not exist on Japanese or Chinese user's keyboard quitAction = new QAction(tr("&Quit"), this); quitAction->setShortcut(tr("Ctrl+Q")); quitAction->setShortcut(QKeySequence::Quit); 18
  • 19. Integrated Computer Solutions Inc. www.ics.com Localization with Qt - Static Strings 19 ● Need to handle literal strings differently when they are outside of a class, such as constant strings ● The macro QT_TR_NOOP marks the strings as needing translation so they will be extracted by lupdate and put in .ts file ● The macro QT_TRANSLATE_NOOP takes a string and a context, typically a classname, which can be used when calling QCoreApplication::translate(). They need to match to translate ● The macros return the source string, so you still need to call tr() when using the string in order to translate it ● Non Qt classes can use the Q_DECLARE_TR_FUNCTIONS(classname) macro to enable tr calls. 19
  • 20. Integrated Computer Solutions Inc. www.ics.com Localization with Qt - Static Strings 20 Example: static const char *baseNames = { QT_TR_NOOP("adenine"), QT_TR_NOOP("cytosine"), QT_TR_NOOP("guanine"), QT_TR_NOOP("thymine") }; QString base(int num) { return tr(baseNames[num]); } 20
  • 21. Integrated Computer Solutions Inc. www.ics.com Qt Internationalization 21 ➢ What is Internationalization? ➢ Traditional Qt (C++) ➢ Qt Quick (QML) ➢ Generating and Loading Translations ➢ Qt Linguist ➢ Advanced Topics ➢ Common Errors and Useful Tips ➢ Demo 21
  • 22. Integrated Computer Solutions Inc. www.ics.com Localization with Qt Quick (QML) 22 ● Wrap all strings in qsTr() function ● Like tr(), supports "%n" replacement and .arg() ○ e.g. qsTr("You are %1 years old.").arg(age()) ● Doesn't accept multiple arguments but can use .arg() multiple times: ● qsTr() can also be used in JavaScript code var dateHeader = qsTr("Downloading file %1 of %2").arg(fileNumber()).arg(totalNumberOfFiles()) 22
  • 23. Integrated Computer Solutions Inc. www.ics.com Localization with Qt Quick (QML) 23 • Can test a translation with qmlviewer or qmlscene using the -translation option, e.g. qmlviewer -translation myapp.qm myapp.qml • See the Internationalization and Localization with Qt Quick section in the Qt documentation • In addition to qsTr(), QML also supports ○ qsTranslate() ○ qsTrid() ○ QT_TR_NOOP() ○ QT_TRANSLATE_NOOP() ○ QT_TRID_NOOP() 23
  • 24. Integrated Computer Solutions Inc. www.ics.com Localization with Qt Quick (QML) 24 C++ Function QML/JavaScript Function tr() qsTr() translate() qsTranslate() qtTrId() qsTrId() QT_TR_NOOP() QT_TR_NOOP() QT_TRANSLATE_NOOP() QT_TRANSLATE_NOOP() QT_TRID_NOOP() QT_TRID_NOOP() 24
  • 25. Integrated Computer Solutions Inc. www.ics.com Qt Internationalization 25 ➢ What is Internationalization? ➢ Traditional Qt (C++) ➢ Qt Quick (QML) ➢ Generating and Loading Translations ➢ Qt Linguist ➢ Advanced Topics ➢ Common Errors and Useful Tips ➢ Demo 25
  • 26. Integrated Computer Solutions Inc. www.ics.com Localization With Qt: Generating Ts Files QMAKE 26 • List the .ts files in your Qt project file .pro using TRANSLATIONS variable, e.g. TRANSLATIONS += myapp_de.ts myapp_fr.ts myapp_it.ts • Run lupdate to extract/merge strings when UI strings change. system (lupdate myapp.pro) $ lupdate myapp.pro $ lupdate main.cpp main.qml myapp_ru.ts • lrelease is used to compile .ts files to .qm files system (lrelease-pro myapp.pro) $ lrelease-pro myapp.pro $ lrelease myapp_ru.ts 26
  • 27. Integrated Computer Solutions Inc. www.ics.com Localization With Qt: Generating Ts Files CMAKE 27 find_package(Qt5LinguistTools) set(needsTranslation Mainwindow.cpp … More source files ) set(languageFiles myApp_en.ts myApp_fr.ts myApp_de.ts ) qt5_create_translation(TSFiles needsTranslation languageFiles ) add_custom_target(TRS ALL DEPENDS ${TSFiles}) Qt5LinguistTools Notes: ● CMake 3.1.0 + ● Provides two macros qt5_create_translation qt5_add_translation find_package(Qt5LinguistTools) qt5_add_translation(QMFiles mainwindow.cpp myApp_en.ts myApp_fr.ts myApp_dr.ts ) add_custom_target(QMS ALL DEPENDS ${QMFiles}) 27
  • 28. Integrated Computer Solutions Inc. www.ics.com Localization With Qt: Loading Translations - Runtime 28 int main(int argc, char *argv[]) { QApplication app(argc, argv); QTranslator qtTranslator; qtTranslator.load(QString("qtCore_%1") .arg(QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath)); ); app.installTranslator(&qtTranslator); QTranslator myappTranslator; myappTranslator.load("myapp_" + QLocale::system().name()); app.installTranslator(&myappTranslator); ... return app.exec(); } 28
  • 29. Integrated Computer Solutions Inc. www.ics.com Localization With Qt: Deploying Translations 29 You can store .qm files in resources. Advantages: ● Fewer files to distribute with your application ● Always easy to load files with a resource path Disadvantages: ● Cannot change or add new translations without rebuilding application ● Cannot distribute translations separately from application You can distribute .qm files as external files. Advantages: ● Can distribute without the application ● Can add new translations without rebuilding the application Disadvantages: ● More files to distribute with your application ● Need to get on disk path to load at runtime 29
  • 30. Integrated Computer Solutions Inc. www.ics.com Qt Internationalization 30 ➢ What is Internationalization? ➢ Traditional Qt (C++) ➢ Qt Quick (QML) ➢ Generating and Loading Translations ➢ Qt Linguist ➢ Advanced Topics ➢ Common Errors and Useful Tips ➢ Demo 30
  • 31. Integrated Computer Solutions Inc. www.ics.com Qt Linguist: 31 ● Cross Platform GUI Application for working with Qt Translations. ● Translators can get standalone copies of Qt Linguist. 31
  • 32. Integrated Computer Solutions Inc. www.ics.com Qt Linguist: Phrase Books 32 ● Phrase books Contain Common Phrases used in a language. ● Used as a tool to help speed up translation. ● Provides a translation that Qt Linguist can then suggest when those phrases are used in your application. 32
  • 33. Integrated Computer Solutions Inc. www.ics.com Qt Internationalization 33 ➢ What is Internationalization? ➢ Traditional Qt (C++) ➢ Qt Quick (QML) ➢ Generating and Loading Translations ➢ Qt Linguist ➢ Advanced Topics ➢ Common Errors and Useful Tips ➢ Demo 33
  • 34. Integrated Computer Solutions Inc. www.ics.com Localization Advanced: Localizers use placeholders 34 ● Placeholders: ○ Arg Placeholders: ■ Format: %[L]<number> ex: %1 %L1 ■ Can have multiple in a string, always start with %1 ■ String order doesn’t matter. “%2, %1” and “%1, %2” ○ Translation function provided ■ Format: %[L]n ex: %n %Ln ■ %n is a number, used with plurals. ○ Optional L Modifier will localize inserted number. ● Accelerators ○ Format: &<character> ex: “e&dit” ○ Localizers are responsible for them. ○ Character that follows the & becomes the accelerated key. 34
  • 35. Integrated Computer Solutions Inc. www.ics.com Localization Advanced: Multimedia Files 35 ● Use the QResource system, resource prefixes have a language tag ● Matching prefixes / alias will be replaced based on language tag ● The best practice is to make your multimedia resources universal Image, video, sound files 35
  • 36. Integrated Computer Solutions Inc. www.ics.com Localization Advanced: Cultural Differences 36 ● Symbols used in images and icons may not be universal to all cultures (e.g. Red Cross, check mark) ● Colors may not have the same meaning (e.g. red may not signify danger). Use the Palette to get your colors. ● Some images may be offensive to some cultures (e.g. animals) ● Embedded Fonts may be missing characters 36
  • 37. Integrated Computer Solutions Inc. www.ics.com Localization Advanced: Text Codecs 37 ● One day hopefully everything will use Unicode. ● File contents may not be encoded in Unicode or 8-bit characters ● May need to use appropriate text codec for locale ● Qt provides most common ones in QTextCodec ● Default text codec is set based on locale ● You may need to set a different text codec (or have the user select from the UI) ● Note: your source code should be encoded in Unicode. 37
  • 38. Integrated Computer Solutions Inc. www.ics.com Localization Advanced: Input Methods 38 ● Some languages require input method editor (IME) ● E.g. Chinese has thousands of characters, one to one mapping of keyboard keys to characters is not possible ● Usually transparent to developer, Qt through standard input widgets interfaces with input method editors such as Scim when present ● Typically set up at OS or desktop level ● Just make sure you don't use custom made input widgets 38
  • 39. Integrated Computer Solutions Inc. www.ics.com Localization Advanced: Id Based Translations 39 ● Strings can be translated by id with qrTsId(“ID”) and qsTrId(“ID”) ● Static Strings are marked with QT_TRID_NOOP(“ID”) ● Id based strings do not get a context. ● Use //% comments for parameters //% “Message Count: %1” ● Good for strings that come from outside your code. ● Must use -idbased option for lrelease when generating .qm files ● It is not recommended to mix ID based and Context based translations in one application, Mixing them can result in incomplete translations being generated. 39
  • 40. Integrated Computer Solutions Inc. www.ics.com Localization Advanced: Dynamic Language Changes 40 ● May want to change language at run time, e.g. from user input rather from locale ● Requires some extra effort to handle LanguageChange event to update text ● See "Dynamic Translation" section in Qt documentation "Internationalization with Qt" ● See section "Dynamic Language Switching" in Blanchette and Summerfield’ book “C++ GUI Programming with Qt4” 40
  • 41. Integrated Computer Solutions Inc. www.ics.com Localization Advanced: Other l10n Methods 41 ● You don't have to use Qt localization support ● There are other localization libraries and tools, e.g. GNU gettext and .po files ● KDE uses their own scheme based on gettext ● Essentially just use different wrapper functions for strings ● For uic generated code, you can change the function used for translating strings with -tr option ● lconvert tool can convert files between .ts, .po, and .xlf formats 41
  • 42. Integrated Computer Solutions Inc. www.ics.com Localization Advanced: lconvert 42 The lconvert tool works with common l10n file types ● Supports the following formats ○ Qts Own formats: Qt Message File (*.qm), Translation Source (*.ts), Qt Phrasebook (*.qph) ○ GNU Gettext formats: Templates (*.pot), GNU Gettext files (*.po) ○ XLIFF Files (*.xlf) ● Concatenate files ○ lconvert - o DESTDIR/myapp_ru.qm qtbase_ru.qm myapp_ru.qm ● Convert Between formats ○ lconvert -of qph -o myapp_fr.qph myapp_fr.ts ● Other options to sort, remove, change language, prune ui file strings , etc… 42
  • 43. Integrated Computer Solutions Inc. www.ics.com Qt Internationalization 43 ➢ What is Internationalization? ➢ Traditional Qt (C++) ➢ Qt Quick (QML) ➢ Generating and Loading Translations ➢ Advanced Topics ➢ Common Errors and Useful Tips ➢ Demo 43
  • 44. Integrated Computer Solutions Inc. www.ics.com Localization With Qt: Common Errors and Problems 44 ● Concatenating strings ● Forgetting to localize dates, currency, etc. ● Forgetting to add translator comments for non-obvious strings, e.g. "%1/%2" ● Not using disambiguation when using the same string for different roles ● Using tr() on strings that change at run-time ● If directly editing ts files, watch encoding format used by your editor. Can also be an issue with source files. ● Not Using Locale Aware compare methods when needed. 44
  • 45. Integrated Computer Solutions Inc. www.ics.com Localization With Qt: Useful Tips 45 ● Generate dummy translations for test purposes (i.e. all uppercase, additional Length) ○ Can be done with, e.g. a Perl script ○ Google Language Tools (don't expect quality) ● ts files can be edited by hand or scripts as they are XML ● Always, Always Use Layouts. ● QLocale stable but continues to improve 45
  • 46. Integrated Computer Solutions Inc. www.ics.com Localization With Qt: Summary 46 Qt makes it easy to localize your application With a few simple steps: ● Use QString for all user-visible text ● Use tr() or qsTr() for all user-visible literal text ● Use layout managers in dialogs ● Extract strings using lupdate and produce .ts files for each language ● Load translation files at run-time 46
  • 47. Integrated Computer Solutions Inc. www.ics.com Qt Internationalization 47 ➢ What is Internationalization? ➢ Traditional Qt (C++) ➢ Qt Quick (QML) ➢ Generating and Loading Translations ➢ Advanced Topics ➢ Common Errors and Useful Tips ➢ Demo 47
  • 48. Integrated Computer Solutions Inc. www.ics.com 4848
  • 49. Integrated Computer Solutions Inc. www.ics.comIntegrated Computer Solutions Inc. Thank You for attending! Any Questions? 49 webinars@ics.com