⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 linguist-programmer.leaf

📁 Linux下的基于X11的图形开发环境。
💻 LEAF
📖 第 1 页 / 共 3 页
字号:
Since we haven't made the translation file \c tt1_la.qm, the source textis shown when we run the application:\img tt1_en.png\caption Tutorial 1 Screenshot, English version\section3 Creating a Latin Message File\index tt1.pro\index LatinThe first step is to create a project file, \c tt1.pro, that listsall the source files for the project. The project file can be a qmakeproject file, or even an ordinary makefile. Any file that contains\index SOURCES!in Project Files\index TRANSLATIONS!in Project Files\quotefile tt1/tt1.pro\skipto SOURCES\printline SOURCES\skipto TRANSLATIONS\printline TRANSLATIONSwill work. \e TRANSLATIONS specifies the message files we want tomaintain. In this example, we just maintain one set of translations,namely Latin.\index .ts Files\index Translation Source Files\index .qm Files\index Qt Message FilesNote that the file extension is \c .ts, not \c .qm. The \c .tstranslation source format is designed for use during theapplication's development. Programmers or release managers run the \llupdate program to generate and update \c .ts files with the sourcetext that is extracted from the source code. Translators read andupdate the \c .ts files using \e {Qt Linguist} adding and editingtheir translations.\index XMLThe \c .ts format is human-readable XML that can be emailed directlyand is easy to put under version control. If you edit this filemanually, be aware that the default encoding for XML is UTF-8, notLatin-1 (ISO 8859-1). One way to type in a Latin-1 character such as'\OSLASH' (Norwegian o with slash) is to use an XML entity:"\&#xf8;". This will work for any Unicode character.Once the translations are complete the \l lrelease program is used toconvert the \c .ts files into the \c .qm Qt message file format. The\c .qm format is a compact binary format designed to deliver veryfast lookup performance. Both \l lupdate and \l lrelease read all theproject's source and header files (as specified in the HEADERS andSOURCES lines of the project file) and extract the strings thatappear in \c tr() function calls.\index lupdate\l lupdate is used to create and update the message files (\c tt1_la.tsin this case) to keep them in sync with the source code. It is safe torun \l lupdate at any time, as \l lupdate does not remove anyinformation. For example, you can put it in the makefile, so the \c .tsfiles are updated whenever the source changes.\index .ts Files\index Translation Source Files\index XMLTry running \l lupdate right now, like this:\code    lupdate -verbose tt1.pro\endcode(The \c -verbose option instructs \c lupdate to display messages thatexplain what it is doing.) You should now have a file \c tt1_la.ts inthe current directory, containing this:\code    <!DOCTYPE TS><TS>    <context>        <name>QPushButton</name>        <message>            <source>Hello world!</source>            <translation type="unfinished"></translation>        </message>    </context>    </TS>\endcodeYou don't need to understand the file format since it is read andupdated using tools (\l lupdate, \e {Qt Linguist}, \l lrelease).\section3 Translating to Latin with Qt Linguist\index Qt Linguist\index LinguistWe will use \e {Qt Linguist} to provide the translation, althoughyou can use any XML or plain text editor to enter a translation into a\c .ts file.To start \e {Qt Linguist}, type\code    linguist tt1_la.ts\endcodeYou should now see the text "QPushButton" in the top left pane.Double-click it, then click on "Hello world!" and enter "Orbis, tesaluto!" in the \e Translation pane (the middle right of thewindow). Don't forget the exclamation mark!Click the \e Done checkbox and choose \e File|Save from themenu bar. The \c .ts file will no longer contain\code    <translation type='unfinished'></translation>\endcodebut instead will have\code    <translation>Orbis, te saluto!</translation>\endcode\section3 Running the Application in Latin\index Latin\index lreleaseTo see the application running in Latin, we have to generate a \c .qmfile from the \c .ts file. Generating a \c .qm file can be achievedeither from within \e {Qt Linguist} (for a single \c .ts file), orby using the command line program \l lrelease which will produce one \c.qm file for each of the \c .ts files listed in the project file.Generate \c tt1_la.qm from \c tt1_la.ts by choosing\e File|Release from \e {Qt Linguist}'s menu bar and pressing\e Save in the file save dialog that pops up. Now run the \e tt1 exampleprogram again. This time the button will be labelled "Orbis, tesaluto!".\img tt1_la.png\caption Tutorial 1 Screenshot, Latin version\section2 Tutorial 2: Using Two or More Languages\img tt2_en.png\caption Tutorial 2 Screenshot, English version\index .pro Files\index Project Files\index qmake!Project Files\include tt2/tt2.pro\caption tt2.pro\index Translation Contexts\index Contexts!for TranslationThis example is a slightly more involved and introduces a key\e {Qt Linguist} concept: "contexts".\list\i \c arrowpad.h contains the definition of \c ArrowPad, a custom widget;\i \c arrowpad.cpp contains the implementation of \c ArrowPad;\i \c mainwindow.h contains the definition of \c MainWindow, a subclass of   \l QMainWindow\i \c mainwindow.cpp contains the implementation of \c MainWindow;\i \c main.cpp contains main().\endlist\index tt2.pro\index French Language\index Dutch LanguageWe will use two translations, French and Dutch, although there is noeffective limit on the number of possible translations that can be usedwith an application. The relevant lines of \c tt2.pro are\quotefile tt2/tt2.pro\skipto HEADERS\printuntil tt2_nl.ts\index lupdate\index tt2_fr.ts\index tt2_nl.tsRun \l lupdate; it should produce two identical message files\c tt2_fr.ts and \c tt2_nl.ts. These files will contain all the sourcetexts marked for translation with \c tr() calls and their contexts.\section3 Line by Line Walk-through\index ArrowPad!in Translation Tutorial\index English LanguageIn \c arrowpad.h we define the \c ArrowPad subclass which is asubclass of \l QWidget. In the \e {Tutorial 2 Screenshot, Englishversion}, above, the central widget with the four buttons is an\c ArrowPad.\quotefile tt2/arrowpad.h\skipto class ArrowPad\printline class ArrowPad\index Q_OBJECT\index tr()\index QObject!tr()\index Translation Contexts\index Contexts!for TranslationWhen \l lupdate is run it not only extracts the source texts but italso groups them into contexts. A context is the name of the class inwhich the source text appears. Thus, in this example, "ArrowPad" is acontext: it is the context of the texts in the \c ArrowPad class. The \c Q_OBJECT macro defines \c tr(x) in \c ArrowPad like this\index QApplication!translate()\index translate()!QApplication\code    qApp->translate( "ArrowPad", x )\endcodeKnowing which class each source text appears in enables \e {QtLinguist} to group texts that are logically related together, e.g.all the text in a dialog will have the context of the dialog's classname and will be shown together. This provides useful information forthe translator since the context in which text appears may influence howit should be translated. For some translations keyboardaccelerators may need to be changed and having all the source texts in aparticular context (class) grouped together makes it easier for thetranslator to perform any accelerator changes without introducingconflicts.In \c arrowpad.cpp we implement the \c ArrowPad class.\quotefile tt2/arrowpad.cpp\skipto QPushButton\printline QPushButtonWe call \c ArrowPad::tr() for each button's label since the labels areuser-visible text.\img tt2_en.png\caption Tutorial 2 Screenshot, English version\index Q_OBJECT\index MainWindow!in Translation Tutorial\quotefile tt2/mainwindow.h\skipto QMainWindow\printline QMainWindow\printuntil Q_OBJECTIn the \e {Tutorial 2 Screenshot, English version}, above, the wholewindow is a \c MainWindow. This is defined in the \c mainwindow.hheader file. Here too, we use \c Q_OBJECT, so that \c MainWindow willbecome a context in \e {Qt Linguist}.In the implementation of \c MainWindow, \c mainwindow.cpp, we createan instance of our \c ArrowPad class\quotefile tt2/mainwindow.cpp\skipto arrow pad\printline arrow padWe also call \c MainWindow::tr() twice, once for the menu item andonce for the accelerator.\index Ctrl Key\index Alt Key\skipto quit()\printline quit()\printuntil Ctrl+QNote the use of \c tr() to support different keys in other languages."Ctrl+Q" is a good choice for Quit in English, but a Dutch translatormight want to use "Ctrl+A" (for Afsluiten) and a German translator"Strg+E" (for Beenden). When using \c tr() for Ctrl key accelerators,the two argument form should be used with the second argumentdescribing the function that the accelerator performs.\index main()Our \c main() function is defined in \c main.cpp as usual.\quotefile tt2/main.cpp\skipto QTranslator\printline QTranslator\printuntil install\index QTextCodec!locale()\index locale()!QTextCodec\index LANG!Environment Variable\index Environment Variables!LANGWe choose which translation to use according to the current locale.\l QTextCodec::locale() can be influenced by setting the \c LANGenvironment variable, for example. Notice that the use of a namingconvention that incorporates the locale for \c .qm message files,(and \c .ts files), makes it easy to implement choosing thetranslation file according to locale.If there is no \c .qm message file for the locale chosen the originalsource text will be used and no error raised.\section3 Translating to French and DutchWe'll begin by translating the example application into French. Start\e {Qt Linguist} with \c tt2_fr.ts. You should get the seven sourcetexts ("\&Up", "\&Left", etc.) grouped in two contexts ("ArrowPad"and "MainWindow").Now, enter the following translations:\list\i \c ArrowPad     \list     \i \&Up - \&Haut     \i \&Left - \&Gauche     \i \&Right - \&Droite     \i \&Down - \&Bas     \endlist\i \c MainWindow     \list     \i E\&xit - \&Quitter     \i Ctrl+Q - Ctrl+Q     \i \&File - \&Fichier     \endlist\endlistIt's quickest to press \Key Alt+D (which clicks the \e {Done \& Next}button) after typing each translation, since this marks thetranslation as done and moves on to the next source text.Save the file and do the same for Dutch working with \c tt2_nl.ts:\list\i \c ArrowPad     \list     \i \&Up - \&Boven     \i \&Left - \&Links     \i \&Right - \&Rechts     \i \&Down - \&Onder     \endlist\i \c MainWindow     \list     \i E\&xit - \&Afsluiten     \i Ctrl+Q - Ctrl+A     \i File - \&Bestand     \endlist\endlistWe have to convert the \c tt1_fr.ts and \c tt1_nl.ts translation sourcefiles into \c .qm files. We could use \e {Qt Linguist} as we've donebefore; however using the command line tool \l lrelease ensures that\e all the \c .qm files for the application are created without ushaving to remember to load and \e File|Release each oneindividually from \e {Qt Linguist}.In practice we would include calls to \l lupdate and \l lrelease in theapplication's makefile to ensure that the latest translations areused.\omitan example of a makefile or .pro file that did this would be nice\endomitType\code    lrelease tt2.pro\endcode\index LANG!Environment Variable\index export!Unix Command\index setenv!Unix CommandThis should create both \c tt2_fr.qm and \c tt2_nl.qm. Set the \cLANG environment variable to \c fr. In Unix, one of the two followingcommands should work\code    export LANG=fr

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -