OSDN Git Service

fb8890e87d1e437921e1820997039d5c9fc0eb5b
[alterlinux/alterlinux-calamares.git] / src / libcalamares / locale / Translation.cpp
1 /* === This file is part of Calamares - <https://calamares.io> ===
2  *
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
6  *
7  *   Calamares is Free Software: see the License-Identifier above.
8  *
9  *
10  */
11
12 #include "Translation.h"
13
14 #include <memory>
15
16 /** @brief Handle special cases of Calamares language names
17  *
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.
24  *
25  * Returns a pair of nullptrs for non-special cases.
26  */
27 static std::pair< QLocale*, QString* >
28 specialCase( const CalamaresUtils::Locale::Translation::Id& locale )
29 {
30     const QString localeName = locale.name;
31     if ( localeName == "sr@latin" )
32     {
33         static QLocale loc( QLocale::Language::Serbian, QLocale::Script::LatinScript, QLocale::Country::Serbia );
34         return { &loc, nullptr };
35     }
36     if ( localeName == "ca@valencia" )
37     {
38         static QString name = QStringLiteral( "Català (València)" );
39         return { nullptr, &name };
40     }
41
42     return { nullptr, nullptr };
43 }
44
45 namespace CalamaresUtils
46 {
47 namespace Locale
48 {
49
50 Translation::Translation( QObject* parent )
51     : Translation( { QString() }, LabelFormat::IfNeededWithCountry, parent )
52 {
53 }
54
55 Translation::Translation( const Id& localeId, LabelFormat format, QObject* parent )
56     : QObject( parent )
57     , m_locale( getLocale( localeId ) )
58     , m_localeId( localeId.name.isEmpty() ? m_locale.name() : localeId.name )
59 {
60     auto [ _, name ] = specialCase( localeId );
61
62     QString longFormat = QObject::tr( "%1 (%2)" );
63
64     QString languageName = name ? *name : m_locale.nativeLanguageName();
65     QString englishName = m_locale.languageToString( m_locale.language() );
66
67     if ( languageName.isEmpty() )
68     {
69         languageName = QString( "* %1 (%2)" ).arg( localeId.name, englishName );
70     }
71
72     bool needsCountryName = ( format == LabelFormat::AlwaysWithCountry )
73         || ( localeId.name.contains( '_' ) && QLocale::countriesForLanguage( m_locale.language() ).count() > 1 );
74     QString countryName = ( needsCountryName ?
75
76                                              m_locale.nativeCountryName()
77                                              : QString() );
78     m_label = needsCountryName ? longFormat.arg( languageName, countryName ) : languageName;
79     m_englishLabel = needsCountryName ? longFormat.arg( englishName, QLocale::countryToString( m_locale.country() ) )
80                                       : englishName;
81 }
82
83 QLocale
84 Translation::getLocale( const Id& localeId )
85 {
86     const QString& localeName = localeId.name;
87     if ( localeName.isEmpty() )
88     {
89         return QLocale();
90     }
91
92     auto [ locale, _ ] = specialCase( localeId );
93     return locale ? *locale : QLocale( localeName );
94 }
95
96 }  // namespace Locale
97 }  // namespace CalamaresUtils