summaryrefslogtreecommitdiffstats
path: root/app/translator.cpp
blob: 9b67f13518849682daa1dd73ec00f1024d3711d6 (plain)
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
#include "translator.h"

#include <QtCore/QCoreApplication>
#include <QtCore/QLocale>
#include <QtCore/QTranslator>
#include <QtCore/QDir>
#include <QtCore/QDebug>

Translator::Translator(QObject *parent)
    : QObject(parent)
    , m_language(QStringLiteral("C"))
    , m_translator(nullptr)
{
}

QString Translator::translate(const QString &string, const QString &language) const
{
    Q_UNUSED(language)
    return string;
}

QString Translator::language() const
{
    return m_language;
}

void Translator::setLanguage(const QString &language)
{
    if (m_language == language) return;
    m_language = language;
    setTranslator(language);
    emit languageChanged(language);
}

void Translator::setTranslator(const QString &language)
{
    if (m_translator) {
        QCoreApplication::removeTranslator(m_translator);
    } else {
        m_translator = new QTranslator(this);
    }
    QLocale locale(language);
    QString fileName = QCoreApplication::instance()->applicationName().toLower();
    qDebug() << "####" << QDir::currentPath() << QCoreApplication::applicationDirPath();
    if (m_translator->load(locale, fileName, QStringLiteral("_"), QStringLiteral("%1/../translations").arg(QCoreApplication::applicationDirPath()))) {
        QCoreApplication::installTranslator(m_translator);
    } else {
        delete m_translator;
        m_translator = nullptr;
    }
}