-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathTranslator.h
48 lines (42 loc) · 1.16 KB
/
Translator.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#pragma once
#include <QObject>
#include <QTranslator>
#include <QLocale>
#include <QMap>
//管理语言翻译
class Translator : public QObject
{
Q_OBJECT
Q_PROPERTY(Translator::Language language READ getLanguage WRITE setLanguage NOTIFY languageChanged)
Q_PROPERTY(QLocale locale READ getLocale NOTIFY languageChanged)
public:
enum Language {
AnyLanguage = 0, //未设置
ZH_CN, //中文
EN_US //英文
};
Q_ENUM(Language)
private:
explicit Translator(QObject *parent = nullptr);
public:
~Translator();
static Translator *getInstance();
Translator::Language getLanguage() const;
void setLanguage(Translator::Language type);
//用于一些组件属性绑定
QLocale getLocale() const;
private:
//QTranslator在Qt5.15才有language接口,低版本可以自己解析
QString parseLanguage(const QString &qmpath);
signals:
void languageChanged();
private:
//当前语言设置
Language lang{AnyLanguage};
//加载翻译文件
QTranslator trans;
//语言枚举与language name的映射表
QMap<Language,QString> langMap;
//当前locale设置
QLocale locale;
};