QuasarAppLib/locales.cpp

51 lines
1.4 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>
2020-05-15 01:43:32 +03:00
#include "params.h"
2018-09-29 15:56:04 +03:00
using namespace QuasarAppUtils;
2020-05-16 15:46:41 +03:00
bool Locales::setLocale(const QLocale &locale, const QString& file, const QString& delimiter, const QString& location) {
auto obj = instance();
return obj->translate(locale, file, delimiter, location);
2020-05-15 01:43:32 +03:00
}
2020-05-16 15:46:41 +03:00
bool Locales::translate(const QLocale &locale, const QString &file, const QString &delimiter, const QString &location) {
2020-05-15 01:43:32 +03:00
auto translator = getTranslator();
2020-05-16 15:46:41 +03:00
if(translator->load(locale, file, delimiter, location) && QCoreApplication::installTranslator(translator)) {
emit sigTranslationChanged();
2020-05-15 01:43:32 +03:00
return true;
2018-09-29 15:56:04 +03:00
}
2020-05-16 15:46:41 +03:00
QLocale defaultLocale = QLocale::system();
if(translator->load(defaultLocale, file, delimiter, location) && QCoreApplication::installTranslator(translator)) {
emit sigTranslationChanged();
2020-05-15 01:43:32 +03:00
return true;
2018-09-29 15:56:04 +03:00
}
2020-05-15 01:43:32 +03:00
Params::log("set translations fail!", VerboseLvl::Warning);
return false;
}
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
}