[libcalamares] Implement translatable-pair-of-char*

- This is base functionality for a translatable model of
   TimeZone names that is scraped from zone.tab
This commit is contained in:
Adriaan de Groot 2019-11-26 11:18:35 +01:00
parent abd3c4171b
commit 3af2754022
2 changed files with 105 additions and 9 deletions

View file

@ -0,0 +1,83 @@
/* === This file is part of Calamares - <https://github.com/calamares> ===
*
* Copyright 2019, Adriaan de Groot <groot@kde.org>
*
* 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 "TimeZone.h"
#include <cstring>
namespace CalamaresUtils
{
namespace Locale
{
CStringPair::CStringPair( CStringPair&& t )
{
// 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 ? strdup( t.m_key ) : nullptr )
{
}
/** @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( const char* s1 )
: m_human( s1 ? munge( s1 ) : nullptr )
, m_key( s1 ? strdup( s1 ) : nullptr )
{
}
CStringPair::~CStringPair()
{
free( m_human );
free( m_key );
}
} // namespace Locale
} // namespace CalamaresUtils

View file

@ -19,6 +19,7 @@
#ifndef LOCALE_TIMEZONE_H
#define LOCALE_TIMEZONE_H
#include <QObject>
#include <QString>
namespace CalamaresUtils
@ -39,14 +40,20 @@ namespace Locale
class CStringPair
{
public:
explicit CStringPair(const char *s1);
CStringPair(CStringPair&& t);
CStringPair(const CStringPair&) = delete;
~CStringPair();
/// @brief An empty pair
CStringPair() {};
/// @brief Given an identifier, create the pair
explicit CStringPair( const char* s1 );
CStringPair( CStringPair&& t );
CStringPair( const CStringPair& );
virtual ~CStringPair();
private:
const char* m_human = nullptr;
const char* m_key = nullptr;
/// @brief Give the localized human-readable form
virtual QString tr() const = 0;
protected:
char* m_human = nullptr;
char* m_key = nullptr;
};
/// @brief A pair of strings for timezone regions (e.g. "America")
@ -54,6 +61,9 @@ class TZRegion : public CStringPair
{
public:
using CStringPair::CStringPair;
// NOTE: context name must match what's used in zone-extractor.py
QString tr() const override { return QObject::tr( m_human, "tz_regions" ); }
};
/// @brief A pair of strings for specific timezone names (e.g. "New_York")
@ -61,9 +71,12 @@ class TZZone : public CStringPair
{
public:
using CStringPair::CStringPair;
// NOTE: context name must match what's used in zone-extractor.py
QString tr() const override { return QObject::tr( m_human, "tz_names" ); }
};
}
}
} // namespace Locale
} // namespace CalamaresUtils
#endif // LOCALE_TIMEZONE_H