mirror of
https://github.com/QuasarApp/QuasarAppLib.git
synced 2025-04-27 02:04:39 +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;
|
||||
|
||||
|
||||
bool Locales::initLocale(QTranslator* translator) {
|
||||
return QCoreApplication::installTranslator(translator);
|
||||
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);
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
if (translator->isEmpty() && !initLocale(translator)) {
|
||||
Params::log("init translations fail! ", VerboseLvl::Error);
|
||||
}
|
||||
|
||||
QString defaultLocale = QLocale::system().name();
|
||||
defaultLocale.truncate(defaultLocale.lastIndexOf('_'));
|
||||
|
||||
if(translator->load(QString("%0/%1").arg(prefix, locale))) {
|
||||
if(translator->load(locale, file, delimiter, location) && QCoreApplication::installTranslator(translator)) {
|
||||
emit sigTranslationChanged();
|
||||
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;
|
||||
}
|
||||
|
||||
@ -43,6 +39,11 @@ bool Locales::setLocale(const QString &prefix, const QString &locale) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Locales *Locales::instance() {
|
||||
static auto instance = new Locales();
|
||||
return instance;
|
||||
}
|
||||
|
||||
QTranslator *Locales::getTranslator() {
|
||||
static QTranslator *translator = new QTranslator();
|
||||
return translator;
|
||||
|
44
locales.h
44
locales.h
@ -11,6 +11,7 @@
|
||||
|
||||
#include "quasarapp_global.h"
|
||||
|
||||
#include <QLocale>
|
||||
#include <QString>
|
||||
|
||||
class QCoreApplication;
|
||||
@ -21,25 +22,46 @@ namespace QuasarAppUtils {
|
||||
/**
|
||||
* @brief The Locales class for parese local files
|
||||
*/
|
||||
class QUASARAPPSHARED_EXPORT Locales
|
||||
class QUASARAPPSHARED_EXPORT Locales : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
Locales() = delete;
|
||||
|
||||
/**
|
||||
* @brief initLocale init translation of applictaion
|
||||
* @param prefix - path to folder with qm files. example (/home)
|
||||
* @param locale - string value of locale. example (en) by default empty. @note If use by default this function set sstem language.
|
||||
* @brief setLocale set 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
|
||||
*/
|
||||
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:
|
||||
/**
|
||||
* @brief initLocale
|
||||
* @return true if function finished seccusseful
|
||||
*/
|
||||
static bool initLocale(QTranslator*);
|
||||
Locales() = default;
|
||||
|
||||
/**
|
||||
* @brief getTranslator
|
||||
|
Loading…
x
Reference in New Issue
Block a user