Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

Hello tr() Example

Files:

This example is a reworking of the Hello World! example from the Qt Tutorial, with a Latin translation. The screenshot below shows the English version.

See the Qt Linguist manual for more information about translating Qt application.

Line by Line Walkthrough

 #include <QTranslator>

This line includes the definition of the QTranslator class. Objects of this class provide translations for user-visible text.

     QTranslator translator;

Creates a QTranslator object without a parent.

     translator.load("hellotr_la");

Tries to load a file called hellotr_la.qm (the .qm file extension is implicit) that contains Latin translations for the source texts used in the program. No error will occur if the file is not found.

     app.installTranslator(&translator);

Adds the translations from hellotr_la.qm to the pool of translations used by the program.

     QPushButton hello(QPushButton::tr("Hello world!"));

Creates a push button that displays "Hello world!". If hellotr_la.qm was found and contains a translation for "Hello world!", the translation appears; if not, the source text appears.

All classes that inherit QObject have a tr() function. Inside a member function of a QObject class, we simply write tr("Hello world!") instead of QPushButton::tr("Hello world!") or QObject::tr("Hello world!").

Running the Application in English

Since we haven't made the translation file hellotr_la.qm, the source text is shown when we run the application:

Creating a Latin Message File

The first step is to create a project file, hellotr.pro, that lists all the source files for the project. The project file can be a qmake project file, or even an ordinary makefile. Any file that contains

 SOURCES      = main.cpp
 TRANSLATIONS = hellotr_la.ts

will work. TRANSLATIONS specifies the message files we want to maintain. In this example, we just maintain one set of translations, namely Latin.

Note that the file extension is .ts, not .qm. The .ts translation source format is designed for use during the application's development. Programmers or release managers run the lupdate program to generate and update .ts files with the source text that is extracted from the source code. Translators read and update the .ts files using Qt Linguist adding and editing their translations.

The .ts format is human-readable XML that can be emailed directly and is easy to put under version control. If you edit this file manually, be aware that the default encoding for XML is UTF-8, not Latin1 (ISO 8859-1). One way to type in a Latin1 character such as 'ø' (Norwegian o with slash) is to use an XML entity: "&#xf8;". This will work for any Unicode 4.0 character.

Once the translations are complete the lrelease program is used to convert the .ts files into the .qm Qt message file format. The .qm format is a compact binary format designed to deliver very fast lookup performance. Both lupdate and lrelease read all the project's source and header files (as specified in the HEADERS and SOURCES lines of the project file) and extract the strings that appear in tr() function calls.

lupdate is used to create and update the message files (hellotr_la.ts in this case) to keep them in sync with the source code. It is safe to run lupdate at any time, as lupdate does not remove any information. For example, you can put it in the makefile, so the .ts files are updated whenever the source changes.

Try running lupdate right now, like this:

 lupdate -verbose hellotr.pro

(The -verbose option instructs lupdate to display messages that explain what it is doing.) You should now have a file hellotr_la.ts in the current directory, containing this:

 <!DOCTYPE TS><TS>
 <context>
     <name>QPushButton</name>
     <message>
         <source>Hello world!</source>
         <translation type="unfinished"></translation>
     </message>
 </context>
 </TS>

You don't need to understand the file format since it is read and updated using tools (lupdate, Qt Linguist, lrelease).

Translating to Latin with Qt Linguist

We will use Qt Linguist to provide the translation, although you can use any XML or plain text editor to enter a translation into a .ts file.

To start Qt Linguist, type

 linguist hellotr_la.ts

You should now see the text "QPushButton" in the top left pane. Double-click it, then click on "Hello world!" and enter "Orbis, te saluto!" in the Translation pane (the middle right of the window). Don't forget the exclamation mark!

Click the Done checkbox and choose File|Save from the menu bar. The .ts file will no longer contain

 <translation type='unfinished'></translation>

but instead will have

 <translation>Orbis, te saluto!</translation>

Running the Application in Latin

To see the application running in Latin, we have to generate a .qm file from the .ts file. Generating a .qm file can be achieved either from within Qt Linguist (for a single .ts file), or by using the command line program lrelease which will produce one .qm file for each of the .ts files listed in the project file. Generate hellotr_la.qm from hellotr_la.ts by choosing File|Release from Qt Linguist's menu bar and pressing Save in the file save dialog that pops up. Now run the hellotr program again. This time the button will be labelled "Orbis, te saluto!".

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 64
  2. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  3. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  4. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  5. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. La rubrique Qt a besoin de vous ! 1
Page suivante

Le Qt Developer Network au hasard

Logo

Comment fermer une application

Le Qt Developer Network est un réseau de développeurs Qt anglophone, où ils peuvent partager leur expérience sur le framework. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 4.2
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web