mirror of
https://github.com/QuasarApp/QuasarAppLib.git
synced 2025-04-26 09:44:38 +00:00
v 1.4.4 "fix translations"
This commit is contained in:
parent
e520e9da41
commit
b5316127aa
@ -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)
|
||||
|
@ -60,5 +60,5 @@ RESOURCES += \
|
||||
|
||||
include(Etalons/qmake/ccache.pri)
|
||||
|
||||
VERSION = 1.4.3
|
||||
VERSION = 1.4.4
|
||||
|
||||
|
47
locales.cpp
47
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() {
|
||||
|
22
locales.h
22
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;
|
||||
|
||||
};
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user