1 /* === This file is part of Calamares - <https://calamares.io> ===
3 * SPDX-FileCopyrightText: 2014-2015 Teo Mrnjavac <teo@kde.org>
4 * SPDX-FileCopyrightText: 2017-2019 Adriaan de Groot <groot@kde.org>
5 * SPDX-License-Identifier: GPL-3.0-or-later
7 * Calamares is Free Software: see the License-Identifier above.
12 #include "Translation.h"
16 /** @brief Handle special cases of Calamares language names
18 * If a given @p localeName (e.g. en_US, or sr@latin) has special handling,
19 * returns a pair of pointers:
20 * - a pointer to a QLocale; this is the locale to use, or may be @c nullptr
21 * to indicate that the Qt locale derived from @p localeName is accepatable.
22 * - a pointer to a QString; this is the native language name to use, or may
23 * be @c nullptr to indicate that the Qt value is acceptable.
25 * Returns a pair of nullptrs for non-special cases.
27 static std::pair< QLocale*, QString* >
28 specialCase( const CalamaresUtils::Locale::Translation::Id& locale )
30 const QString localeName = locale.name;
31 if ( localeName == "sr@latin" )
33 static QLocale loc( QLocale::Language::Serbian, QLocale::Script::LatinScript, QLocale::Country::Serbia );
34 return { &loc, nullptr };
36 if ( localeName == "ca@valencia" )
38 static QString name = QStringLiteral( "Català (València)" );
39 return { nullptr, &name };
42 return { nullptr, nullptr };
45 namespace CalamaresUtils
50 Translation::Translation( QObject* parent )
51 : Translation( { QString() }, LabelFormat::IfNeededWithCountry, parent )
55 Translation::Translation( const Id& localeId, LabelFormat format, QObject* parent )
57 , m_locale( getLocale( localeId ) )
58 , m_localeId( localeId.name.isEmpty() ? m_locale.name() : localeId.name )
60 auto [ _, name ] = specialCase( localeId );
62 QString longFormat = QObject::tr( "%1 (%2)" );
64 QString languageName = name ? *name : m_locale.nativeLanguageName();
65 QString englishName = m_locale.languageToString( m_locale.language() );
67 if ( languageName.isEmpty() )
69 languageName = QString( "* %1 (%2)" ).arg( localeId.name, englishName );
72 bool needsCountryName = ( format == LabelFormat::AlwaysWithCountry )
73 || ( localeId.name.contains( '_' ) && QLocale::countriesForLanguage( m_locale.language() ).count() > 1 );
74 QString countryName = ( needsCountryName ?
76 m_locale.nativeCountryName()
78 m_label = needsCountryName ? longFormat.arg( languageName, countryName ) : languageName;
79 m_englishLabel = needsCountryName ? longFormat.arg( englishName, QLocale::countryToString( m_locale.country() ) )
84 Translation::getLocale( const Id& localeId )
86 const QString& localeName = localeId.name;
87 if ( localeName.isEmpty() )
92 auto [ locale, _ ] = specialCase( localeId );
93 return locale ? *locale : QLocale( localeName );
97 } // namespace CalamaresUtils