From b5316127aab1a81942f68ed904a416b9f25fda32 Mon Sep 17 00:00:00 2001
From: EndrII <EndrIIMail@gmail.com>
Date: Fri, 12 Feb 2021 12:39:46 +0300
Subject: [PATCH] v 1.4.4 "fix translations"

---
 CMakeLists.txt |  2 +-
 QuasarApp.pro  |  2 +-
 locales.cpp    | 47 ++++++++++++++++++++++++++++++++---------------
 locales.h      | 22 +++++++++++-----------
 4 files changed, 45 insertions(+), 28 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index b877fc7..f2b346d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -45,4 +45,4 @@ add_library(${PROJECT_NAME} ${SOURCE_CPP})
 target_link_libraries(${PROJECT_NAME} PRIVATE Qt::Core)
 target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
 
-setVersion(1 4 3)
+setVersion(1 4 4)
diff --git a/QuasarApp.pro b/QuasarApp.pro
index b11e0fc..8bb1625 100644
--- a/QuasarApp.pro
+++ b/QuasarApp.pro
@@ -60,5 +60,5 @@ RESOURCES += \
 
 include(Etalons/qmake/ccache.pri)
 
-VERSION = 1.4.3
+VERSION = 1.4.4
 
diff --git a/locales.cpp b/locales.cpp
index 79b6a56..ed4db53 100644
--- a/locales.cpp
+++ b/locales.cpp
@@ -11,32 +11,49 @@
 #include <QCoreApplication>
 #include <QTranslator>
 #include <QLocale>
+#include <QLibraryInfo>
+#include <QDir>
+#include <QRegularExpression>
 #include "params.h"
 
 using namespace QuasarAppUtils;
 
-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::translate(const QLocale &locale, const QString &file, const QString &delimiter, const QString &location) {
+bool Locales::setLocalePrivate(const QLocale &locale) {
     auto translator = getTranslator();
 
-    if(translator->load(locale, file, delimiter, location) && QCoreApplication::installTranslator(translator)) {
-        emit sigTranslationChanged();
-        return true;
+    const auto qmFiles = QDir(_location).entryInfoList({"*" + locale.bcp47Name() + "*.qm"}, QDir::Files);
+
+    for (const auto & file: qmFiles) {
+        if(!translator->load(file.absoluteFilePath())) {
+            return false;
+        }
     }
 
-    QLocale defaultLocale = QLocale::system();
-    if(translator->load(defaultLocale, file, delimiter, location) && QCoreApplication::installTranslator(translator)) {
-        emit sigTranslationChanged();
-        return true;
+    emit sigTranslationChanged();
+
+    return true;
+}
+
+bool Locales::setLocale(const QLocale &locale) {
+    auto obj = instance();
+    return obj->setLocalePrivate(locale);
+}
+
+bool Locales::translate(const QLocale &locale, QString location) {
+    auto translator = getTranslator();
+
+    if (location.isEmpty())
+        _location = QLibraryInfo::path(QLibraryInfo::TranslationsPath);
+    else
+        _location = location;
+
+    if (!setLocale(locale)) {
+        return false;
     }
 
-    Params::log("set translations fail!", VerboseLvl::Warning);
+    QCoreApplication::installTranslator(translator);
 
-    return false;
+    return true;
 }
 
 Locales *Locales::instance() {
diff --git a/locales.h b/locales.h
index a834529..e471eb7 100644
--- a/locales.h
+++ b/locales.h
@@ -28,25 +28,22 @@ class QUASARAPPSHARED_EXPORT Locales : public QObject
 public:
 
     /**
-     * @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
+     * @brief setLocale This method sets locale for application and loaded all translations for this locale.
+     * @param locale This is new locale.
+     * @param location This is localtion of the qm file. if you want to load files from resource set patht to resources files
+     *  Example :(:/tr/)
+     * @return true if the all ltranstations files loaded successful.
      */
-    static bool setLocale(const QLocale &locale = {}, const QString& file = {}, const QString& delimiter = "_", const QString& location = "");
+    static bool setLocale(const QLocale &locale);
 
     /**
      * @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 = "");
+    bool translate(const QLocale &locale = QLocale::system(),
+                   QString location = "");
 
     /**
      * @brief instance
@@ -62,12 +59,15 @@ signals:
 
 private:
     Locales() = default;
+    bool setLocalePrivate(const QLocale &locale = QLocale::system());
+
 
     /**
      * @brief getTranslator
      * @return instance of Translation
      */
     static QTranslator* getTranslator();
+    QString _location;
 
 };
 }