QT---之QTranslator【界面语言翻译】及QT-Linguist的用法

       QTranslator类就是使用Qt的翻译文件,可以对界面中菜单栏、label、对话框等进行翻译。使用的前提是,开发者要自己创建翻译文 。

(一)QT项目实现多语言,必须做两件事
        1)确保每一个用户可见的字符串都使用了tr()函数。
        2)在应用程序启动的时候,使用QTranslator载入一个翻译文件(.qm)。
tr() 的用法
             caseCheckBox = new QCheckBox(tr("Match &case"));
在main()函数里载入翻译文件
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    //翻译程序
   QTranslator translator;
    translator.load("spreadsheet_cn.qm");
    app.installTranslator(&translator);
    ……
}
注意:翻译文件加载的位置必须在界面实例化之前完成。


(二)生成.qm翻译文件
1、在该应用程序的.pro文件文件中添加TRANSLATIONS项,可分别对应于不同的语言,如:spreadsheet_cn.ts, 对应中文,名字 可以自己定义,后缀名.ts不可变动。<.ts是可读的翻译文件,使用简单的XML格式;而.qm是经过.ts转换而成的二进制机器语言>
2、翻译文件。分三步来完成:
     1)运行lupdate, 从应用程序的源代码中提取所有用户可见的字符串。
     2)使用Qt Linguist 翻译该应用程序。
     3)运行lrelease,生成二进制的.qm 文件。
以上三步均需用到QT自带的命令行控制台,启动方法:开始--->所有程序--->Qt by Nokia v4.6.3 (OpenSource)--->Qt 4.6.3 Command Prompt
启动命令行后,对应输入如下命令:
    1)lupdate –verbose spreadsheet.pro //生成相应的.ts 文件
    2)linguist //启动Linguist语言翻译工具,可以翻译相应可见字符串
    3)lrelease –verbose spreadsheet.pro //将翻译好的文件生成.qm文件


(三)Linguist 语言工具的使用
    1)启动:命令行或者开始菜单均可【bin目录下输入Linguist
    2)打开:工具界面中的File--->Open,可以打开所需的 .ts 文件
    3)翻译:界面中部的翻译栏,两行:第一行:Source Text 第二行:… Translation, 在地二行进行相应的翻译即可,翻译完一条之后点击“确定下一个”按钮。
   4)发布:点击File--->Release, 生成 .qm 文件。(与命令行的效果一样)

QTranslator使用整理

   源码 

    QTranslator *translator = new QTranslator;
    translator->load("../TestHello/TestHello.qm");
    a.installTranslator(translator);

    MainWindow w;
    w.show();

testHello.ts内容 

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="zh_CN">
<context>
    <name>MainWindow</name>
    <message>
        <location filename="mainwindow.ui" line="14"/>
        <source>MainWindow</source>
        <translation>主窗口</translation>
    </message>
    <message>
        <location filename="mainwindow.ui" line="27"/>
        <source>hello</source>
        <translation>你好</translation>
    </message>
    <message>
        <location filename="mainwindow.ui" line="40"/>
        <source>china</source>
        <translation>中国</translation>
    </message>
</context>
</TS>

 mainwindow.ui 

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>70</x>
      <y>110</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>hello</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_2">
    <property name="geometry">
     <rect>
      <x>230</x>
      <y>110</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>china</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>400</width>
     <height>19</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

---------------------

本文来自 yuxing55555 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/yuxing55555/article/details/76099572?utm_source=copy 

猜你喜欢

转载自blog.csdn.net/weixin_39609623/article/details/82802666