Qt4之国际化精晣

还是从一个Hello World开始吧。

1.首先这个例子在D:\Qt\4.7.3\examples\linguist\hellotr,当然不同的版本和路径则有所不同。

这个例子中有两个文件:

main.cpp:

/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation ([email protected])
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
**     the names of its contributors may be used to endorse or promote
**     products derived from this software without specific prior written
**     permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QApplication>
#include <QPushButton>
//! [0]
#include <QTranslator>
//! [0]

//! [1] //! [2]
int main(int argc, char *argv[])
//! [1] //! [3] //! [4]
{
    QApplication app(argc, argv);
//! [3]

//! [5]
    QTranslator translator;
//! [5] //! [6]
    translator.load("hellotr_la");
//! [6] //! [7]
    app.installTranslator(&translator);
//! [4] //! [7]

//! [8]
    QPushButton hello(QPushButton::tr("Hello world!"));
//! [8]
    hello.resize(100, 30);

    hello.show();
    return app.exec();
}
//! [2]

而hellotr.pro文件如下:

TEMPLATE = app
TARGET = 
DEPENDPATH += .
INCLUDEPATH += .

# Input
SOURCES += main.cpp
TRANSLATIONS += hellotr_la.ts
CODECFORTR = utf-8 #or gbk

其中:

TRANSLATIONS += hellotr_la.ts
CODECFORTR = utf-8 #or gbk
为国际化资源文件以及它的编码。


以下是一些dos命令:

在hellotr.pro添加如下代码:
 SOURCES      = main.cpp
 TRANSLATIONS = hellotr_la.ts
CODECFORTR = utf-8 #or gbk

•在 .pro 中指定要生成的ts文件时,在 .pro 文件内指定编码
CODECFORTR = utf-8 #or gbk

运行:
或者运行:
lupdate -verbose hellotr.pro
生成hellotr_la.ts

查看hellotr_la.ts并保存.
linguist hellotr_la.ts

运行
lrelease  hellotr.pro
生成
hellotr_la.qm

qmake -project
qmake hellotr.pro
make

之后在文件中生成如下所示:

Qt4之国际化精晣_第1张图片

未经过处理,直接运行debug下面的exe程序,得到如下:


将hellotr_la.qm放在debug目录下,或将debug目录下面的exe文件放至hellotr目录下面

可以得到如下:



最后再讲解一下hellotr_la.ts,这个文件通过运行lupdate -verbose hellotr.pro生成,或者在hellotr.pro中加入TRANSLATIONS += hellotr_la.ts生成。

而CODECFORTR = utf-8 #or gbk用于指定hellotr_la.ts中的编码,不然会产生乱码。

hellotr_la.ts源码如下:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.0" language="zh_CN">
<defaultcodec>UTF-8</defaultcodec>
<context>
    <name>QPushButton</name>
    <message>
        <location filename="main.cpp" line="63"/>
        <source>Hello world!</source>
        <translation type="unfinished">你好</translation>
    </message>
</context>
</TS>


注意:要去掉type="unfinished",不然这个资源文件将不起重要的。

待续之。。。。。




你可能感兴趣的:(express,qt,documentation,Nokia,encoding,translation)