Application Development in Linux: QT international programming

Source: Internet
Author: User
Tags i18n
Article Title: Application Development in Linux: QT international programming. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Qt's current version (2.2.4) provides excellent support for internationalization. In text display, Qt uses Unicode as internal encoding and supports multiple types of encoding at the same time. It is also convenient to add a encoding support for Qt. You only need to add the encoding and Unicode conversion encoding. Qt currently supports ISO 8859-1, ISO 8859-2, ISO 8859-3, ISO 8859-4, ISO 8859-5, ISO 8859-7, ISO 8859-9, and ISO 8859-15 (support for Arabic and Hebrew is under development), Chinese GBK/Big5, Japanese eucJP/JIS/ShiftJIS, Korean eucKR, Russian KOI8-R. Of course, you can also use UTF8 encoding directly.
  
Qt uses its own Locale mechanism, which makes up for the shortcomings of Locale and gettext widely used in Unix in terms of encoding support and Message File translation. This mechanism of Qt enables the same component (QWidget) of Qt to display different encoding texts at the same time. For example, Qt labels can use both Simplified Chinese text and Traditional Chinese text.
  
In text Input, Qt adopts the XIM (X Input Method) standard protocol and can directly use the XIM Input server. Currently, most input servers are for a single language, so the input in the standard Qt input component (QLineEdit, QMultiLineEdit) is subject to a single encoding, qt does not support dynamic switching of encoding input, which is a disadvantage.
  
   1. text display of Qt
Use Qt to write international programs. It is best not to directly use specially encoded text in the program. For example, to compile the Qt program on the text interface, you should use English in the program. After the program is compiled, extract the text and translate it. In this way, the program can support multiple languages based on Locale. The following describes how to mark strings in Qt programs and how to extract and translate texts.
  
Like a normal internationalization process, Qt uses the QObject: tr () function similar to GNU gettext, which is used to extract information files from Qt. information is extracted from qm, which is processed by Qt tools. Qt also uses the QTranslator class for coding and can be used to specify information files of the entire application software.
  
The following code uses QObject: tr (). It creates a pop-up menu named "Quit", which is placed on the menu bar, the label "File" is displayed on the menu bar ".
  
QPopupMenu * popup;
Popup = new QPopupMenu (this );
Popup-> insertItem (tr ("& Quit"), qApp, SLOT (quit ()));
Menubar-> insertItem (tr ("& File"), popup );
  
In most cases, the above method can be used. However, the preceding method cannot be used to define character strings used in some variables. However, to allow Qt to extract and translate the string, some methods must be used to mark the strings. Qt defines QT_TR_NOOP () and QT_TRANSLATE_NOOP () to mark them. The former is used for a single character string, and the latter is used for multiple strings. For example,
  
Static const char * strings [] = {
QT_TR_NOOP ("Hello "),
QT_TR_NOOP ("World ")
};
  
Sometimes you need to use functions such as printf/sprintf to dynamically generate strings, for example,
  
QStings s;
S. sprintf ("Button % d", I );
But-> setText (s );
  
The arg () function is used for internationalization.
  
QString s = tr ("Button % 1"). arg (I );
But-> setText (s );
  
The findtr Command provided by Qt is used to extract the above information:
  
Findtr [filename]. cpp> i18n. po
  
It is similar to GNU's xgettext. The extracted information files of the preceding files include,
  
....
"Content-Type: text/plain; charset = iso-8859-1 \ n"
  
#: I18n. cpp: 34
Msgid "ExampleWidget: & File"
Msgstr ""
...
  
Next is the text translation process. Attention should be paid to the following when translating information files in Qt: (1) the encoding of the extracted information file is a iso-8859-1, and when translating it into a language (encoding), attention should be paid to changing its character set, for example, the encoding for Chinese GB2312 and Big5 should be "Content-Type: text/plain; charset = gb2312 \ n" or "Content-Type: text/plain; charset = big5 \ n ". (2) The extracted information has a range. For example, if the range specified by the above File is ExampleWidget, remove it before translation and change it to msgid ": & File ". (3) The translated string may contain the accelerator key, such as "F" in "& File". It is best to keep this information if it is translated into Chinese, it can be translated into "file (& F )".
  
For translated files (for example, the above translation file is saved as i18n_gb.po), you must use the tool msg2qm provided by Qt to convert it to a. qm file,
  
> Msg2qm i18n_gb.po i18n_gb.qm
  
It is similar to the GNU msgfmt command. The translated files can be directly called using the Qt program.
  
QTranslator * translator = new QTranslator (0 );
Translator-> load ("i18n_gb.qm ",".");
QApp-> installTranslator (translator );
  
In addition, Qt provides a tool similar to msgmerge, mergetr, which is used to combine the Newly Extracted information files with the translated information files.
  
In Qt, you can also directly use QTextCodec to convert the string encoding, which brings convenience to the Development of pure Chinese software in Qt. However, this method is not consistent with the habit of internationalization/localization,
  
Char * string = "Chinese and English mixed strings! "
QTextCodec * gbk_codec = QTextCodec: codecByName ("GBK ");
QString gbk_string = codec-> toUnicode (string );
QLabel * label = new QLabel (gbk_string );
  
If the program supports only one encoding, you can also directly set the encoding of the entire application to GBK encoding, and then add tr (QObject: tr) before the string ),
  
QApp-> setdefacodecodec (QTextCodec: codecForName ("GBK "));
QLabel * label = new QLabel (tr ("Chinese label "));
  
If you want Qt to obtain the character set based on Locale environment variables, you can use
  
QString: fromLocal8Bit (str ).
  
For examples in this section, see qt-i18n-example.tar.gz
  
   2. text input of Qt
In terms of input, QLineEdit and QMultiLineEdit support XIM. You can enter Chinese, Japanese, and Korean as long as you match the corresponding input server. Currently, there are many XIM-supported software, such as Chinese: Chinput/xcin/rfinput/q9, Japanese: kinput2/skkinput, and Korean: ami/hanIM.
  
The default input style of the Qt program is OverTheSpot. It also supports OffTheSpot and Root. You can specify the input style on the command line when starting a program, for example,
  
  
./App-inputstyle overthespot # default style, cursor follows
./App-inputstyle offthespot
./App-inputstyle root
  
The Qt-2.2.0 patched with MiziLinux supports the OnTheSpot input style and uses it as the default input style. See http://www.mizi.com/ko/kde/doc/onthespot/onthespot.html.
  
Any Widget in Qt can accept input, as long as it can have Keyboard Focus ). Therefore, you only need to intercept keyboard input for processing the input of special widgets and obtain the strings from the XIM server. For OverTheSport style support, refresh the XIM input server location.
  
   3. Qt Printing
In terms of printing, Qt in XWindow generates PostScript and uses lpr for printing. It contains the QPrinter class, which can easily support control of output pages. For Chinese printing, the output part of the PostScript file must be corrected.
Related Article

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.