QT-Linguist国际化的简单实现

QT-Linguist工具主要用在项目的多语言翻译处理过程中,所有先简单介绍一下整个多语言处理过程,最后介绍Linguist的用法。

 

QT项目实现多语言,必须做两件事:

1)确保每一个用户可见的字符串都使用了tr()函数

2)在应用程序启动的时候,使用QTranslator载入一个翻译文件(.qm)

 

(一)编写源码

类SpeedLinguist:

Cpp代码 复制代码   收藏代码
  1. #ifndef SPEEDLINGUIST_H_   
  2. #define SPEEDLINGUIST_H_   
  3.   
  4. #include <QtCore/QObject>   
  5. #include <QtGui/QWidget>   
  6.   
  7. class SpeedLinguist: public QWidget   
  8. {   
  9.     Q_OBJECT   
  10.        
  11. public:   
  12.     SpeedLinguist(QObject *parent = 0);   
  13. };   
  14.   
  15. #endif  

 

 

Cpp代码 复制代码   收藏代码
  1. #include "speedlinguist.h"   
  2. #include <QtGui/QPushButton>   
  3. #include <QtGui/QVBoxLayout>   
  4.   
  5. SpeedLinguist::SpeedLinguist(QObject *parent)   
  6. {   
  7.     QPushButton *pushButton = new QPushButton(tr("lingust default"));   //翻译工具linguist的依据tr()函数   
  8.     QVBoxLayout *layout = new QVBoxLayout;   
  9.     layout->addWidget(pushButton);   
  10.        
  11.     this->resize(480,240);   
  12.     this->setLayout(layout);   
  13. }  

在主函数中,必须载入翻译文件,下面代码中的speedlingiust_zh_cn.qm文件,由linguist工具生成,后面会详细介绍生成过程:

 

Cpp代码 复制代码   收藏代码
  1. #include "speedlinguist.h"   
  2. #include <QtGui/QApplication>   
  3. #include <QtCore/QTranslator>   
  4.   
  5. int main(int argc, char *argv[])   
  6. {   
  7.     QApplication app(argc, argv);   
  8.        
  9.     //载入翻译文件   
  10.     QTranslator translator;   
  11.     translator.load("speedlingiust_zh_cn.qm");  //语言包文件,由linguist工具生成   
  12.     app.installTranslator(&translator);   
  13.        
  14.     SpeedLinguist speedLinguist;   
  15.     speedLinguist.show();   
  16.        
  17.     return app.exec();   
  18. }  

编写好代码之后,打开命令行终端,执行命令:

 
Terminal代码 复制代码   收藏代码
  1. qmake -projecct  

生成工程文件tmp.pro,打开tmp.pro文件,添加一行翻译配置:

 

Pro代码 复制代码   收藏代码
  1. TRANSLATIONS = speedlinguist_zh_cn.ts speedlinguist_en.ts  

如下图所示:


QT-Linguist国际化的简单实现_第1张图片

 

其作用是,指明在项目中生成翻译源文件speedlinguist_zh_cn.ts和speedlinguist_en.ts,两者空格隔开。说明一下,文件后缀ts是英文translation source的简写;名称后缀zh_cn表明speedlinguist_zh_cn.ts是中文翻译源文件,很显然,en对应英式英文。建议使用这种方式命名文件,目的是方便linguist工具自动识别目标语言。

参考地址:http://blog.interidea.org/2010/07/18/%E8%AF%AD%E8%A8%80%E5%8C%85%E7%BC%A9%E5%86%99/

 

(二)生成语言包

(1)执行命令:

 

Terminal代码 复制代码   收藏代码
  1. lupdate tmp.pro  

生成翻译源文件speedlinguist_zh_cn.ts和speedlinguist_en.ts,如下图所示


QT-Linguist国际化的简单实现_第2张图片

(2)使用命令:

 

Terminal代码 复制代码   收藏代码
 
  1. linguist  

打开linguist工具后,使用linguist工具打开speedlinguist_zh_cn.ts文件。

在这里,我们看到linguist工具针对tr()函数的字符串参数进行翻译。在linguist工具中,对speedlinguist.cpp文件里的代码

QPushButton *pushButton = new QPushButton(tr("lingust default"));

的字符串“lingust default”翻译成中文“欢迎来到中国”,保存,当然,这样翻译在语言上讲,是不正确的。

同理,使用linguist工具打开speedlinguist_en.ts文件进行翻译,暂且翻译成“Welcome to England”。

(3)使用命令:

 

Terminal代码 复制代码   收藏代码
  1. lrelease tmp.pro  

生成二进制文件.qm,在上面主函数main()被使用。如下图所示


QT-Linguist国际化的简单实现_第3张图片
 以上3个步骤的结果是,生成了翻译语言包,以.qm作为后缀的文件。下来,执行命令:

 

Terminal代码 复制代码   收藏代码
  1. qmake  

生成Makefile文件后,执行命令:

 

Terminal代码 复制代码   收藏代码
  1. mingw32-make  

提一下,我在windows中测试,使用minGW编译器。执行完该命令后,生成可执行文件tmp.exe(前提没有修改.pro文件的目标文件名),通过命令运行程序,如下图所示:


QT-Linguist国际化的简单实现_第4张图片

 也可以,通过修改main()函数,加载speedlinguist_en.qm文件,使用生成英式英文语言包。还有在上面生成.qm文件的步骤中,也可以使用linguist工具的直接生成,而无需输入命令,方法是在完成翻译后,打开菜单File,点击Release选项,生成.qm文件。

你可能感兴趣的:(layout,语言,工具,makefile,Terminal,translation)