From 66c4445077631147ea6c18c5f3cfae17ac621210 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 29 Apr 2019 06:33:55 -0400 Subject: [PATCH] [libcalamares] Move translation-related functions to Retranslator.h --- src/calamares/CalamaresApplication.cpp | 1 + src/libcalamares/utils/CalamaresUtils.cpp | 108 ---------------------- src/libcalamares/utils/CalamaresUtils.h | 12 --- src/libcalamares/utils/Retranslator.cpp | 96 +++++++++++++++++++ src/libcalamares/utils/Retranslator.h | 15 +++ 5 files changed, 112 insertions(+), 120 deletions(-) diff --git a/src/calamares/CalamaresApplication.cpp b/src/calamares/CalamaresApplication.cpp index 81ec2d160..2d5ea41f0 100644 --- a/src/calamares/CalamaresApplication.cpp +++ b/src/calamares/CalamaresApplication.cpp @@ -30,6 +30,7 @@ #include "utils/CalamaresUtilsSystem.h" #include "utils/Dirs.h" #include "utils/Logger.h" +#include "utils/Retranslator.h" #include "JobQueue.h" #include "Branding.h" #include "Settings.h" diff --git a/src/libcalamares/utils/CalamaresUtils.cpp b/src/libcalamares/utils/CalamaresUtils.cpp index 25eceb159..b44be6795 100644 --- a/src/libcalamares/utils/CalamaresUtils.cpp +++ b/src/libcalamares/utils/CalamaresUtils.cpp @@ -24,116 +24,8 @@ #include "CalamaresUtils.h" -#include "CalamaresConfig.h" -#include "Logger.h" - -#include -#include -#include -#include -#include -#include - -#include - -using std::cerr; - namespace CalamaresUtils { - -static QTranslator* s_brandingTranslator = nullptr; -static QTranslator* s_translator = nullptr; -static QString s_translatorLocaleName; - -void -installTranslator( const QLocale& locale, - const QString& brandingTranslationsPrefix, - QObject* parent ) -{ - QString localeName = locale.name(); - localeName.replace( "-", "_" ); - - if ( localeName == "C" ) - localeName = "en"; - - // Special case of sr@latin - // - // See top-level CMakeLists.txt about special cases for translation loading. - if ( locale.language() == QLocale::Language::Serbian && locale.script() == QLocale::Script::LatinScript ) - localeName = QStringLiteral( "sr@latin" ); - - cDebug() << "Looking for translations for" << localeName; - - QTranslator* translator = nullptr; - - // Branding translations - if ( !brandingTranslationsPrefix.isEmpty() ) - { - QString brandingTranslationsDirPath( brandingTranslationsPrefix ); - brandingTranslationsDirPath.truncate( brandingTranslationsPrefix.lastIndexOf( - QDir::separator() ) ); - QDir brandingTranslationsDir( brandingTranslationsDirPath ); - if ( brandingTranslationsDir.exists() ) - { - QString filenameBase( brandingTranslationsPrefix ); - filenameBase.remove( 0, brandingTranslationsPrefix.lastIndexOf( - QDir::separator() ) + 1 ); - translator = new QTranslator( parent ); - if ( translator->load( locale, - filenameBase, - "_", - brandingTranslationsDir.absolutePath() ) ) - { - cDebug() << Logger::SubEntry << "Branding using locale:" << localeName; - } - else - { - cDebug() << Logger::SubEntry << "Branding using default, system locale not found:" << localeName; - translator->load( brandingTranslationsPrefix + "en" ); - } - - if ( s_brandingTranslator ) - { - QCoreApplication::removeTranslator( s_brandingTranslator ); - delete s_brandingTranslator; - } - - QCoreApplication::installTranslator( translator ); - s_brandingTranslator = translator; - } - } - - // Calamares translations - translator = new QTranslator( parent ); - if ( translator->load( QString( ":/lang/calamares_" ) + localeName ) ) - { - cDebug() << Logger::SubEntry << "Calamares using locale:" << localeName; - } - else - { - cDebug() << Logger::SubEntry << "Calamares using default, system locale not found:" << localeName; - translator->load( QString( ":/lang/calamares_en" ) ); - } - - if ( s_translator ) - { - QCoreApplication::removeTranslator( s_translator ); - delete s_translator; - } - - QCoreApplication::installTranslator( translator ); - s_translator = translator; - - s_translatorLocaleName = localeName; -} - - -QString -translatorLocaleName() -{ - return s_translatorLocaleName; -} - QString removeDiacritics( const QString& string ) { diff --git a/src/libcalamares/utils/CalamaresUtils.h b/src/libcalamares/utils/CalamaresUtils.h index 49abf0cd6..39d338d5c 100644 --- a/src/libcalamares/utils/CalamaresUtils.h +++ b/src/libcalamares/utils/CalamaresUtils.h @@ -37,18 +37,6 @@ class QObject; */ namespace CalamaresUtils { - /** - * @brief installTranslator changes the application language. - * @param locale the new locale. - * @param brandingTranslationsPrefix the branding path prefix, from Calamares::Branding. - * @param parent the parent QObject. - */ - DLLEXPORT void installTranslator( const QLocale& locale, - const QString& brandingTranslationsPrefix, - QObject* parent ); - - DLLEXPORT QString translatorLocaleName(); - /** * @brief removeDiacritics replaces letters with diacritics and ligatures with * alternative forms and digraphs. diff --git a/src/libcalamares/utils/Retranslator.cpp b/src/libcalamares/utils/Retranslator.cpp index 1cc25fa70..93560f4ae 100644 --- a/src/libcalamares/utils/Retranslator.cpp +++ b/src/libcalamares/utils/Retranslator.cpp @@ -18,11 +18,107 @@ #include "Retranslator.h" +#include "Logger.h" + +#include +#include #include +#include namespace CalamaresUtils { +static QTranslator* s_brandingTranslator = nullptr; +static QTranslator* s_translator = nullptr; +static QString s_translatorLocaleName; + +void +installTranslator( const QLocale& locale, + const QString& brandingTranslationsPrefix, + QObject* parent ) +{ + QString localeName = locale.name(); + localeName.replace( "-", "_" ); + + if ( localeName == "C" ) + localeName = "en"; + + // Special case of sr@latin + // + // See top-level CMakeLists.txt about special cases for translation loading. + if ( locale.language() == QLocale::Language::Serbian && locale.script() == QLocale::Script::LatinScript ) + localeName = QStringLiteral( "sr@latin" ); + + cDebug() << "Looking for translations for" << localeName; + + QTranslator* translator = nullptr; + + // Branding translations + if ( !brandingTranslationsPrefix.isEmpty() ) + { + QString brandingTranslationsDirPath( brandingTranslationsPrefix ); + brandingTranslationsDirPath.truncate( brandingTranslationsPrefix.lastIndexOf( + QDir::separator() ) ); + QDir brandingTranslationsDir( brandingTranslationsDirPath ); + if ( brandingTranslationsDir.exists() ) + { + QString filenameBase( brandingTranslationsPrefix ); + filenameBase.remove( 0, brandingTranslationsPrefix.lastIndexOf( + QDir::separator() ) + 1 ); + translator = new QTranslator( parent ); + if ( translator->load( locale, + filenameBase, + "_", + brandingTranslationsDir.absolutePath() ) ) + { + cDebug() << Logger::SubEntry << "Branding using locale:" << localeName; + } + else + { + cDebug() << Logger::SubEntry << "Branding using default, system locale not found:" << localeName; + translator->load( brandingTranslationsPrefix + "en" ); + } + + if ( s_brandingTranslator ) + { + QCoreApplication::removeTranslator( s_brandingTranslator ); + delete s_brandingTranslator; + } + + QCoreApplication::installTranslator( translator ); + s_brandingTranslator = translator; + } + } + // Calamares translations + translator = new QTranslator( parent ); + if ( translator->load( QString( ":/lang/calamares_" ) + localeName ) ) + { + cDebug() << Logger::SubEntry << "Calamares using locale:" << localeName; + } + else + { + cDebug() << Logger::SubEntry << "Calamares using default, system locale not found:" << localeName; + translator->load( QString( ":/lang/calamares_en" ) ); + } + + if ( s_translator ) + { + QCoreApplication::removeTranslator( s_translator ); + delete s_translator; + } + + QCoreApplication::installTranslator( translator ); + s_translator = translator; + + s_translatorLocaleName = localeName; +} + + +QString +translatorLocaleName() +{ + return s_translatorLocaleName; +} void Retranslator::attachRetranslator( QObject* parent, diff --git a/src/libcalamares/utils/Retranslator.h b/src/libcalamares/utils/Retranslator.h index af561a9ce..e06630630 100644 --- a/src/libcalamares/utils/Retranslator.h +++ b/src/libcalamares/utils/Retranslator.h @@ -19,15 +19,30 @@ #ifndef UTILS_RETRANSLATOR_H #define UTILS_RETRANSLATOR_H +#include "DllMacro.h" + #include #include +#include #include class QEvent; +class QLocale; namespace CalamaresUtils { + /** + * @brief installTranslator changes the application language. + * @param locale the new locale. + * @param brandingTranslationsPrefix the branding path prefix, from Calamares::Branding. + * @param parent the parent QObject. + */ + DLLEXPORT void installTranslator( const QLocale& locale, + const QString& brandingTranslationsPrefix, + QObject* parent ); + + DLLEXPORT QString translatorLocaleName(); class Retranslator : public QObject {