mirror of
https://github.com/QuasarApp/QuasarAppLib.git
synced 2025-05-01 20:19:41 +00:00
fix locales
This commit is contained in:
parent
ec0622fa12
commit
537d7b0634
2
CMake
2
CMake
@ -1 +1 @@
|
|||||||
Subproject commit 0c7a0ac156d9ea8e43eac7829d25e977801a219e
|
Subproject commit 9da9019155af44887c9ba11c176cac47fdd020ee
|
27
locales.cpp
27
locales.cpp
@ -15,26 +15,22 @@
|
|||||||
|
|
||||||
using namespace QuasarAppUtils;
|
using namespace QuasarAppUtils;
|
||||||
|
|
||||||
|
bool Locales::setLocale(const QLocale &locale, const QString& file, const QString& delimiter, const QString& location) {
|
||||||
bool Locales::initLocale(QTranslator* translator) {
|
auto obj = instance();
|
||||||
return QCoreApplication::installTranslator(translator);
|
return obj->translate(locale, file, delimiter, location);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Locales::setLocale(const QString &prefix, const QString &locale) {
|
bool Locales::translate(const QLocale &locale, const QString &file, const QString &delimiter, const QString &location) {
|
||||||
auto translator = getTranslator();
|
auto translator = getTranslator();
|
||||||
|
|
||||||
if (translator->isEmpty() && !initLocale(translator)) {
|
if(translator->load(locale, file, delimiter, location) && QCoreApplication::installTranslator(translator)) {
|
||||||
Params::log("init translations fail! ", VerboseLvl::Error);
|
emit sigTranslationChanged();
|
||||||
}
|
|
||||||
|
|
||||||
QString defaultLocale = QLocale::system().name();
|
|
||||||
defaultLocale.truncate(defaultLocale.lastIndexOf('_'));
|
|
||||||
|
|
||||||
if(translator->load(QString("%0/%1").arg(prefix, locale))) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(translator->load(QString("%0/%1").arg(prefix, defaultLocale))) {
|
QLocale defaultLocale = QLocale::system();
|
||||||
|
if(translator->load(defaultLocale, file, delimiter, location) && QCoreApplication::installTranslator(translator)) {
|
||||||
|
emit sigTranslationChanged();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,6 +39,11 @@ bool Locales::setLocale(const QString &prefix, const QString &locale) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Locales *Locales::instance() {
|
||||||
|
static auto instance = new Locales();
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
QTranslator *Locales::getTranslator() {
|
QTranslator *Locales::getTranslator() {
|
||||||
static QTranslator *translator = new QTranslator();
|
static QTranslator *translator = new QTranslator();
|
||||||
return translator;
|
return translator;
|
||||||
|
44
locales.h
44
locales.h
@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
#include "quasarapp_global.h"
|
#include "quasarapp_global.h"
|
||||||
|
|
||||||
|
#include <QLocale>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
|
||||||
class QCoreApplication;
|
class QCoreApplication;
|
||||||
@ -21,25 +22,46 @@ namespace QuasarAppUtils {
|
|||||||
/**
|
/**
|
||||||
* @brief The Locales class for parese local files
|
* @brief The Locales class for parese local files
|
||||||
*/
|
*/
|
||||||
class QUASARAPPSHARED_EXPORT Locales
|
class QUASARAPPSHARED_EXPORT Locales : public QObject
|
||||||
{
|
{
|
||||||
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
Locales() = delete;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief initLocale init translation of applictaion
|
* @brief setLocale set translation of applictaion
|
||||||
* @param prefix - path to folder with qm files. example (/home)
|
* @param locale - see info about QLocale
|
||||||
* @param locale - string value of locale. example (en) by default empty. @note If use by default this function set sstem language.
|
* @param location - path to folder with qm files. example (:/tr)
|
||||||
|
* @param file - prefix for translations.
|
||||||
* @return return true if locale set for application
|
* @return return true if locale set for application
|
||||||
*/
|
*/
|
||||||
static bool setLocale(const QString& prefix , const QString &locale = {});
|
static bool setLocale(const QLocale &locale = {}, const QString& file = {}, const QString& delimiter = "_", const QString& location = "");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief translate init translation of applictaion
|
||||||
|
* @param locale - see info about QLocale
|
||||||
|
* @param location - path to folder with qm files. example (:/tr)
|
||||||
|
* @param file - prefix for translations.
|
||||||
|
* @return return true if locale set for application
|
||||||
|
*/
|
||||||
|
bool translate(const QLocale &locale = {},
|
||||||
|
const QString& file = {},
|
||||||
|
const QString& delimiter = "_",
|
||||||
|
const QString& location = "");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief instance
|
||||||
|
* @return return static object
|
||||||
|
*/
|
||||||
|
static Locales *instance();
|
||||||
|
|
||||||
|
signals:
|
||||||
|
/**
|
||||||
|
* @brief sigTranslationChanged - emited when set new locale for application.
|
||||||
|
*/
|
||||||
|
void sigTranslationChanged();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/**
|
Locales() = default;
|
||||||
* @brief initLocale
|
|
||||||
* @return true if function finished seccusseful
|
|
||||||
*/
|
|
||||||
static bool initLocale(QTranslator*);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief getTranslator
|
* @brief getTranslator
|
||||||
|
Loading…
x
Reference in New Issue
Block a user