From 537d7b0634aeeafee095331284fc00abc2ee6abd Mon Sep 17 00:00:00 2001 From: EndrII <endriimail@gmail.com> Date: Sat, 16 May 2020 15:46:41 +0300 Subject: [PATCH] fix locales --- CMake | 2 +- locales.cpp | 27 ++++++++++++++------------- locales.h | 44 +++++++++++++++++++++++++++++++++----------- 3 files changed, 48 insertions(+), 25 deletions(-) diff --git a/CMake b/CMake index 0c7a0ac..9da9019 160000 --- a/CMake +++ b/CMake @@ -1 +1 @@ -Subproject commit 0c7a0ac156d9ea8e43eac7829d25e977801a219e +Subproject commit 9da9019155af44887c9ba11c176cac47fdd020ee diff --git a/locales.cpp b/locales.cpp index 7d64c1c..f6cc55f 100644 --- a/locales.cpp +++ b/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; diff --git a/locales.h b/locales.h index 1826617..b8d00cf 100644 --- a/locales.h +++ b/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