0
Translating Qt applications
                                    09/12/09
It does not have to be boring ;-)
Who is Benjamin Poulain at Nokia?
• first support engineer
   – Linux and Mac OS X


• now Webkit developer


• pet project...
What do I do after work?

• some hacking
   – web applications

• lots of sport: running, cycling, climbing




          ...
Why do I work on translations?

• my family do not speak english
  ➡ need for localized software

• work on
  – tutorial i...
What are we gonna do today?

• Why bother?
• Translating your application
• Tools for translators
• Going further
• Conclu...
What are we gonna do today?

• Why bother?
• Translating your application
• Tools for translators
• Going further
• Conclu...
Translations increase your user base
 Everybody speaks English. right?
                              Native speaker
      ...
What are we gonna do today?

• Why bother?
• Translating your application
   –   Simple application
   –   UI
   –   Sourc...
Ugly Snake is a mini game in Qt
How do we translate the application?

  Internationalization workflow:

          1) write code ready for translation

    ...
What are we gonna do today?

• Why bother?
• Translating your application
   –   Simple application
   –   UI
   –   Sourc...
The .ui files are the biggest part

• most strings are in the .ui

• this is what is seen by the user


• good news: work o...
What are we gonna do today?

• Why bother?
• Translating your application
   –   Simple application
   –   UI
   –   Sourc...
Translate the strings with tr()

