[libcalamares] Factor out the list of translations

The list of translation codes lives in one place, make it accessible
through a function in Calamares::Locale.
This commit is contained in:
Adriaan de Groot 2022-07-03 00:05:40 +02:00
parent 18a3092aa1
commit 170a5a8697
5 changed files with 26 additions and 16 deletions

View File

@ -10,7 +10,6 @@
#include "CalamaresApplication.h" #include "CalamaresApplication.h"
#include "CalamaresConfig.h" #include "CalamaresConfig.h"
#include "CalamaresTranslations.h"
#include "CalamaresVersionX.h" #include "CalamaresVersionX.h"
#include "CalamaresWindow.h" #include "CalamaresWindow.h"
#include "progresstree/ProgressTreeView.h" #include "progresstree/ProgressTreeView.h"
@ -19,6 +18,7 @@
#include "JobQueue.h" #include "JobQueue.h"
#include "Settings.h" #include "Settings.h"
#include "ViewManager.h" #include "ViewManager.h"
#include "locale/TranslationsModel.h"
#include "modulesystem/ModuleManager.h" #include "modulesystem/ModuleManager.h"
#include "utils/CalamaresUtilsGui.h" #include "utils/CalamaresUtilsGui.h"
#include "utils/CalamaresUtilsSystem.h" #include "utils/CalamaresUtilsSystem.h"
@ -68,7 +68,7 @@ CalamaresApplication::init()
{ {
Logger::setupLogfile(); Logger::setupLogfile();
cDebug() << "Calamares version:" << CALAMARES_VERSION; cDebug() << "Calamares version:" << CALAMARES_VERSION;
cDebug() << Logger::SubEntry << "languages:" << Calamares::Locale::availableLanguages; cDebug() << Logger::SubEntry << "languages:" << Calamares::Locale::availableLanguages();
if ( !Calamares::Settings::instance() ) if ( !Calamares::Settings::instance() )
{ {

View File

@ -23,15 +23,14 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/CalamaresVersionX.h.in ${CMAKE_CURREN
set(_names_tu "#ifndef CALAMARES_TRANSLATIONS_H set(_names_tu "#ifndef CALAMARES_TRANSLATIONS_H
#define CALAMARES_TRANSLATIONS_H #define CALAMARES_TRANSLATIONS_H
#include <QStringList> #include <QStringList>
namespace Calamares { namespace {
namespace Locale { static const QStringList availableLanguageList{
static const QStringList availableLanguages{
") ")
foreach( l ${CALAMARES_TRANSLATION_LANGUAGES}) foreach( l ${CALAMARES_TRANSLATION_LANGUAGES})
string(APPEND _names_tu "\"${l}\",\n") string(APPEND _names_tu "\"${l}\",\n")
endforeach() endforeach()
string(APPEND _names_tu "};\n}} // namespaces\n#endif\n\n") string(APPEND _names_tu "};\n} // namespace\n#endif\n\n")
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/CalamaresTranslations.h "${_names_tu}") file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/CalamaresTranslations.cc "${_names_tu}")
add_library( add_library(
calamares calamares

View File

@ -13,7 +13,6 @@
#include "locale/TranslatableConfiguration.h" #include "locale/TranslatableConfiguration.h"
#include "locale/TranslationsModel.h" #include "locale/TranslationsModel.h"
#include "CalamaresTranslations.h"
#include "CalamaresVersion.h" #include "CalamaresVersion.h"
#include "GlobalStorage.h" #include "GlobalStorage.h"
#include "utils/Logger.h" #include "utils/Logger.h"
@ -154,11 +153,11 @@ someLanguages()
void void
LocaleTests::testTranslatableLanguages() LocaleTests::testTranslatableLanguages()
{ {
cDebug() << "Translation languages:" << Calamares::Locale::availableLanguages; cDebug() << "Translation languages:" << Calamares::Locale::availableLanguages();
for ( const auto& language : someLanguages() ) for ( const auto& language : someLanguages() )
{ {
// Could be QVERIFY, but then we don't see what language code fails // Could be QVERIFY, but then we don't see what language code fails
QCOMPARE( Calamares::Locale::availableLanguages.contains( language ) ? language : QString(), language ); QCOMPARE( Calamares::Locale::availableLanguages().contains( language ) ? language : QString(), language );
} }
} }

View File

@ -13,7 +13,7 @@
#include "Lookup.h" #include "Lookup.h"
#include "CalamaresTranslations.h" // For the list of translations #include "CalamaresTranslations.cc" // For the list of translations, generated at build time
namespace Calamares namespace Calamares
{ {
@ -139,9 +139,15 @@ TranslationsModel::find( const Translation::Id& id ) const
TranslationsModel* TranslationsModel*
availableTranslations() availableTranslations()
{ {
static TranslationsModel* model = new TranslationsModel( Calamares::Locale::availableLanguages ); static TranslationsModel* model = new TranslationsModel( availableLanguageList );
return model; return model;
} }
const QStringList&
availableLanguages()
{
return availableLanguageList;
}
} // namespace Locale } // namespace Locale
} // namespace Calamares } // namespace Calamares

View File

@ -74,16 +74,22 @@ private:
/** @brief Returns a model with all available translations. /** @brief Returns a model with all available translations.
* *
* The translations are set when Calamares is compiled; the list * The translations are set when Calamares is compiled; the list
* is provided by CMake via the CALAMARES_TRANSLATION_LANGUAGES * of names used can be queried with avalableLanguages().
* CMake variable (top-level CMakeLists.txt in Calamares) and
* compiled into the CalamaresTranslations.h internal header.
* #define.
* *
* This model is a singleton and can be shared. * This model is a singleton and can be shared.
* *
* NOTE: While the model is not typed const, it should be. Do not modify. * NOTE: While the model is not typed const, it should be. Do not modify.
*/ */
DLLEXPORT TranslationsModel* availableTranslations(); DLLEXPORT TranslationsModel* availableTranslations();
/** @brief The list of names (e.g. en, pt_BR) of available translations.
*
* The translations are set when Calamares is compiled.
* At CMake-time, the list CALAMARES_TRANSLATION_LANGUAGES
* is used to create the table.
*/
DLLEXPORT const QStringList& availableLanguages();
} // namespace Locale } // namespace Locale
} // namespace Calamares } // namespace Calamares
#endif #endif