10 #include <QActionGroup>
13 #include <QGlobalStatic>
14 #include <QMessageBox>
15 #include <QRegularExpression>
17 #include <QTranslator>
26 return *sTranslationmanager;
30 const QString &
path) {
31 mTranslatorFileInfo.append({prefix,
path});
35 const QLocale locale(languagePref());
37 const auto &info = mTranslatorFileInfo;
39 for (
const auto &fileInfo : info) {
40 auto translator =
new QTranslator(
ecvApp);
42 bool loaded = translator->load(locale, fileInfo.prefix,
43 QStringLiteral(
"_"), fileInfo.path);
46 ecvApp->installTranslator(translator);
54 const QLocale locale(language);
56 const auto &info = mTranslatorFileInfo;
58 for (
const auto &fileInfo : info) {
59 auto translator =
new QTranslator(
ecvApp);
61 bool loaded = translator->load(locale, fileInfo.prefix,
62 QStringLiteral(
"_"), fileInfo.path);
65 ecvApp->installTranslator(translator);
73 const QString &pathToTranslationFiles) {
74 const LanguageList cList = availableLanguages(
75 QStringLiteral(
"ACloudViewer"), pathToTranslationFiles);
77 QActionGroup *group =
new QActionGroup(menu);
79 group->setExclusive(
true);
81 QAction *action = group->addAction(tr(
"No Translation (English)"));
83 action->setCheckable(
true);
84 action->setChecked(
true);
86 connect(action, &QAction::triggered,
this,
87 [
this]() { setLanguagePref(QStringLiteral(
"en")); });
89 QAction *separator =
new QAction(group);
90 separator->setSeparator(
true);
92 const QString currentLanguage = languagePref();
94 for (
auto &langInfo : cList) {
95 action = group->addAction(langInfo.second);
97 action->setCheckable(
true);
99 if (currentLanguage == langInfo.first) {
100 action->setChecked(
true);
103 connect(action, &QAction::triggered,
this,
104 [=]() { setLanguagePref(langInfo.first); });
107 menu->addActions(group->actions());
110 const QString ccTranslationManager::languagePref() {
113 settings.beginGroup(
"Translation");
115 const QString langCode =
116 settings.value(QStringLiteral(
"Language")).toString();
123 ccTranslationManager::LanguageList ccTranslationManager::availableLanguages(
124 const QString &appName,
const QString &pathToTranslationFiles) {
125 LanguageList theList;
127 QDir dir(pathToTranslationFiles);
129 const QString cFilter = QStringLiteral(
"%1_*.qm").arg(appName);
130 const QStringList cFileNames = dir.entryList({cFilter});
132 QRegularExpression regExp(
133 QStringLiteral(
"%1_(?<langCode>.{2}).*.qm").arg(appName));
137 for (
const auto &cFileName : cFileNames) {
138 QRegularExpressionMatch match = regExp.match(cFileName);
140 if (!match.hasMatch()) {
144 const QString cLangCode(match.captured(QStringLiteral(
"langCode")));
146 QString language(QLocale(cLangCode).nativeLanguageName());
148 if (language.isEmpty()) {
149 qWarning() <<
"Language not found for translation file"
154 language = language.at(0).toUpper() + language.mid(1);
156 theList += {cLangCode, language};
162 void ccTranslationManager::setLanguagePref(
const QString &languageCode) {
163 if (languageCode == languagePref()) {
169 settings.beginGroup(
"Translation");
171 settings.setValue(QStringLiteral(
"Language"), languageCode);
175 QMessageBox::information(
nullptr, tr(
"Language Change"),
176 tr(
"Language change will take effect when "
177 "ACloudViewer is restarted"));
void populateMenu(QMenu *menu, const QString &pathToTranslationFiles)
void loadTranslation(QString language)
void registerTranslatorFile(const QString &prefix, const QString &path)
#define ecvApp
Mimic Qt's qApp for easy access to the application instance.
Q_GLOBAL_STATIC(PrivatePluginManager, sPluginManager)
static const std::string path