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 #include #include +#include +#include +#include #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; }; }