-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.cpp
70 lines (58 loc) · 1.92 KB
/
utils.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
#include "utils.h"
#include <QCoreApplication>
#include <QStandardPaths>
#include <QDir>
#include <QStringList>
#include <QMutableStringListIterator>
#include <QSettings>
#include <QLocale>
Utils::Utils(QObject *parent) : QObject(parent)
{
}
QString Utils::getLocalName()
{
QString localDataLocation = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
QDir dir(localDataLocation);
dir.mkpath(localDataLocation);
QSettings settings(QString("%1/%2.ini").arg(localDataLocation, qApp->applicationName()),
QSettings::IniFormat);
QString localName = settings.value("locale").toString();
if (localName.isEmpty()) {
localName = QLocale::system().name();
settings.setValue("locale", localName);
}
return localName;
}
void Utils::setLocalName(const QString &name)
{
QString localDataLocation = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
QDir dir(localDataLocation);
dir.mkpath(localDataLocation);
QSettings settings(QString("%1/%2.ini").arg(localDataLocation, qApp->applicationName()),
QSettings::IniFormat);
settings.setValue("locale", name);
}
QStringList Utils::findQmFiles()
{
QDir dir(":/translations");
QStringList fileNames = dir.entryList(QStringList("*.qm"), QDir::Files,
QDir::Name);
QMutableStringListIterator i(fileNames);
while (i.hasNext()) {
i.next();
i.setValue(dir.filePath(i.value()));
}
return fileNames;
}
QString Utils::findQmFile(const QString &localName)
{
QStringList qmFiles = Utils::findQmFiles();
QString languageCode = localName.left(2);
foreach (const QString &qmFile, qmFiles) {
QFileInfo info(qmFile);
if (info.baseName() == QString("%1_%2").arg(qApp->applicationName(), languageCode)) {
return qmFile;
}
}
return QString();
}