2018-09-29 15:56:04 +03:00
|
|
|
/*
|
2024-12-30 22:39:49 +01:00
|
|
|
* Copyright (C) 2018-2025 QuasarApp.
|
2018-09-29 15:56:04 +03:00
|
|
|
* Distributed under the lgplv3 software license, see the accompanying
|
|
|
|
* Everyone is permitted to copy and distribute verbatim copies
|
|
|
|
* of this license document, but changing it is not allowed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef LOCALES_H
|
|
|
|
#define LOCALES_H
|
|
|
|
|
|
|
|
#include "quasarapp_global.h"
|
|
|
|
|
2020-05-16 15:46:41 +03:00
|
|
|
#include <QLocale>
|
2021-02-20 12:42:34 +03:00
|
|
|
#include <QSet>
|
2020-05-15 01:43:32 +03:00
|
|
|
#include <QString>
|
2022-04-26 20:55:50 +03:00
|
|
|
#include <QDir>
|
2020-05-15 01:43:32 +03:00
|
|
|
|
2018-09-29 15:56:04 +03:00
|
|
|
class QCoreApplication;
|
|
|
|
class QTranslator;
|
|
|
|
|
|
|
|
namespace QuasarAppUtils {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The Locales class for parese local files
|
2021-04-26 12:01:08 +03:00
|
|
|
* **Example :**
|
2021-02-12 13:28:47 +03:00
|
|
|
* @code{cpp}
|
2021-04-26 11:27:19 +03:00
|
|
|
* QuasarAppUtils::Locales::init();
|
|
|
|
* @endcode
|
|
|
|
*
|
|
|
|
* @note If you want to add you own location of the qm files then add this into seccond arguments of the Locales::init method.
|
|
|
|
*
|
|
|
|
* @code{cpp}
|
|
|
|
* QuasarAppUtils::Locales::init(QLocale::system(), "myPath");
|
2021-02-12 13:28:47 +03:00
|
|
|
* @endcode
|
2022-05-29 14:03:56 +03:00
|
|
|
*
|
|
|
|
* @note All translations qm files should be named with lower case example : en.qm
|
2018-09-29 15:56:04 +03:00
|
|
|
*/
|
2020-05-16 15:46:41 +03:00
|
|
|
class QUASARAPPSHARED_EXPORT Locales : public QObject
|
2018-09-29 15:56:04 +03:00
|
|
|
{
|
2020-05-16 15:46:41 +03:00
|
|
|
Q_OBJECT
|
2018-09-29 15:56:04 +03:00
|
|
|
public:
|
|
|
|
|
|
|
|
/**
|
2021-02-12 12:39:46 +03:00
|
|
|
* @brief setLocale This method sets locale for application and loaded all translations for this locale.
|
|
|
|
* @param locale This is new locale.
|
|
|
|
* @return true if the all ltranstations files loaded successful.
|
2020-05-15 01:43:32 +03:00
|
|
|
*/
|
2023-10-01 11:27:35 +02:00
|
|
|
static bool setLocale(const QLocale &locale, bool force = false);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief init This method initialize translations of applictaion.
|
|
|
|
* @param locales This is list of locales that you want to locad to application cache. See info about QLocale.
|
|
|
|
* @param location Path to folder with qm files. example (:/tr).
|
|
|
|
* @return return true if locale set for application.
|
|
|
|
*/
|
|
|
|
static bool init(const QList<QLocale> & locales,
|
|
|
|
const QSet<QString> & location = {});
|
2020-05-16 15:46:41 +03:00
|
|
|
|
|
|
|
/**
|
2021-04-07 13:24:30 +03:00
|
|
|
* @brief init This method initialize translation of applictaion.
|
|
|
|
* @param locale See info about QLocale.
|
|
|
|
* @param location Path to folder with qm files. example (:/tr).
|
|
|
|
* @return return true if locale set for application.
|
2020-05-16 15:46:41 +03:00
|
|
|
*/
|
2022-06-21 19:14:59 +03:00
|
|
|
static bool init(const QLocale &locale = QLocale::system(),
|
|
|
|
const QSet<QString> & location = {});
|
2021-05-05 15:44:21 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief addLocation This method add location for qm files. Use This method if you create a own library with translations supports.
|
|
|
|
* @param location This is a new location of the qm files.
|
|
|
|
*/
|
2022-06-21 19:14:59 +03:00
|
|
|
static void addLocation(const QString& location);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief instance This method return pointer to the Locales service.
|
|
|
|
* @return return pointer to the Locales static object
|
|
|
|
*/
|
|
|
|
static Locales *instance();
|
2020-05-16 15:46:41 +03:00
|
|
|
|
2022-02-21 14:45:00 +03:00
|
|
|
/**
|
|
|
|
* @brief currentLocate This method return current locate of applicatuon.
|
|
|
|
* @return current or last sets locate of applciation.
|
|
|
|
*/
|
2022-06-21 19:14:59 +03:00
|
|
|
static const QLocale ¤tLocate();
|
2022-02-21 14:45:00 +03:00
|
|
|
|
2023-10-01 11:27:35 +02:00
|
|
|
/**
|
|
|
|
* @brief tr This method will translate single string to choosed language.
|
|
|
|
* @param source This is source string of translations.
|
|
|
|
* @param locale This is choosed language.
|
|
|
|
* @return translated string value.
|
|
|
|
* @note use instant QOBject::tr, This method must be read by the lupdate utility
|
|
|
|
*/
|
|
|
|
static QString tr(const char *source, const QLocale& locale);
|
|
|
|
|
2020-05-16 15:46:41 +03:00
|
|
|
signals:
|
|
|
|
/**
|
2021-04-07 13:24:30 +03:00
|
|
|
* @brief sigTranslationChanged Emited when set new locale for application.
|
2020-05-16 15:46:41 +03:00
|
|
|
*/
|
|
|
|
void sigTranslationChanged();
|
|
|
|
|
|
|
|
private:
|
|
|
|
Locales() = default;
|
2021-02-15 13:17:28 +03:00
|
|
|
~Locales();
|
|
|
|
|
2023-10-01 11:27:35 +02:00
|
|
|
bool setLocalePrivate(const QLocale &locale = QLocale::system(), bool force = false, bool install = true);
|
2021-02-12 13:28:47 +03:00
|
|
|
bool initPrivate(const QLocale &locale = QLocale::system(),
|
2021-02-15 13:17:28 +03:00
|
|
|
const QSet<QString> &location = {});
|
2023-10-01 11:27:35 +02:00
|
|
|
|
|
|
|
bool initPrivate(const QList<QLocale> &locales,
|
|
|
|
const QSet<QString> &location = {});
|
|
|
|
|
|
|
|
void clearCache(const QLocale& locale);
|
|
|
|
void clearCache();
|
|
|
|
|
|
|
|
void removeOldTranslation(const QLocale& locale);
|
|
|
|
|
2021-05-05 15:44:21 +03:00
|
|
|
void addLocationPrivate(const QString& location);
|
2020-05-15 01:43:32 +03:00
|
|
|
|
2022-02-21 14:45:00 +03:00
|
|
|
const QLocale ¤tLocatePrivate() const;
|
|
|
|
|
2023-04-15 21:35:09 +02:00
|
|
|
bool findQm(QString localePrefix,
|
2022-12-25 20:38:08 +03:00
|
|
|
QList<QTranslator *> &result);
|
|
|
|
bool findQmPrivate(const QString &prefix,
|
|
|
|
QList<QTranslator *> &qmFiles);
|
2023-10-01 11:27:35 +02:00
|
|
|
void installTranslations(QList<QTranslator *> &qmFiles);
|
|
|
|
QString translatePrivate(const char *source, const QLocale& locale);
|
|
|
|
|
2022-04-26 20:55:50 +03:00
|
|
|
|
2022-02-21 14:45:00 +03:00
|
|
|
QLocale _currentLocate;
|
2021-02-15 13:17:28 +03:00
|
|
|
QSet<QString> _locations;
|
2023-10-01 11:27:35 +02:00
|
|
|
QHash<QLocale, QList<QTranslator *>> _translations;
|
2018-09-29 15:56:04 +03:00
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif // LOCALES_H
|