2020-08-25 16:05:56 +02:00
|
|
|
/* === This file is part of Calamares - <https://calamares.io> ===
|
2020-08-10 18:21:42 +02:00
|
|
|
*
|
2020-05-30 16:15:03 +02:00
|
|
|
* SPDX-FileCopyrightText: 2014-2015 Teo Mrnjavac <teo@kde.org>
|
2020-08-10 18:21:42 +02:00
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2014-11-11 12:42:37 +01:00
|
|
|
*
|
2020-08-25 16:05:56 +02:00
|
|
|
* Calamares is Free Software: see the License-Identifier above.
|
2014-11-11 12:42:37 +01:00
|
|
|
*
|
2020-05-30 16:15:03 +02:00
|
|
|
*
|
2014-11-11 12:42:37 +01:00
|
|
|
*/
|
|
|
|
|
2019-04-29 12:12:18 +02:00
|
|
|
#ifndef UTILS_RETRANSLATOR_H
|
|
|
|
#define UTILS_RETRANSLATOR_H
|
2014-11-11 12:42:37 +01:00
|
|
|
|
2019-04-29 12:33:55 +02:00
|
|
|
#include "DllMacro.h"
|
|
|
|
|
2014-11-11 15:12:17 +01:00
|
|
|
#include <QList>
|
2014-11-11 12:42:37 +01:00
|
|
|
#include <QObject>
|
2019-04-29 12:33:55 +02:00
|
|
|
#include <QString>
|
2014-11-11 12:42:37 +01:00
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
class QEvent;
|
2019-04-29 12:33:55 +02:00
|
|
|
class QLocale;
|
2020-10-30 13:58:17 +01:00
|
|
|
class QTranslator;
|
2014-11-11 12:42:37 +01:00
|
|
|
|
|
|
|
namespace CalamaresUtils
|
|
|
|
{
|
2019-08-04 21:22:54 +02:00
|
|
|
/**
|
|
|
|
* @brief installTranslator changes the application language.
|
|
|
|
* @param locale the new locale.
|
|
|
|
* @param brandingTranslationsPrefix the branding path prefix, from Calamares::Branding.
|
|
|
|
*/
|
2020-03-25 12:08:27 +01:00
|
|
|
DLLEXPORT void installTranslator( const QLocale& locale, const QString& brandingTranslationsPrefix );
|
2019-08-04 21:22:54 +02:00
|
|
|
|
2020-10-30 13:58:17 +01:00
|
|
|
/** @brief The name of the (locale of the) most recently installed translator
|
|
|
|
*
|
|
|
|
* May return something different from the locale.name() of the
|
|
|
|
* QLocale passed in, because Calamares will munge some names and
|
|
|
|
* may remap translations.
|
|
|
|
*/
|
2019-08-04 21:22:54 +02:00
|
|
|
DLLEXPORT QString translatorLocaleName();
|
2014-11-11 12:42:37 +01:00
|
|
|
|
2020-10-30 13:58:17 +01:00
|
|
|
/** @brief Loads <prefix><locale> translations into the given @p translator
|
|
|
|
*
|
|
|
|
* This function is not intended for general use: it is for those special
|
|
|
|
* cases where modules need their own translator / translations for data
|
|
|
|
* that is locale to the module. Tries to load a .qm from "sensible"
|
|
|
|
* locations, which are the same ones that installTranslator() would use.
|
|
|
|
* Takes local-translations into account.
|
|
|
|
*
|
|
|
|
* Note that @p prefix should end with an underscore '_' -- this function
|
|
|
|
* does not introduce one by itself.
|
|
|
|
*
|
|
|
|
* @returns @c true on success
|
|
|
|
*/
|
|
|
|
DLLEXPORT bool loadTranslator( const QLocale& locale, const QString& prefix, QTranslator* translator );
|
|
|
|
|
2020-02-05 17:48:39 +01:00
|
|
|
/** @brief Set @p allow to true to load translations from current dir.
|
|
|
|
*
|
|
|
|
* If false, (or never called) the translations are loaded only from
|
|
|
|
* system locations (the AppData dir) and from QRC (compiled in).
|
|
|
|
* Enable local translations to test translations stored in the
|
|
|
|
* current directory.
|
|
|
|
*/
|
|
|
|
DLLEXPORT void setAllowLocalTranslation( bool allow );
|
|
|
|
|
2014-11-11 12:42:37 +01:00
|
|
|
class Retranslator : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
2019-08-29 14:53:03 +02:00
|
|
|
/// @brief Call @p retranslateFunc when the language changes
|
2019-08-04 21:22:54 +02:00
|
|
|
static void attachRetranslator( QObject* parent, std::function< void( void ) > retranslateFunc );
|
2019-08-29 14:53:03 +02:00
|
|
|
/// @brief What retranslator belongs to @p parent (may create one)
|
|
|
|
static Retranslator* retranslatorFor( QObject* parent );
|
2014-11-11 15:12:17 +01:00
|
|
|
|
2019-08-29 14:53:03 +02:00
|
|
|
signals:
|
|
|
|
void languageChange();
|
|
|
|
|
2014-11-11 12:42:37 +01:00
|
|
|
protected:
|
|
|
|
bool eventFilter( QObject* obj, QEvent* e ) override;
|
|
|
|
|
|
|
|
private:
|
2014-11-11 15:12:17 +01:00
|
|
|
explicit Retranslator( QObject* parent );
|
|
|
|
|
|
|
|
QList< std::function< void( void ) > > m_retranslateFuncList;
|
2014-11-11 12:42:37 +01:00
|
|
|
};
|
|
|
|
|
2014-11-11 15:11:06 +01:00
|
|
|
|
2019-08-04 21:22:54 +02:00
|
|
|
} // namespace CalamaresUtils
|
2014-11-11 12:42:37 +01:00
|
|
|
|
2021-03-16 14:55:26 +01:00
|
|
|
#define CALAMARES_RETRANSLATE( body ) CalamaresUtils::Retranslator::attachRetranslator( this, [=] { body } )
|
2019-08-04 21:22:54 +02:00
|
|
|
#define CALAMARES_RETRANSLATE_WIDGET( widget, body ) \
|
2021-03-16 14:55:26 +01:00
|
|
|
CalamaresUtils::Retranslator::attachRetranslator( widget, [=] { body } )
|
2019-08-29 14:53:03 +02:00
|
|
|
#define CALAMARES_RETRANSLATE_SLOT( slotfunc ) \
|
2021-03-16 14:55:26 +01:00
|
|
|
do \
|
2019-08-29 14:53:03 +02:00
|
|
|
{ \
|
|
|
|
this->connect( CalamaresUtils::Retranslator::retranslatorFor( this ), \
|
|
|
|
&CalamaresUtils::Retranslator::languageChange, \
|
|
|
|
this, \
|
|
|
|
slotfunc ); \
|
2021-03-16 14:55:26 +01:00
|
|
|
} while ( 0 )
|
2014-11-11 15:11:06 +01:00
|
|
|
|
2019-04-29 12:12:18 +02:00
|
|
|
#endif
|