Cppcheck
translationhandler.h
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2024 Cppcheck team.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef TRANSLATIONHANDLER_H
20 #define TRANSLATIONHANDLER_H
21 
22 #include <QList>
23 #include <QObject>
24 #include <QString>
25 
26 class QTranslator;
27 
28 /// @addtogroup GUI
29 /// @{
30 
31 /**
32  * @brief Information for one translation.
33  *
34  */
36  /**
37  * @brief Readable name for the translation (e.g. "English").
38  *
39  */
40  QString mName;
41 
42  /**
43  * @brief Filename for the translation.
44  *
45  */
46  QString mFilename;
47 
48  /**
49  * @brief ISO 639 language code for the translation (e.g. "en").
50  *
51  */
52  QString mCode;
53 };
54 
55 /**
56  * @brief A class handling the available translations.
57  *
58  * This class contains a list of available translations. The class also keeps
59  * track which translation is the currently active translation.
60  *
61  */
62 class TranslationHandler : QObject {
63  Q_OBJECT
64 public:
65  explicit TranslationHandler(QObject *parent = nullptr);
66 
67  /**
68  * @brief Get a list of available translations.
69  * @return List of available translations.
70  *
71  */
72  const QList<TranslationInfo>& getTranslations() const {
73  return mTranslations;
74  }
75 
76  /**
77  * @brief Set active translation.
78  * @param code ISO 639 language code for new selected translation.
79  * @return true if succeeds, false otherwise.
80  *
81  */
82  bool setLanguage(const QString &code);
83 
84  /**
85  * @brief Get currently selected translation.
86  * @return ISO 639 language code for current translation.
87  *
88  */
89  const QString& getCurrentLanguage() const;
90 
91  /**
92  * @brief Get translation suggestion for the system.
93  * This function checks the current system locale and determines which of
94  * the available translations is best as current translation. If none of
95  * the available translations is good then it returns English ("en").
96  * @return Suggested translation ISO 639 language code.
97  *
98  */
99  QString suggestLanguage() const;
100 
101 protected:
102 
103  /**
104  * @brief Add new translation to list of available translations.
105  * @param name Name of the translation ("English").
106  * @param filename Filename of the translation.
107  *
108  */
109  void addTranslation(const char *name, const char *filename);
110 
111  /**
112  * @brief Find language in the list and return its index.
113  * @param code ISO 639 language code.
114  * @return Index at list, or -1 if not found.
115  *
116  */
117  int getLanguageIndexByCode(const QString &code) const;
118 
119 private:
120 
121  /**
122  * @brief ISO 639 language code of the currently selected translation.
123  *
124  */
126 
127  /**
128  * @brief List of available translations.
129  *
130  */
131  QList<TranslationInfo> mTranslations;
132 
133  /**
134  * @brief Translator class instance.
135  *
136  */
137  QTranslator* mTranslator{};
138 };
139 
140 /// @}
141 #endif // TRANSLATIONHANDLER_H
A class handling the available translations.
const QList< TranslationInfo > & getTranslations() const
Get a list of available translations.
TranslationHandler(QObject *parent=nullptr)
int getLanguageIndexByCode(const QString &code) const
Find language in the list and return its index.
const QString & getCurrentLanguage() const
Get currently selected translation.
QTranslator * mTranslator
Translator class instance.
bool setLanguage(const QString &code)
Set active translation.
void addTranslation(const char *name, const char *filename)
Add new translation to list of available translations.
QList< TranslationInfo > mTranslations
List of available translations.
QString suggestLanguage() const
Get translation suggestion for the system.
QString mCurrentLanguage
ISO 639 language code of the currently selected translation.
Information for one translation.
QString mName
Readable name for the translation (e.g.
QString mCode
ISO 639 language code for the translation (e.g.
QString mFilename
Filename for the translation.