1 #include "translator.h"
3 #include <QtCore/QCoreApplication>
4 #include <QtCore/QLocale>
5 #include <QtCore/QTranslator>
7 #include <QtCore/QDebug>
9 Translator::Translator(QObject *parent)
11 , m_language(QStringLiteral("C"))
12 , m_translator(nullptr)
16 QString Translator::translate(const QString &string, const QString &language) const
22 QString Translator::language() const
27 void Translator::setLanguage(const QString &language)
29 if (m_language == language) return;
30 m_language = language;
31 setTranslator(language);
32 emit languageChanged(language);
35 void Translator::setTranslator(const QString &language)
38 QCoreApplication::removeTranslator(m_translator);
40 m_translator = new QTranslator(this);
42 QLocale locale(language);
43 QString fileName = QCoreApplication::instance()->applicationName().toLower();
44 qDebug() << "####" << QDir::currentPath() << QCoreApplication::applicationDirPath();
45 if (m_translator->load(locale, fileName, QStringLiteral("_"), QStringLiteral("%1/../translations").arg(QCoreApplication::applicationDirPath()))) {
46 QCoreApplication::installTranslator(m_translator);
49 m_translator = nullptr;