mirror of
https://github.com/QuasarApp/QuasarAppLib.git
synced 2025-05-05 05:59:42 +00:00
new translation logick
This commit is contained in:
parent
60f961375a
commit
ec0622fa12
29
locales.cpp
29
locales.cpp
@ -11,22 +11,39 @@
|
|||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
#include <QTranslator>
|
#include <QTranslator>
|
||||||
#include <QLocale>
|
#include <QLocale>
|
||||||
|
#include "params.h"
|
||||||
|
|
||||||
using namespace QuasarAppUtils;
|
using namespace QuasarAppUtils;
|
||||||
|
|
||||||
|
|
||||||
bool Locales::initLocale(const QString& prefix, const QString &locale, QCoreApplication *app, QTranslator *translator){
|
bool Locales::initLocale(QTranslator* translator) {
|
||||||
|
return QCoreApplication::installTranslator(translator);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Locales::setLocale(const QString &prefix, const QString &locale) {
|
||||||
|
auto translator = getTranslator();
|
||||||
|
|
||||||
|
if (translator->isEmpty() && !initLocale(translator)) {
|
||||||
|
Params::log("init translations fail! ", VerboseLvl::Error);
|
||||||
|
}
|
||||||
|
|
||||||
QString defaultLocale = QLocale::system().name();
|
QString defaultLocale = QLocale::system().name();
|
||||||
defaultLocale.truncate(defaultLocale.lastIndexOf('_'));
|
defaultLocale.truncate(defaultLocale.lastIndexOf('_'));
|
||||||
|
|
||||||
if(!locale.isEmpty() && translator->load(QString("%0/%1").arg(prefix, locale))) {
|
if(translator->load(QString("%0/%1").arg(prefix, locale))) {
|
||||||
return app->installTranslator(translator);
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!translator->load(QString("%0/%1").arg(prefix, defaultLocale))) {
|
if(translator->load(QString("%0/%1").arg(prefix, defaultLocale))) {
|
||||||
return false;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return app->installTranslator(translator);
|
Params::log("set translations fail!", VerboseLvl::Warning);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
QTranslator *Locales::getTranslator() {
|
||||||
|
static QTranslator *translator = new QTranslator();
|
||||||
|
return translator;
|
||||||
}
|
}
|
||||||
|
23
locales.h
23
locales.h
@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
#include "quasarapp_global.h"
|
#include "quasarapp_global.h"
|
||||||
|
|
||||||
|
#include <QString>
|
||||||
|
|
||||||
class QCoreApplication;
|
class QCoreApplication;
|
||||||
class QTranslator;
|
class QTranslator;
|
||||||
|
|
||||||
@ -27,12 +29,23 @@ public:
|
|||||||
/**
|
/**
|
||||||
* @brief initLocale init translation of applictaion
|
* @brief initLocale init translation of applictaion
|
||||||
* @param prefix - path to folder with qm files. example (/home)
|
* @param prefix - path to folder with qm files. example (/home)
|
||||||
* @param locale - string value of locale. example (en)
|
* @param locale - string value of locale. example (en) by default empty. @note If use by default this function set sstem language.
|
||||||
* @param app - app core of qt
|
* @return return true if locale set for application
|
||||||
* @param translator - translator core of qt
|
|
||||||
* @return return true if locale funded
|
|
||||||
*/
|
*/
|
||||||
static bool initLocale(const QString& prefix ,const QString &locale, QCoreApplication* app, QTranslator *translator);
|
static bool setLocale(const QString& prefix , const QString &locale = {});
|
||||||
|
|
||||||
|
private:
|
||||||
|
/**
|
||||||
|
* @brief initLocale
|
||||||
|
* @return true if function finished seccusseful
|
||||||
|
*/
|
||||||
|
static bool initLocale(QTranslator*);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief getTranslator
|
||||||
|
* @return instance of Translation
|
||||||
|
*/
|
||||||
|
static QTranslator* getTranslator();
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user