2017-09-07 10:44:15 +00:00
|
|
|
#include "translationmanager.h"
|
|
|
|
|
|
|
|
#include <QCoreApplication>
|
2017-09-09 04:24:04 +00:00
|
|
|
#include <QDir>
|
2017-09-07 10:44:15 +00:00
|
|
|
#include <QTranslator>
|
|
|
|
|
2019-03-10 18:03:59 +00:00
|
|
|
#include "util/fileutil.h"
|
2017-09-09 04:24:04 +00:00
|
|
|
#include "util/stringutil.h"
|
|
|
|
|
|
|
|
#define TRANSLATION_FILE_PREFIX "i18n_"
|
|
|
|
#define TRANSLATION_FILE_SUFFIX ".qm"
|
|
|
|
|
2017-09-07 10:44:15 +00:00
|
|
|
TranslationManager::TranslationManager(QObject *parent) :
|
|
|
|
QObject(parent),
|
2017-09-09 04:24:04 +00:00
|
|
|
m_language(0)
|
2017-09-07 10:44:15 +00:00
|
|
|
{
|
2017-09-09 04:24:04 +00:00
|
|
|
setupTranslation();
|
2017-09-07 10:44:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TranslationManager::~TranslationManager()
|
|
|
|
{
|
2017-09-09 04:24:04 +00:00
|
|
|
uninstallAllTranslators();
|
2017-09-07 10:44:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TranslationManager *TranslationManager::instance()
|
|
|
|
{
|
2017-12-10 07:07:05 +00:00
|
|
|
static TranslationManager *g_instanceTranslationManager = nullptr;
|
2017-09-07 10:44:15 +00:00
|
|
|
|
|
|
|
if (!g_instanceTranslationManager) {
|
|
|
|
g_instanceTranslationManager = new TranslationManager();
|
|
|
|
}
|
|
|
|
return g_instanceTranslationManager;
|
|
|
|
}
|
|
|
|
|
2017-09-09 04:24:04 +00:00
|
|
|
void TranslationManager::setupTranslation()
|
2017-09-07 10:44:15 +00:00
|
|
|
{
|
2017-09-09 04:24:04 +00:00
|
|
|
// Collect locales from i18n files
|
|
|
|
const int prefixLen = QLatin1String(TRANSLATION_FILE_PREFIX).size();
|
2017-09-07 10:44:15 +00:00
|
|
|
|
2017-09-09 04:24:04 +00:00
|
|
|
m_locales.append(QLocale(QLocale::English, QLocale::UnitedStates));
|
2017-09-07 10:44:15 +00:00
|
|
|
|
2017-09-09 04:24:04 +00:00
|
|
|
foreach (const QFileInfo &fileInfo, QDir(i18nDir())
|
|
|
|
.entryInfoList(QStringList() << ("*" TRANSLATION_FILE_SUFFIX))) {
|
|
|
|
const QString localeName = fileInfo.completeBaseName().mid(prefixLen);
|
|
|
|
const QLocale locale(localeName);
|
2017-09-07 10:44:15 +00:00
|
|
|
|
2017-09-09 04:24:04 +00:00
|
|
|
m_locales.append(locale);
|
|
|
|
}
|
2017-09-07 10:44:15 +00:00
|
|
|
|
2017-09-09 04:24:04 +00:00
|
|
|
// Translators will be loaded later when needed
|
|
|
|
m_translators.resize(m_locales.size());
|
|
|
|
m_translators.fill(nullptr);
|
2017-09-07 10:44:15 +00:00
|
|
|
}
|
|
|
|
|
2017-09-09 04:24:04 +00:00
|
|
|
QStringList TranslationManager::naturalLabels() const
|
2017-09-07 10:44:15 +00:00
|
|
|
{
|
2017-09-09 04:24:04 +00:00
|
|
|
QStringList list;
|
|
|
|
list.reserve(m_locales.size());
|
|
|
|
|
|
|
|
foreach (const QLocale &locale, m_locales) {
|
|
|
|
list.append(StringUtil::capitalize(
|
|
|
|
locale.nativeLanguageName()));
|
2017-09-07 10:44:15 +00:00
|
|
|
}
|
2017-09-09 04:24:04 +00:00
|
|
|
return list;
|
2017-09-07 10:44:15 +00:00
|
|
|
}
|
|
|
|
|
2017-09-09 04:24:04 +00:00
|
|
|
int TranslationManager::getLanguageByName(const QString &localeName) const
|
2017-09-07 10:44:15 +00:00
|
|
|
{
|
2017-09-09 04:24:04 +00:00
|
|
|
int index = 0;
|
|
|
|
foreach (const QLocale &locale, m_locales) {
|
|
|
|
if (localeName == locale.name()) {
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
++index;
|
2017-09-07 10:44:15 +00:00
|
|
|
}
|
2017-09-09 04:24:04 +00:00
|
|
|
return 0;
|
2017-09-07 10:44:15 +00:00
|
|
|
}
|
|
|
|
|
2017-09-09 04:24:04 +00:00
|
|
|
bool TranslationManager::switchLanguage(int language)
|
2017-09-07 10:44:15 +00:00
|
|
|
{
|
2017-09-09 04:24:04 +00:00
|
|
|
if (language < 0 || language >= m_locales.size())
|
|
|
|
return false;
|
2017-09-07 10:44:15 +00:00
|
|
|
|
2017-09-09 04:24:04 +00:00
|
|
|
m_locale = m_locales.at(language);
|
2017-09-07 10:44:15 +00:00
|
|
|
|
2017-09-09 04:24:04 +00:00
|
|
|
QLocale::setDefault(m_locale);
|
2017-09-07 10:44:15 +00:00
|
|
|
|
2017-09-09 04:24:04 +00:00
|
|
|
uninstallTranslator(m_language);
|
2017-09-07 10:44:15 +00:00
|
|
|
|
2017-09-09 04:24:04 +00:00
|
|
|
m_language = language;
|
|
|
|
emit languageChanged(m_language);
|
2017-09-07 10:44:15 +00:00
|
|
|
|
2017-09-09 04:24:04 +00:00
|
|
|
installTranslator(m_language, m_locale);
|
2017-09-07 10:44:15 +00:00
|
|
|
|
2017-09-09 04:24:04 +00:00
|
|
|
refreshTranslations();
|
2017-09-07 10:44:15 +00:00
|
|
|
|
2017-09-09 04:24:04 +00:00
|
|
|
return true;
|
2017-09-07 10:44:15 +00:00
|
|
|
}
|
|
|
|
|
2017-09-09 04:24:04 +00:00
|
|
|
bool TranslationManager::switchLanguageByName(const QString &localeName)
|
2017-09-07 10:44:15 +00:00
|
|
|
{
|
2017-09-09 04:24:04 +00:00
|
|
|
return switchLanguage(getLanguageByName(localeName));
|
2017-09-07 10:44:15 +00:00
|
|
|
}
|
|
|
|
|
2017-09-09 04:24:04 +00:00
|
|
|
void TranslationManager::uninstallAllTranslators()
|
2017-09-07 10:44:15 +00:00
|
|
|
{
|
2017-09-09 04:24:04 +00:00
|
|
|
const int translatorsCount = m_translators.size();
|
|
|
|
for (int i = 0; i < translatorsCount; ++i) {
|
|
|
|
uninstallTranslator(i);
|
|
|
|
}
|
2017-09-07 10:44:15 +00:00
|
|
|
}
|
|
|
|
|
2017-09-09 04:24:04 +00:00
|
|
|
void TranslationManager::uninstallTranslator(int language)
|
2017-09-07 10:44:15 +00:00
|
|
|
{
|
2017-09-09 04:24:04 +00:00
|
|
|
QTranslator *translator = m_translators.at(language);
|
|
|
|
if (translator) {
|
2019-03-10 18:03:59 +00:00
|
|
|
QCoreApplication::removeTranslator(translator);
|
2017-09-09 04:24:04 +00:00
|
|
|
}
|
2017-09-07 10:44:15 +00:00
|
|
|
}
|
|
|
|
|
2017-09-09 04:24:04 +00:00
|
|
|
void TranslationManager::installTranslator(int language, const QLocale &locale)
|
2017-09-07 10:44:15 +00:00
|
|
|
{
|
2017-09-09 04:24:04 +00:00
|
|
|
QTranslator *translator = m_translators.at(language);
|
|
|
|
if (!translator) {
|
|
|
|
translator = loadTranslator(language, locale);
|
|
|
|
}
|
|
|
|
if (translator) {
|
2019-03-10 18:03:59 +00:00
|
|
|
QCoreApplication::installTranslator(translator);
|
2017-09-09 04:24:04 +00:00
|
|
|
}
|
2017-09-07 10:44:15 +00:00
|
|
|
}
|
|
|
|
|
2017-09-09 04:24:04 +00:00
|
|
|
QTranslator *TranslationManager::loadTranslator(int language, const QLocale &locale)
|
2017-09-07 10:44:15 +00:00
|
|
|
{
|
2017-09-09 04:26:37 +00:00
|
|
|
if (!language)
|
|
|
|
return nullptr;
|
|
|
|
|
2017-09-09 04:24:04 +00:00
|
|
|
// Load .qm file
|
|
|
|
QTranslator *translator = new QTranslator(this);
|
|
|
|
translator->load(TRANSLATION_FILE_PREFIX + locale.name(), i18nDir());
|
|
|
|
|
|
|
|
m_translators.replace(language, translator);
|
|
|
|
|
|
|
|
return translator;
|
2017-09-07 10:44:15 +00:00
|
|
|
}
|
|
|
|
|
2017-09-09 04:24:04 +00:00
|
|
|
void TranslationManager::refreshTranslations()
|
2017-09-07 10:44:15 +00:00
|
|
|
{
|
2018-03-07 10:14:22 +00:00
|
|
|
emit trTriggerChanged();
|
2017-09-07 10:44:15 +00:00
|
|
|
}
|
|
|
|
|
2017-09-09 04:24:04 +00:00
|
|
|
QString TranslationManager::i18nDir()
|
2017-09-07 10:44:15 +00:00
|
|
|
{
|
2019-03-10 18:03:59 +00:00
|
|
|
return FileUtil::appBinLocation() + "/i18n";
|
2017-09-07 10:44:15 +00:00
|
|
|
}
|