qml如何进行动态翻译

      Qt已经提供了语言家(linguist)生成语言库文件(*qm)和自带翻译的类,很方便的实现语言的切换。

功能概述:点击按钮,实现对应语言切换,关闭应用再次启动,保留上一次的设置。

 一.qml实现国际化的过程:

       在项目中加载翻译文件,在文本中利用标识需要翻译的文本,当程序接收到切换语言的信号时,应用会在加载的翻译文件查找对应的翻译,显示新的语言。

二、qml国际化的实现

2.1在pro文件添加TRANSLATION +=translate_cn.ts

打开qt命令行,执行lupdate  XXX/***  -ts translate_cn.ts  生成翻译文件,在这个文件里面会包含所有标识过的文本,

但是还没有翻译。

注意:The lupdate tool extracts user interface strings from your application. lupdate reads your application's .pro file to identify which source files contain texts to be translated. This means your source files must be listed in the SOURCES or HEADERS entry in the .pro file. If your files are not listed the texts in them will not be found.However, the SOURCES variable is intended for C++ source files. If you list QML or JavaScript source files there, the compiler tries to build them as though they are C++ files. As a workaround, you can use an lupdate_only{...} conditional statement so thelupdate tool sees the .qml files but the C++ compiler ignores them.

For example, the following .pro file snippet specifies two .qml files in the application.

lupdate_only{
SOURCES = main.qml \
          MainPage.qml
}
截取自  Qt助手中的:internationlization and  with quick  使用lupdate_only 可以文本匹配文件,自动识别qml中需要翻译的文本,点击菜单栏的工具—》外部—》qt语言家-》更新翻译翻译(lupdate)自动生成tanslate_cn.ts文件,编辑对应的翻译语言,翻译完成,保存,点击发布(或者使用lrelease    translate_cn.ts)生成对应的translate_cn.qm文件,为了方便加载,语言库文件添加到资源里面。

3,语言库的加载,通过C++实现语言动态切换,保存当前语言设置,再次运行,保持最后一次设置的语言

构造自己的类,继承qt中的Qtranslation类

在translate.h的文件中

classlanguageTranslator:publicQObject

{
    Q_OBJECT
//实现C++与qml之间的通信
    Q_PROPERTY (QString emptyString READ getEmptyString  NOTIFY languageChanged)    public :
        languageTranslator ();
        QString getEmptyString();
        Q_INVOKABLE  void setlectLanguage(int language);
  signals:
        void languageChanged();
    private:
        QTranslator *translator1;
};
在translate.cpp 中

languageTranslator::languageTranslator()

{
    translator1 = new QTranslator(this);
}
 
QString languageTranslator::getEmptyString()
{
  return "";
}
 
 void  languageTranslator::setlectLanguage(int  language)
{
 
     QSettings *readconfig = new QSettings(".config.ini",QSettings::IniFormat);
        if(language == 1)
     {
           translator1->load(":/lang.qm");
          qApp->installTranslator(translator1);
     }
 
     if(language == 2)
     {
         qApp->removeTranslator(translator1);
     }
 
     readconfig->setValue("Language/languageflag",language);
     readconfig->value("Language/languageflag").toFloat();
 
     emit languageChanged();
 
 }
 qApp一个指针 

通过每一次启动读取配置文本存储语言标志量(languageflag)实现显示最后一次设置的语言

当每次修改语言时候,main.cpp会对配置文本中变量languageflag做出修改,记录当前语言。

languageTranslatormyObj;

   QSettings *readconfig = new QSettings(".config.ini",QSettings::IniFormat);
   int flag = readconfig->value("Language/languageflag").toFloat();
   myObj.setlectLanguage(flag);
   view->rootContext()->setContextProperty("rootItem",(QObject *)&myObj);
  
将C++中的类注册到qml中使用,这样在qml就可以会用rootItem了

import QtQuick 2.3
import QtQuick.Controls 1.2
import QtQuick.Window 2.2
import SceneGraphRendering 1.0
import QtQuick.Window 2.0
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.2
import QtQml 2.2
import QmlMinAdjust 1.0
import languageTranslator 1.0
import QtGraphicalEffects 1.0
import "."
Rectangle{
    id: root
    width:1280
    height:720
 
    Row{
        Rectangle{
            id:directory
            x:0
            y:0
            width: root.width
            height: root.height
            color: "black"
 
       Column{
        Text {
            id:language
            x:root.width*1/2
            y:root.height*1/6
            font.pixelSize: 40
            width:root.width
            height:root.height*1/4
             text:qsTr("language") +rootItem.emptyString
            color: "green"
        }
 
        Button{
                id: chinese
                x: 0
                y: root.height*2/6
                width: root.width
                height: root.height*1/6
                text:"中文"
                style:ButtonFlatStyle
                {
                    font.family: "FontAwesome"
                    font.pixelSize: 20
                }
                onClicked: rootItem.setlectLanguage(1)
            }
 
        Button{
                id: english
                x: 0
                y: root.height*3/6
                width: root.width
                height: root.height*1/6
                text:"English"
                style:ButtonFlatStyle
                {
                    font.family: "FontAwesome"
                    font.pixelSize: 20
                }
                onClicked: {
                     rootItem.setlectLanguage(2)
                }
 
             }
 
             }
          }
        }
   }
 
 

Qt助手中的:internationlization and  Localization with quick 可供参考

nternationalization and Localization with Qt Quick

Internationalization and Localization with Qt Quick

Internationalization and Localization with Qt Quick


参考连接

https://wiki.qt.io/How_to_do_dynamic_translation_in_QML

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值