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 (qt-info@nokia.com)
**
** 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

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


未经过处理,直接运行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",不然这个资源文件将不起重要的。

待续之。。。。。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值