-
Notifications
You must be signed in to change notification settings - Fork 0
/
qlanguageselector.h
164 lines (145 loc) · 4.98 KB
/
qlanguageselector.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#ifndef QLANGUAGESELECTOR_H
#define QLANGUAGESELECTOR_H
#include <QtGui>
#if QT_VERSION_MAJOR >= 5
# include <QtWidgets>
#endif
#include "qtexttranslator.h"
#ifndef LANGUAGES_INI
#define LANGUAGES_INI "languages.ini"
#endif
#ifndef LANGUAGE_DEFUALT
#define LANGUAGE_DEFUALT "English"
#endif
struct LanguageInfo
{
/**
* @brief Code
* Code page for the language. Usually, e.g. it may be 'en', or 'en_US'
*/
QString Code;
/**
* @brief Caption
* The language name which is human-readarable. e.g. 'English', 'Chinese(Simplified)'
*/
QString Caption;
/**
* @brief Qm
* The translation file name which is Qt standard(qm format).
* If Qm is empty, and ForseUseText=false, this section will be default language for the application.
*/
QString Qm;
/**
* @brief TextFile
* The translation file name which is text(Internally, ini) format.
* Users can edit the files.
* And QLanguageSelector will load and change all 'tr' words every time when the menu is selected.
*/
QString TextFile;
/**
* @brief OpenTextEditor
* When user select the menu, open the 'TextFile' with a text editor, if the flag is true.
*/
bool OpenTextEditor;
};
/**
* @brief The QLanguageSelector class
*
* This object provides a mechanism for applications to make language changes by users.
* Each language is registered as a child QAction menu of QMenu specified.
* Each language is described in the ini file (usually "translations/languages.ini").
* Allow users of the application to add new languages without changing the source code.
* When the application user selects the Language Menu,
* the QLanguageSelector automatically sets the new QTranslator
* to qApp and replaces the language resources of the application.
* Eventually the languageChanged signal is emitted and you can handle additional change events.
*/
class QLanguageSelector : public QObject
{
Q_OBJECT
public:
/**
* @brief QLanguageSelector
* @param prefix: prefix of *.qm files e.g. [prefix][langageId].qm
* @param path: The path of the directory where the qm file is stored
*/
QLanguageSelector(QString prefix, QString path="translations/");
QString language() { return m_uiLanguage; }
/**
* @brief initialize
*
* Load language.ini and recognize the list of languages this application can switch.
* Besides the usual binary(.qm) format, it supports text format (TransConf) converted from TS(.ts) format.
*/
void initialize(QString path="");
/**
* @brief initializeMenu
* @param parent
*
* Create a menu that allows you to change the display language.
* For each specified parent menu, add each language registered in language.ini as a submenu.
* Each menu is checkable, and the currently selected menu is checked.
* When the user selects a menu, the display language of the application is changed.
*/
void initializeMenu(QMenu* parent);
/**
* @brief getLanguageInfo
* @param languageId
*
* Returns an instance of the language information specified by languageId.
* This is the information set in languages.ini.
*/
LanguageInfo getLanguageInfo(QString languageId);
/**
* @brief getSystemLanguageInfo
*
* Returns language information that matches the language resource
* of the environment in which the application is executed.
* If it does not match, it returns the language information specified by LANGUAGE_DEFUALT.
*/
LanguageInfo getSystemLanguageInfo();
void setForceUseText(bool force) { m_forceUseText = force; }
bool ForceUseText() const { return m_forceUseText; }
void copyLanguages(QMap<QString, LanguageInfo>& languages) { m_languages = languages; }
QMap<QString, LanguageInfo>& Languages() { return m_languages; }
signals:
/**
* @brief languageChanged
* @param languageId
*
* Called after the language resource of the application has been replaced.
* To change the language of Widgets other than QMainWindow
* you should receive this signal in your slot.
*/
void languageChanged(QString languageId);
/**
* @brief openTextEditorForLanguage
* @param info
*
* Called before the text translation file selected.
*/
void openTextEditorForLanguage(LanguageInfo info);
public slots:
/**
* @brief resetTranslator
* @param languageId: language name in English
*
* Replace the language resources of the application.
*/
void resetTranslator(QString languageId);
private:
QString m_uiLanguage;
QTranslator *m_translator;
QTextTranslator *m_reversed;
QString m_path;
QString m_prefix;
QString m_reverseFile;
bool m_forceUseText;
bool m_textEditorOpened;
bool m_userSelecting;
QMap<QString, LanguageInfo> m_languages;
QList<QAction*> m_actions;
QStringList m_languageList;
void clearLanguageMenus();
};
#endif // QLANGUAGESELECTOR_H