[libcalamares] Split out CStringPair into TranslatableString
The (renamed) class TranslatableString keeps a key value (e.g. New_York) and a human-readable version around; the human-readable one is passed through QObject::tr() for translation on-the-fly.
This commit is contained in:
parent
3e32335511
commit
10fb5b95c7
@ -56,6 +56,7 @@ set( libSources
|
|||||||
locale/Lookup.cpp
|
locale/Lookup.cpp
|
||||||
locale/TimeZone.cpp
|
locale/TimeZone.cpp
|
||||||
locale/TranslatableConfiguration.cpp
|
locale/TranslatableConfiguration.cpp
|
||||||
|
locale/TranslatableString.cpp
|
||||||
|
|
||||||
# Modules
|
# Modules
|
||||||
modulesystem/InstanceKey.cpp
|
modulesystem/InstanceKey.cpp
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
|
|
||||||
#include "TimeZone.h"
|
#include "TimeZone.h"
|
||||||
|
|
||||||
|
#include "locale/TranslatableString.h"
|
||||||
#include "utils/Logger.h"
|
#include "utils/Logger.h"
|
||||||
#include "utils/String.h"
|
#include "utils/String.h"
|
||||||
|
|
||||||
@ -30,6 +31,11 @@
|
|||||||
|
|
||||||
static const char TZ_DATA_FILE[] = "/usr/share/zoneinfo/zone.tab";
|
static const char TZ_DATA_FILE[] = "/usr/share/zoneinfo/zone.tab";
|
||||||
|
|
||||||
|
namespace CalamaresUtils
|
||||||
|
{
|
||||||
|
namespace Locale
|
||||||
|
{
|
||||||
|
|
||||||
/** @brief Turns a string longitude or latitude notation into a double
|
/** @brief Turns a string longitude or latitude notation into a double
|
||||||
*
|
*
|
||||||
* This handles strings like "+4230+00131" from zone.tab,
|
* This handles strings like "+4230+00131" from zone.tab,
|
||||||
@ -63,104 +69,8 @@ getRightGeoLocation( QString str )
|
|||||||
return sign * num;
|
return sign * num;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @brief A pair of strings, one human-readable, one a key
|
|
||||||
*
|
|
||||||
* Given an identifier-like string (e.g. "New_York"), makes
|
|
||||||
* a human-readable version of that and keeps a copy of the
|
|
||||||
* identifier itself.
|
|
||||||
*
|
|
||||||
* This explicitly uses const char* instead of just being
|
|
||||||
* QPair<QString, QString> because the human-readable part
|
|
||||||
* may need to be translated through tr(), and that takes a char*.
|
|
||||||
* C-style strings.
|
|
||||||
*/
|
|
||||||
class CStringPair : public QObject
|
|
||||||
{
|
|
||||||
Q_OBJECT
|
|
||||||
public:
|
|
||||||
/// @brief An empty pair
|
|
||||||
CStringPair() {}
|
|
||||||
/// @brief Given an identifier, create the pair
|
|
||||||
explicit CStringPair( const char* s1 );
|
|
||||||
explicit CStringPair( const QString& s );
|
|
||||||
CStringPair( CStringPair&& t );
|
|
||||||
CStringPair( const CStringPair& );
|
|
||||||
virtual ~CStringPair();
|
|
||||||
|
|
||||||
/// @brief Give the localized human-readable form
|
class TimeZoneData : public TranslatableString
|
||||||
virtual QString tr() const = 0;
|
|
||||||
QString key() const { return m_key; }
|
|
||||||
|
|
||||||
bool operator==( const CStringPair& other ) const { return m_key == other.m_key; }
|
|
||||||
bool operator<( const CStringPair& other ) const { return m_key < other.m_key; }
|
|
||||||
|
|
||||||
protected:
|
|
||||||
char* m_human = nullptr;
|
|
||||||
QString m_key;
|
|
||||||
};
|
|
||||||
|
|
||||||
/** @brief Massage an identifier into a human-readable form
|
|
||||||
*
|
|
||||||
* Makes a copy of @p s, caller must free() it.
|
|
||||||
*/
|
|
||||||
static char*
|
|
||||||
munge( const char* s )
|
|
||||||
{
|
|
||||||
char* t = strdup( s );
|
|
||||||
if ( !t )
|
|
||||||
{
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
// replace("_"," ") in the Python script
|
|
||||||
char* p = t;
|
|
||||||
while ( *p )
|
|
||||||
{
|
|
||||||
if ( ( *p ) == '_' )
|
|
||||||
{
|
|
||||||
*p = ' ';
|
|
||||||
}
|
|
||||||
++p;
|
|
||||||
}
|
|
||||||
|
|
||||||
return t;
|
|
||||||
}
|
|
||||||
|
|
||||||
CStringPair::CStringPair( CStringPair&& t )
|
|
||||||
: m_human( nullptr )
|
|
||||||
, m_key()
|
|
||||||
{
|
|
||||||
// My pointers are initialized to nullptr
|
|
||||||
std::swap( m_human, t.m_human );
|
|
||||||
std::swap( m_key, t.m_key );
|
|
||||||
}
|
|
||||||
|
|
||||||
CStringPair::CStringPair( const CStringPair& t )
|
|
||||||
: m_human( t.m_human ? strdup( t.m_human ) : nullptr )
|
|
||||||
, m_key( t.m_key )
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
CStringPair::CStringPair( const char* s1 )
|
|
||||||
: m_human( s1 ? munge( s1 ) : nullptr )
|
|
||||||
, m_key( s1 ? QString( s1 ) : QString() )
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
CStringPair::CStringPair( const QString& s )
|
|
||||||
: m_human( munge( s.toUtf8().constData() ) )
|
|
||||||
, m_key( s )
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
CStringPair::~CStringPair()
|
|
||||||
{
|
|
||||||
free( m_human );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class TimeZoneData : public CStringPair
|
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
TimeZoneData( const QString& region,
|
TimeZoneData( const QString& region,
|
||||||
@ -181,7 +91,7 @@ TimeZoneData::TimeZoneData( const QString& region,
|
|||||||
const QString& country,
|
const QString& country,
|
||||||
double latitude,
|
double latitude,
|
||||||
double longitude )
|
double longitude )
|
||||||
: CStringPair( zone )
|
: TranslatableString( zone )
|
||||||
, m_region( region )
|
, m_region( region )
|
||||||
, m_country( country )
|
, m_country( country )
|
||||||
, m_latitude( latitude )
|
, m_latitude( latitude )
|
||||||
@ -197,10 +107,10 @@ TimeZoneData::tr() const
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class RegionData : public CStringPair
|
class RegionData : public TranslatableString
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using CStringPair::CStringPair;
|
using TranslatableString::TranslatableString;
|
||||||
QString tr() const override;
|
QString tr() const override;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -282,10 +192,6 @@ loadTZData( QVector< RegionData >& regions, QVector< TimeZoneData >& zones )
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace CalamaresUtils
|
|
||||||
{
|
|
||||||
namespace Locale
|
|
||||||
{
|
|
||||||
|
|
||||||
struct Private
|
struct Private
|
||||||
{
|
{
|
||||||
|
89
src/libcalamares/locale/TranslatableString.cpp
Normal file
89
src/libcalamares/locale/TranslatableString.cpp
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
||||||
|
*
|
||||||
|
* SPDX-FileCopyrightText: 2019 Adriaan de Groot <groot@kde.org>
|
||||||
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
*
|
||||||
|
* Calamares is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Calamares is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
#include "TranslatableString.h"
|
||||||
|
|
||||||
|
|
||||||
|
/** @brief Massage an identifier into a human-readable form
|
||||||
|
*
|
||||||
|
* Makes a copy of @p s, caller must free() it.
|
||||||
|
*/
|
||||||
|
static char*
|
||||||
|
munge( const char* s )
|
||||||
|
{
|
||||||
|
char* t = strdup( s );
|
||||||
|
if ( !t )
|
||||||
|
{
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// replace("_"," ") in the Python script
|
||||||
|
char* p = t;
|
||||||
|
while ( *p )
|
||||||
|
{
|
||||||
|
if ( ( *p ) == '_' )
|
||||||
|
{
|
||||||
|
*p = ' ';
|
||||||
|
}
|
||||||
|
++p;
|
||||||
|
}
|
||||||
|
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace CalamaresUtils
|
||||||
|
{
|
||||||
|
namespace Locale
|
||||||
|
{
|
||||||
|
|
||||||
|
TranslatableString::TranslatableString( TranslatableString&& t )
|
||||||
|
: m_human( nullptr )
|
||||||
|
, m_key()
|
||||||
|
{
|
||||||
|
// My pointers are initialized to nullptr
|
||||||
|
std::swap( m_human, t.m_human );
|
||||||
|
std::swap( m_key, t.m_key );
|
||||||
|
}
|
||||||
|
|
||||||
|
TranslatableString::TranslatableString( const TranslatableString& t )
|
||||||
|
: m_human( t.m_human ? strdup( t.m_human ) : nullptr )
|
||||||
|
, m_key( t.m_key )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
TranslatableString::TranslatableString( const char* s1 )
|
||||||
|
: m_human( s1 ? munge( s1 ) : nullptr )
|
||||||
|
, m_key( s1 ? QString( s1 ) : QString() )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
TranslatableString::TranslatableString( const QString& s )
|
||||||
|
: m_human( munge( s.toUtf8().constData() ) )
|
||||||
|
, m_key( s )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
TranslatableString::~TranslatableString()
|
||||||
|
{
|
||||||
|
free( m_human );
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Locale
|
||||||
|
} // namespace CalamaresUtils
|
68
src/libcalamares/locale/TranslatableString.h
Normal file
68
src/libcalamares/locale/TranslatableString.h
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
||||||
|
*
|
||||||
|
* SPDX-FileCopyrightText: 2019 Adriaan de Groot <groot@kde.org>
|
||||||
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
*
|
||||||
|
* Calamares is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Calamares is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
#ifndef LOCALE_TRANSLATABLESTRING_H
|
||||||
|
#define LOCALE_TRANSLATABLESTRING_H
|
||||||
|
|
||||||
|
#include <QString>
|
||||||
|
|
||||||
|
namespace CalamaresUtils
|
||||||
|
{
|
||||||
|
namespace Locale
|
||||||
|
{
|
||||||
|
|
||||||
|
/** @brief A pair of strings, one human-readable, one a key
|
||||||
|
*
|
||||||
|
* Given an identifier-like string (e.g. "New_York"), makes
|
||||||
|
* a human-readable version of that and keeps a copy of the
|
||||||
|
* identifier itself.
|
||||||
|
*
|
||||||
|
* This explicitly uses const char* instead of just being
|
||||||
|
* QPair<QString, QString> because the human-readable part
|
||||||
|
* may need to be translated through tr(), and that takes a char*
|
||||||
|
* C-style strings.
|
||||||
|
*/
|
||||||
|
class TranslatableString
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/// @brief An empty pair
|
||||||
|
TranslatableString() {}
|
||||||
|
/// @brief Given an identifier, create the pair
|
||||||
|
explicit TranslatableString( const char* s1 );
|
||||||
|
explicit TranslatableString( const QString& s );
|
||||||
|
TranslatableString( TranslatableString&& t );
|
||||||
|
TranslatableString( const TranslatableString& );
|
||||||
|
virtual ~TranslatableString();
|
||||||
|
|
||||||
|
/// @brief Give the localized human-readable form
|
||||||
|
virtual QString tr() const = 0;
|
||||||
|
QString key() const { return m_key; }
|
||||||
|
|
||||||
|
bool operator==( const TranslatableString& other ) const { return m_key == other.m_key; }
|
||||||
|
bool operator<( const TranslatableString& other ) const { return m_key < other.m_key; }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
char* m_human = nullptr;
|
||||||
|
QString m_key;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Locale
|
||||||
|
} // namespace CalamaresUtils
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue
Block a user