• QObject::tr() translate a string:
   trayIcon.showMessage("new message", "you have too ...
Provide some context for short strings

  What are 1, 2?   tr("The %1 is %2").arg(...);
Provide some context for short strings

  What are 1, 2?        tr("The %1 is %2").arg(...);




  3 ways to add context:
...
How to mark strings for translations?

• tr() returns QString

• QT_TR_NOOP marks for translation

• translated later with...
What are we gonna do today?

• Why bother?
• Translating your application
   –   Simple application
   –   UI
   –   Sourc...
Applications use a compiled version of the translation


• lrelease compiles .ts in .qm

• .qm is the binary format for re...
A translator must be installed
• QTranslator

   - translate the strings

   - load the .qm file:
     QTranslator uglySnak...
What are we gonna do today?

• Why bother?
• Translating your application
• Tools for translators
• Going further
• Conclu...
There are two kinds of tools for translators

• offline ➜ Qt Linguist

• online ➜ Pootle, ...
Presentation of Linguist
Linguist is great but need to be distributed

• support multiple languages (simultaneously)

• great integration with Qt

...
Presentation of Pootle
Pootle is great, but offer no integration

• no need to distribute

• easier for amateur



but: no integration (ui files!)
What are we gonna do today?

• Why bother?
• Translating your application
• Tools for translators
• Going further
  –   “c...
One word, multiple concepts

                       noun     édition

          edit
                         verb   édite...
Comments does not disambiguate the strings

• works:
           QString a = tr("edit", "N97");
           QString b = tr("...
Understanding context and disambiguation
                  source      disambiguation


   QPushButton::tr("edit", "verb")...
Comments does not disambiguate the strings

• works:
              QString a = tr("edit", "N97");
              QString b ...
What are we gonna do today?

• Why bother?
• Translating your application
• Tools for translators
• Going further
  –   “c...
The translation should fit the widgets

• the layouts adapt the widgets




• problems:
   - fixed size
   - phone screen
Some images need a translation

• localize icons, images, etc

• qresource support locale:
         <qresource lang="fr">
...
What if you want multiple UI?

• cultural conventions

• phone screens

• right to left interface


✓ Solution: QUiLoader ...
What are we gonna do today?

• Why bother?
• Translating your application
• Tools for translators
• Going further
  –   “c...
What if the language is changed after the start?

• examples:

   - phone

   - public terminal

   - MS Windows applicati...
Everything needs a new translation

• LanguageChange event on ::installTranslator()

• QWidget::changeEvent() to catch the...
When to change the translators?

• LocaleChange event to the active window

➡ install event filter on QApplication

➡ chang...
What are we gonna do today?

• Why bother?
• Translating your application
• Tools for translators
• Going further
  –   “c...
Translate on painting is not a good strategy
       char languageList[] = { QT_TR_NOOP("english"),
                       ...
The source might not be in Latin 1

• tr() uses Latin 1 by default
         tr("Qt est génial"); // might fail




✓ Solut...
Mac uses an ordered list of language

• Mac OS X

   - ordered list of languages
                                         ...
What are we gonna do today?

• Why bother?
• Translating your application
• Tools for translators
• Going further
• Conclu...
Qt makes translation easy

• Internationalization is important
• Qt already solves the problems
• Minimal changes are need...
Translating Qt Applications
Upcoming SlideShare
Loading in...5
×

Translating Qt Applications

8,591

Published on

Supporting several languages is a key point to increase the audience of an application. We will see what is needed in Qt to enable internationalization and how to ensure all the components can be translated. We will also see the tools available for the translators and how to use them.

Presentation by Benjamin Poulain held during Qt Developer Days 2009.

http://qt.nokia.com/developer/learning/elearning

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
8,591
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
115
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Translating Qt Applications"

  1. 1. Translating Qt applications 09/12/09 It does not have to be boring ;-)
  2. 2. Who is Benjamin Poulain at Nokia? • first support engineer – Linux and Mac OS X • now Webkit developer • pet projects – graphical tablet – Cocoa support 2
  3. 3. What do I do after work? • some hacking – web applications • lots of sport: running, cycling, climbing I am not so good on a snowboard
  4. 4. Why do I work on translations? • my family do not speak english ➡ need for localized software • work on – tutorial in french – translation of Qt Creator – french community of Qt
  5. 5. What are we gonna do today? • Why bother? • Translating your application • Tools for translators • Going further • Conclusion
  6. 6. What are we gonna do today? • Why bother? • Translating your application • Tools for translators • Going further • Conclusion
  7. 7. Translations increase your user base Everybody speaks English. right? Native speaker Secondary Do not understand English writing an application is difficult ➜ spread it
  8. 8. What are we gonna do today? • Why bother? • Translating your application – Simple application – UI – Source – Integrate the translation • Tools for translators • Going further • Conclusion
  9. 9. Ugly Snake is a mini game in Qt
  10. 10. How do we translate the application? Internationalization workflow: 1) write code ready for translation 2) extract strings 3) translate 4) compile strings
  11. 11. What are we gonna do today? • Why bother? • Translating your application – Simple application – UI – Source – Integrate the translation • Tools for translators • Going further • Conclusion
  12. 12. The .ui files are the biggest part • most strings are in the .ui • this is what is seen by the user • good news: work out of the box - string extraction - translation
  13. 13. What are we gonna do today? • Why bother? • Translating your application – Simple application – UI – Source – Integrate the translation • Tools for translators • Going further • Conclusion
  14. 14. Translate the strings with tr() • QObject::tr() translate a string: trayIcon.showMessage("new message", "you have too many mail"); trayIcon.showMessage(tr("new message"), tr("you have too many mail")); • works great with QString::arg(): QString::number(userCount) + "users online"; tr("%n user(s) online").arg(userCount);
  15. 15. Provide some context for short strings What are 1, 2? tr("The %1 is %2").arg(...);
  16. 16. Provide some context for short strings What are 1, 2? tr("The %1 is %2").arg(...); 3 ways to add context: – luck – context in tr(): tr("The %1 is %2", "The <extension> is <state>").arg(...); – comment in code //: The <extension> is <state> tr("The %1 is %2").arg(...);
  17. 17. How to mark strings for translations? • tr() returns QString • QT_TR_NOOP marks for translation • translated later with tr() example: char *const EDITOR = QT_TR_NOOP("emacs"); void function() { (...) QString editor = tr(EDITOR); (...) }
  18. 18. What are we gonna do today? • Why bother? • Translating your application – Simple application – UI – Source – Integrate the translation • Tools for translators • Going further • Conclusion
  19. 19. Applications use a compiled version of the translation • lrelease compiles .ts in .qm • .qm is the binary format for release • fast lookup • not into the binary
  20. 20. A translator must be installed • QTranslator - translate the strings - load the .qm file: QTranslator uglySnakeTranslator; uglySnakeTranslator.load("uglysnake_" + QLocale::system().name(), QCoreApplication::applicationDirPath()); app.installTranslator(&uglySnakeTranslator);
  21. 21. What are we gonna do today? • Why bother? • Translating your application • Tools for translators • Going further • Conclusion
  22. 22. There are two kinds of tools for translators • offline ➜ Qt Linguist • online ➜ Pootle, ...
  23. 23. Presentation of Linguist
  24. 24. Linguist is great but need to be distributed • support multiple languages (simultaneously) • great integration with Qt • integration with the UI files: • errors are spoted imediately • full context but: need to be distributed
  25. 25. Presentation of Pootle
  26. 26. Pootle is great, but offer no integration • no need to distribute • easier for amateur but: no integration (ui files!)
  27. 27. What are we gonna do today? • Why bother? • Translating your application • Tools for translators • Going further – “context” – tricky UI – change the language – pitfalls • Conclusion
  28. 28. One word, multiple concepts noun édition edit verb éditer • short text ➜ context tr("edit", "verb"); tr("edit", "noun"); • also in designer
  29. 29. Comments does not disambiguate the strings • works: QString a = tr("edit", "N97"); QString b = tr("edit", "N900"); • do not work //: N97 QString a = tr("edit"); //: N900 QString b = tr("edit"); • why?
  30. 30. Understanding context and disambiguation source disambiguation QPushButton::tr("edit", "verb"); context QCoreApplication::translate("QPushButton", "edit", "verb"); QTranslator::translate("QPushButton", "edit", "verb"); context source disambiguation ➜ result QPushButton edit verb ➜ editer QPushButton edit ➜
  31. 31. Comments does not disambiguate the strings • works: QString a = tr("edit", "N97"); QString b = tr("edit", "N900"); • do not work: //: N97 QString a = tr("edit"); //: N900 QString b = tr("edit"); • solution: //: N97 QString a = tr("edit", "phoneA"); //: N900 QString b = tr("edit", "phoneB");
  32. 32. What are we gonna do today? • Why bother? • Translating your application • Tools for translators • Going further – “context” – tricky UI – change the language – pitfalls • Conclusion
  33. 33. The translation should fit the widgets • the layouts adapt the widgets • problems: - fixed size - phone screen
  34. 34. Some images need a translation • localize icons, images, etc • qresource support locale: <qresource lang="fr"> <file>burnIcon.png</file> </qresource>
  35. 35. What if you want multiple UI? • cultural conventions • phone screens • right to left interface ✓ Solution: QUiLoader to load the UI X difficult to maintain X difficult to document
  36. 36. What are we gonna do today? • Why bother? • Translating your application • Tools for translators • Going further – “context” – tricky UI – change the language – pitfalls • Conclusion
  37. 37. What if the language is changed after the start? • examples: - phone - public terminal - MS Windows applications
  38. 38. Everything needs a new translation • LanguageChange event on ::installTranslator() • QWidget::changeEvent() to catch the event • UI responds to retranslateUI() • manual update for the code void SettingsPanel::changeEvent(QEvent *e) { if (e->type() == QEvent::LanguageChange) { ui->retranslateUi(this); updateSpeedDescription(); } QWidget::changeEvent(e); }
  39. 39. When to change the translators? • LocaleChange event to the active window ➡ install event filter on QApplication ➡ change the translators
  40. 40. What are we gonna do today? • Why bother? • Translating your application • Tools for translators • Going further – “context” – tricky UI – change the language – pitfalls • Conclusion
  41. 41. Translate on painting is not a good strategy char languageList[] = { QT_TR_NOOP("english"), QT_TR_NOOP("german") }; if (language == "english”) { ... void paintEvent(QEvent *) { QString toShow = tr(m_language); ... } list sorted alphabetically: ✓ Solution: use classes for identity, not strings
  42. 42. The source might not be in Latin 1 • tr() uses Latin 1 by default tr("Qt est génial"); // might fail ✓ Solution 1: use English for the source ✓ Solution 2: - QTextCodec::setCodecForTr() - CODECFORTR = UTF-8
  43. 43. Mac uses an ordered list of language • Mac OS X - ordered list of languages Not accurate • language selection: QTranslator uglySnakeTranslator; uglySnakeTranslator.load("uglysnake_" + QLocale::system().name(), QCoreApplication::applicationDirPath()); app.installTranslator(&uglySnakeTranslator); ✓ Solution: iterate over the languages of core foundation
  44. 44. What are we gonna do today? • Why bother? • Translating your application • Tools for translators • Going further • Conclusion
  45. 45. Qt makes translation easy • Internationalization is important • Qt already solves the problems • Minimal changes are needed in the code – tr() every string – provide context for short strings ➡ think about it when you develop
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×