Qtopia/Qt Chinese support

Source: Internet
Author: User
Tags i18n
At the beginning of the design, QT considered multi-language support. therefore, it is easier to support Chinese characters. it is easy to compile multi-language programs. chinese support for qtopia/QT. the main scenarios are as follows: 1. add Chinese support to your QT program. 2. display Chinese characters on the qtopia desktop. let's look at how QT supports Chinese in two cases. here we will mainly use QT for demonstration, and a version of qtopia will be demonstrated later. I would like to express my gratitude for the majority of the procedures and analysis in this article. Added Chinese support for applications.The idea of multi-language support for Qt applications is that it is very similar to Windows. That is, non-English strings are not compiled in the program. Instead, it is saved in a dictionary file. Use the dictionary index in the program. In this way, if the program is not modified, it can be displayed in different languages as long as it is replaced with different dictionary files. In a Windows program, this string is a resource file (*. Res), which is finally linked to an executable file. Use the resource ID of the integer type in the program to extract the string from the resource file through the API function loadstring. Similar procedures are also used in QT. The string is stored in the translation file (*. Ts). When published, it is converted to a binary. QM file. This file does not need to be linked to the executable file. The program uses an English string as the dictionary index. Use the tr () function to retrieve the corresponding string from the translation file. The following example shows how to use Chinese characters in the QT program. we will use an example of a QT player to demonstrate that its buttons will use text labels. We will replace these labels with Chinese characters later. Use qtdesigner to draw this interface. I use the QT 3.3 designer here. The qtopia 2.2 designer is generally similar to the design. Where text is used, the designer will automatically add tr () and function.

/*
 * Sets the strings of the subwidgets using the current
 * language.
 */
void PlayerForm::languageChange()
{
    setCaption( tr( "QT Player" ) );
    btn_open->setText( tr( "open" ) );
    btn_play->setText( tr( "play" ) );
    btn_stop->setText( tr( "stop" ) );
    btn_close->setText( tr( "close" ) );
}

Step 2: Use the desktop version of QT lupdate to create a TS. file. First, you need to manually modify the pro file. Add the TS file option to add Translations = qplayer_cn.ts
Then lupdate can identify the translations of the Pro files on the desktop and qtopia to create the corresponding. Ts files. $ Qtdir/bin/lupdate qplayer. ProGenerate ts is also an XML file with the following content,

<! Doctype TS> <TS>
<Context>
<Name> playerform </Name>
<Message>
<Source> QT player </source>
<Translation type = "unfinished"> </translation>
</Message>
<Message>
<Source> open </source>
<Translation type = "unfinished"> </translation>
</Message>
<Message>
<Source> play </source>
<Translation type = "unfinished"> </translation>
</Message>
<Message>
<Source> stop </source>
<Translation type = "unfinished"> </translation>
</Message>
<Message>
<Source> close </source>
<Translation type = "unfinished"> </translation>
</Message>

Step 3: Use linguist on the desktop to open ts. for translation. Open the generated xx_cn.ts file and you will find that the Fields marked with TR () appear in the source text box. Then translate the fields to be translated to complete the operations at this stage. Translate operations on the Interface

 

Step 4: Use lrelease to generate the Qm file from the TS file and call the program. The following command generates a binary file qplayer_cn.qm with the same nameLrelease qplayer_cn.tsStep 4: add support for the qtranslator class to the main program. And load the corresponding QM file.

#include <qapplication.h>
#include <qtranslator.h>

#include "qplayer.h"

int main( int argc, char ** argv )
{
    QApplication a( argc, argv );
    QTranslator translator(0);
    translator.load(QString("qplayer_CN.qm"));
    a.installTranslator(&translator);

    PlayerForm w;
    w.show();
    a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
    return a.exec();
}

 

After the program is fully compiled, run the program again to view the Chinese interface (Note: The desktop version requires the Chinese Simplified language settings)

(The simplest way is to export lang = zh_CN.UTF-8, or modify the/etc/sysconfig/i18n configuration file, where the options for Lang are zh_CN.UTF-8)

The following source code is the full source code of QT 3 desktop edition and qtopia 2.

File: Qplayer.tar.rar
Size: 2 kb
Download: Download

 

Localization of qtopia Desktop

  •  Rebuild makefile

The definition of qtopia desktop requires that the desktop be compiled again. And reference the Chinese font. Generate the configure script of makefile in the/xxx_qtopia/qtopia directory and add the -- ages "zh_cn"

./Configure-platform Linux-generic-G ++-- Ages "zh_cn"

Compile make lupdate.

  • Generate TS

Finally, run./nct_lupdate in qtopia/bin.

After this step, the zh_cn folder will be generated under the qtopia/i18n/directory, and The. Ts file on the main interface will be generated under the zh_cn folder. The linguist will be used for Chinese conversion.

  • Create a Chinese configuration file

In qtopia/i18n/create a dictionary configuration file. directory, the content is as follows.

[Translation]
File = qtopiai18n
Context = Chinese
[Desktop entry]
Name [] = Chinese

  • Modify Chinese font

Modify <Name> fontmap </Name> In qtopia/i18n/zh_cn/qtopiadefault. Ts:

<Name> fontmap </Name>
<Message encoding = "UTF-8">
<Source> small, Helvetica, 10 </source>
<Translation> Petit, uniont, 11 </translation>
</Message>

 

  • Install the Chinese version.

Use make install or directly call lrelease to convert the Qm file into the Development Board.

  • Modify interface language

Go to the QT interface and enter settings. The language option sets the language to Chinese.
At the same time, select a font containing Chinese characters in the apperance option, such as the built-in uniont font.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.