C 语言

关注公众号 jb51net

关闭
首页 > 软件编程 > C 语言 > Qt加载qm文件

详解Qt如何实现一键加载qm文件

作者:雲烟

这篇文章主要为大家详细介绍了Qt实现一键加载qm文件的相关方法,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考下

背景

多个子项目,分别翻译生成的qm文件,如何一键加载?

.h

#include <QtCore/QObject>
#include <QtCore/QTranslator>
 
class LanguageSelector:public QObeject{
    Q_OBJECT
public:
    ~LanguageSelector() override = default;
    void setLanguage(const QString &l);
    QString getLanguage();
    void addLanguageFile(const QString &n);
    void removeLanguageFile(const QString &n);
    void reload();
    static LanguageSelector *getInstance();
signals:
    void languageChanged(const QString &l);
privite:
    LanguageSelector();
    static LanguageSelector* instance;
    static constexpr char CONFIGER_KEY[] = "system/language";
    QString baseDir;
    QList<QString> languageFiles;
    QList<QTranslator *> translations;
}

.cpp

#include "languageselector.h"
#include <QApplication>
#include <QCoreApplication>
#include <QDebug>
#include <QStringBuilder>
#include <QSettings>
 
LanguageSelector *LanguageSelector::instance = nullptr;
constexpr char LanguageSelector::CONFIGER_KEY[];
QString LanguageSelector::getLanguage()
{
    return QSettings::value( CONFIGER_KEY, defaultValue );
}
 
void LanguageSelector::setLanguage(const QString &language)
{
    QSettings qs;
    auto ov = QSettings::value( CONFIGER_KEY, defaultValue );
    qs::setValue( CONFIGER_KEY, language);
    if(ov != l){
        reload();
        emit languageChanged( language);
    }
}
 
LanguageSelector *LanguageSelector::getInstance()
{
    if(!instance){
        instance = new LanguageSelector;
    }
    return instance;
}
 
LanguageSelector::LanguageSelector()
    : baseDir( "i18n" ) {}
 
void LanguageSelector::addLanguageFile(const QString &name)
{
    if(name.isEmpty()){
        return;
    }
    languageFiles.append(name);
}
 
void LanguageSelector::removeLanguageFile(const QString &name)
{
    if(name.isEmpty()){
        return;
    }
    languageFiles.removeOne(name);
}
 
void LanguageSelector::reload()
{
    auto language = getLanguage();
    QString qmBase = QApplication::applicationDirPath() + "/" + baseDir;
    if(!qmBase.isEmpty() && !qmBase.endWith("/")){
        qmBase += "/";
    }
    if(!translations.isEmpty()){
        for(auto const t : translations){
            QCoreApplication::removeTranslator(t);
            t->deleteLater();
        }
    }
    for(auto const &qm :languageFiles){
    auto *t = new QTranslator(this);
    if(t->load(qmBase % qm % "_" % language ))
        translations.append(t);
        QCoreApplication::installTranslator(t);
    }else{
        qDebug() << "can not load lang file:" << qm << ", language" << language ;
    }
}

使用

main中,节目构造之前添加:

      auto ls = LanguageSelector::getInstance();
            ls->addLangFile( "test" );
            ls->addLangFile( "local" );
            ls->addLangFile( "remote" );
            ls->reload();

到此这篇关于详解Qt如何实现一键加载qm文件的文章就介绍到这了,更多相关Qt加载qm文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文