-
Notifications
You must be signed in to change notification settings - Fork 39
/
translator.cpp
154 lines (124 loc) · 5.15 KB
/
translator.cpp
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
////////////////////////////////////////////////////////////////////////////////
// This file is part of LibreELEC - http://www.libreelec.tv
// Copyright (C) 2016 Team LibreELEC
//
// LibreELEC is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// LibreELEC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with LibreELEC. If not, see <http://www.gnu.org/licenses/>.
////////////////////////////////////////////////////////////////////////////////
#include "translator.h"
#include <QApplication>
#include <QDebug>
#include <QFile>
#include <QDir>
#include <QCollator>
#include <QRegularExpression>
#include <algorithm>
Translator::Translator(QObject *parent, QSettings *set) :
QObject(parent),
settings(set)
{
qtranslator = new QTranslator();
}
Translator::~Translator()
{
delete qtranslator;
}
void Translator::fillLanguages(QMenu *menuPtr, QPushButton *langBtnPtr)
{
menu = menuPtr;
langBtn = langBtnPtr;
QStringList qmFiles;
// languages from resources
qmFiles << QDir(":/lang").entryList(QStringList("*.qm"));
// add menu entry for all the files
QList<QAction *> actions;
foreach (const QString &qmFile, qmFiles) {
QRegularExpression regExp = QRegularExpression("lang-(.*)\\.qm");
QRegularExpressionMatch match = regExp.match(qmFile);
QString locale = match.captured(1);
QIcon icon;
QString iconName = "flag-" + locale + ".png";
if (QFile::exists(":/lang/" + iconName))
icon = QIcon(":/lang/" + iconName);
else if (QFile::exists(iconName))
icon = QIcon(iconName);
else
icon = QIcon(":/lang/flag-empty.png");
QString lang = QLocale(locale).nativeLanguageName();
lang = lang.left(1).toUpper() + lang.mid(1); // capitalize first letter
QString langEn = QLocale::languageToString(QLocale(locale).language());
// make names nicer
lang.replace("British English", "English UK");
lang.replace("American English", "English US");
lang.replace("Portugu" + QString::fromUtf8("\xc3\xaa") + "s europeu", \
"Portugu" + QString::fromUtf8("\xc3\xaa"));
lang.replace("Espa" + QString::fromUtf8("\xc3\xb1") + "ol de Espa" + QString::fromUtf8("\xc3\xb1") + "a", \
"Espa" + QString::fromUtf8("\xc3\xb1") + "ol");
langEn.replace("NorwegianBokmal", "Norwegian");
QAction *action = new QAction(langEn + " / " + lang, menu);
action->setCheckable(true);
action->setIcon(icon);
action->setData(locale);
actions << action;
}
// sort actions by country name
QCollator collator;
collator.setNumericMode(false);
collator.setCaseSensitivity(Qt::CaseSensitive);
std::sort(actions.begin(), actions.end(),
[&collator](const QAction *act1, const QAction *act2)
{return collator.compare(act1->text(), act2->text()) < 0;}
);
menu->addActions(actions); // add to menu
connect(menu, SIGNAL(triggered(QAction*)), SLOT(languageAction(QAction*)));
QString locale = settings->value("preferred/lang").toString();
// set first time locale from the system
if (locale.isEmpty() && QLocale::system().uiLanguages().count() >= 1) {
locale = QLocale::system().uiLanguages().at(0);
locale.replace("-", "_");
}
// check for file in resources and on disk
if (QFile::exists(":/lang/lang-" + locale + ".qm") == false &&
QFile::exists("lang-" + locale + ".qm") == false)
locale = "en_GB"; // default locale
for (int i=0; i<menu->actions().count(); i++) {
if (locale == menu->actions().at(i)->data()) {
langBtn->setIcon(menu->actions().at(i)->icon());
languageAction(menu->actions().at(i));
break;
}
}
QApplication::setLayoutDirection(QLocale(locale).textDirection());
}
void Translator::languageAction(QAction *action)
{
QString locale = action->data().toString();
settings->setValue("preferred/lang", locale);
langBtn->setIcon(action->icon());
if (qtranslator->isFilled())
qApp->removeTranslator(qtranslator);
bool loaded = false;
if (QFile::exists(":/lang/lang-" + locale + ".qm"))
loaded = qtranslator->load(":/lang/lang-" + locale + ".qm");
else
loaded = qtranslator->load("lang-" + locale + ".qm");
if (loaded && qtranslator->isFilled())
qApp->installTranslator(qtranslator);
// clear checked status
for (int i=0; i<menu->actions().count(); i++)
menu->actions().at(i)->setChecked(false);
// set checked and tooltip for current one
action->setChecked(true);
langBtn->setToolTip(action->text());
QApplication::setLayoutDirection(QLocale(locale).textDirection());
}