QuasarAppLib/locales.cpp

68 lines
1.5 KiB
C++
Raw Normal View History

2018-09-29 15:56:04 +03:00
/*
2021-01-05 13:04:05 +03:00
* Copyright (C) 2018-2021 QuasarApp.
2018-09-29 15:56:04 +03:00
* Distributed under the lgplv3 software license, see the accompanying
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
#include "locales.h"
#include <QCoreApplication>
#include <QTranslator>
#include <QLocale>
2021-02-12 12:39:46 +03:00
#include <QLibraryInfo>
#include <QDir>
#include <QRegularExpression>
2020-05-15 01:43:32 +03:00
#include "params.h"
2018-09-29 15:56:04 +03:00
using namespace QuasarAppUtils;
2021-02-12 12:39:46 +03:00
bool Locales::setLocalePrivate(const QLocale &locale) {
auto translator = getTranslator();
const auto qmFiles = QDir(_location).entryInfoList({"*" + locale.bcp47Name() + "*.qm"}, QDir::Files);
for (const auto & file: qmFiles) {
if(!translator->load(file.absoluteFilePath())) {
return false;
}
}
emit sigTranslationChanged();
return true;
}
bool Locales::setLocale(const QLocale &locale) {
2020-05-16 15:46:41 +03:00
auto obj = instance();
2021-02-12 12:39:46 +03:00
return obj->setLocalePrivate(locale);
2020-05-15 01:43:32 +03:00
}
2021-02-12 12:39:46 +03:00
bool Locales::translate(const QLocale &locale, QString location) {
2020-05-15 01:43:32 +03:00
auto translator = getTranslator();
2021-02-12 12:39:46 +03:00
if (location.isEmpty())
_location = QLibraryInfo::path(QLibraryInfo::TranslationsPath);
else
_location = location;
2018-09-29 15:56:04 +03:00
2021-02-12 12:39:46 +03:00
if (!setLocale(locale)) {
return false;
2018-09-29 15:56:04 +03:00
}
2021-02-12 12:39:46 +03:00
QCoreApplication::installTranslator(translator);
2020-05-15 01:43:32 +03:00
2021-02-12 12:39:46 +03:00
return true;
2020-05-15 01:43:32 +03:00
}
2020-05-16 15:46:41 +03:00
Locales *Locales::instance() {
static auto instance = new Locales();
return instance;
}
2020-05-15 01:43:32 +03:00
QTranslator *Locales::getTranslator() {
static QTranslator *translator = new QTranslator();
return translator;
2018-09-29 15:56:04 +03:00
}