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"
|
2021-07-26 13:14:30 +02:00
|
|
|
#include "locale/Translation.h"
|
2019-04-29 12:33:55 +02:00
|
|
|
|
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
|
|
|
|
{
|
2021-07-26 13:14:30 +02:00
|
|
|
/** @brief changes the application language.
|
|
|
|
* @param locale the new locale (names as defined by Calamares).
|
2019-08-04 21:22:54 +02:00
|
|
|
* @param brandingTranslationsPrefix the branding path prefix, from Calamares::Branding.
|
|
|
|
*/
|
2021-07-26 13:14:30 +02:00
|
|
|
DLLEXPORT void installTranslator( const CalamaresUtils::Locale::Translation::Id& locale, const QString& brandingTranslationsPrefix );
|
|
|
|
|
|
|
|
/** @brief Initializes the translations with the current system settings
|
|
|
|
*/
|
|
|
|
DLLEXPORT void installTranslator();
|
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.
|
|
|
|
*/
|
2021-07-26 13:14:30 +02:00
|
|
|
DLLEXPORT CalamaresUtils::Locale::Translation::Id 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
|
|
|
|
*/
|
2021-07-26 13:14:30 +02:00
|
|
|
DLLEXPORT bool loadTranslator( const CalamaresUtils::Locale::Translation::Id& locale, const QString& prefix, QTranslator* translator );
|
2020-10-30 13:58:17 +01:00
|
|
|
|
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 );
|
|
|
|
|
2021-07-24 12:01:30 +02:00
|
|
|
|
|
|
|
/** @brief Handles change-of-language events
|
|
|
|
*
|
|
|
|
* There is one single Retranslator object. Use `instance()` to get it.
|
|
|
|
* The top-level widget of the application should call
|
|
|
|
* `installEventFilter( Retranslator::instance() )`
|
|
|
|
* to set up event-handling for translation events. The Retranslator
|
|
|
|
* will emit signal `languageChanged()` if there is such an event.
|
|
|
|
*
|
|
|
|
* Normal consumers should not have to use the Retranslator directly,
|
|
|
|
* but use the macros `CALAMARES_RETRANSLATE*` to set things up
|
|
|
|
* in code -- the macros will connect to the Retranslator's signals.
|
|
|
|
*/
|
2014-11-11 12:42:37 +01:00
|
|
|
class Retranslator : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
2021-07-24 12:16:19 +02:00
|
|
|
/// @brief Gets the global (single) Retranslator object
|
2021-07-24 12:01:30 +02:00
|
|
|
static Retranslator* instance();
|
2014-11-11 15:12:17 +01:00
|
|
|
|
2021-07-24 12:16:19 +02:00
|
|
|
/// @brief Helper function for attaching lambdas
|
|
|
|
static void attach( QObject* o, std::function< void( void ) > f);
|
|
|
|
|
2019-08-29 14:53:03 +02:00
|
|
|
signals:
|
2021-07-24 12:01:30 +02:00
|
|
|
void languageChanged();
|
2019-08-29 14:53:03 +02:00
|
|
|
|
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 );
|
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-07-24 12:01:30 +02:00
|
|
|
/** @brief Call code for this object when language changes
|
|
|
|
*
|
|
|
|
* @p body should be a code block (it does not need braces) that can be wrapped
|
|
|
|
* up as a lambda. When the language changes, the lambda is called. Note that
|
|
|
|
* this macro should be used in constructors or other code that is run only
|
|
|
|
* once, since otherwise you will end up with multiple calls to @p body.
|
2021-07-24 12:16:19 +02:00
|
|
|
*
|
|
|
|
* NOTE: unlike plain QObject::connect(), the body is **also** called
|
|
|
|
* immediately after setting up the connection. This allows
|
|
|
|
* setup and translation code to be mixed together.
|
2021-07-24 12:01:30 +02:00
|
|
|
*/
|
2021-07-24 12:16:19 +02:00
|
|
|
#define CALAMARES_RETRANSLATE( body ) CalamaresUtils::Retranslator::attach( this, [=] { body } )
|
2021-07-24 12:01:30 +02:00
|
|
|
/** @brief Call code for the given object (widget) when language changes
|
|
|
|
*
|
|
|
|
* This is identical to CALAMARES_RETRANSLATE, except the @p body is called
|
2021-07-24 12:16:19 +02:00
|
|
|
* for the given object, not this object.
|
2021-07-24 12:01:30 +02:00
|
|
|
*
|
2021-07-24 12:16:19 +02:00
|
|
|
* NOTE: unlike plain QObject::connect(), the body is **also** called
|
|
|
|
* immediately after setting up the connection. This allows
|
|
|
|
* setup and translation code to be mixed together.
|
2021-07-24 12:01:30 +02:00
|
|
|
*/
|
2021-07-24 12:16:19 +02:00
|
|
|
#define CALAMARES_RETRANSLATE_FOR( object, body ) CalamaresUtils::Retranslator::attach( object, [=] { body } )
|
2021-07-24 12:01:30 +02:00
|
|
|
/** @brief Call a slot in this object when language changes
|
|
|
|
*
|
|
|
|
* Given a slot (in method-function-pointer notation), call that slot when the
|
|
|
|
* language changes. This is shorthand for connecting the Retranslator's
|
|
|
|
* signal to the given slot.
|
2021-07-24 12:16:19 +02:00
|
|
|
*
|
|
|
|
* NOTE: unlike plain QObject::connect(), the slot is **also** called
|
|
|
|
* immediately after setting up the connection. This allows
|
|
|
|
* setup and translation code to be mixed together.
|
2021-07-24 12:01:30 +02:00
|
|
|
*/
|
2021-07-24 12:16:19 +02:00
|
|
|
#define CALAMARES_RETRANSLATE_SLOT( slotfunc ) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
connect( CalamaresUtils::Retranslator::instance(), &CalamaresUtils::Retranslator::languageChanged, this, slotfunc ); \
|
|
|
|
(this->*slotfunc)(); \
|
|
|
|
} while ( false )
|
2014-11-11 15:11:06 +01:00
|
|
|
|
2019-04-29 12:12:18 +02:00
|
|
|
#endif
|