mirror of https://github.com/cutefishos/calamares
Merge branch 'translatable-configuration'
commit
39b7e4c55f
@ -0,0 +1,112 @@
|
|||||||
|
/* === 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 "TranslatableConfiguration.h"
|
||||||
|
|
||||||
|
#include "LabelModel.h"
|
||||||
|
|
||||||
|
#include "utils/Logger.h"
|
||||||
|
#include "utils/Variant.h"
|
||||||
|
|
||||||
|
#include <QRegularExpression>
|
||||||
|
#include <QRegularExpressionMatch>
|
||||||
|
|
||||||
|
namespace CalamaresUtils
|
||||||
|
{
|
||||||
|
namespace Locale
|
||||||
|
{
|
||||||
|
TranslatedString::TranslatedString( const QString& string )
|
||||||
|
{
|
||||||
|
m_strings[ QString() ] = string;
|
||||||
|
}
|
||||||
|
TranslatedString::TranslatedString( const QVariantMap& map, const QString& key )
|
||||||
|
{
|
||||||
|
// Get the un-decorated value for the key
|
||||||
|
QString value = CalamaresUtils::getString( map, key );
|
||||||
|
if ( value.isEmpty() )
|
||||||
|
{
|
||||||
|
value = key;
|
||||||
|
}
|
||||||
|
m_strings[ QString() ] = value;
|
||||||
|
|
||||||
|
for ( auto it = map.constKeyValueBegin(); it != map.constKeyValueEnd(); ++it )
|
||||||
|
{
|
||||||
|
QString subkey = ( *it ).first;
|
||||||
|
if ( subkey == key )
|
||||||
|
{
|
||||||
|
// Already obtained, above
|
||||||
|
}
|
||||||
|
else if ( subkey.startsWith( key ) )
|
||||||
|
{
|
||||||
|
QRegularExpressionMatch match;
|
||||||
|
if ( subkey.indexOf( QRegularExpression( "\\[([a-zA-Z_@]*)\\]" ), 0, &match ) > 0 )
|
||||||
|
{
|
||||||
|
QString language = match.captured( 1 );
|
||||||
|
m_strings[ language ] = ( *it ).second.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
QString
|
||||||
|
TranslatedString::get() const
|
||||||
|
{
|
||||||
|
return get( QLocale() );
|
||||||
|
}
|
||||||
|
|
||||||
|
QString
|
||||||
|
TranslatedString::get( const QLocale& locale ) const
|
||||||
|
{
|
||||||
|
QString localeName = locale.name();
|
||||||
|
// Special case, sr@latin doesn't have the @latin reflected in the name
|
||||||
|
if ( locale.language() == QLocale::Language::Serbian && locale.script() == QLocale::Script::LatinScript )
|
||||||
|
{
|
||||||
|
localeName = QStringLiteral( "sr@latin" );
|
||||||
|
}
|
||||||
|
|
||||||
|
cDebug() << "Getting locale" << localeName;
|
||||||
|
if ( m_strings.contains( localeName ) )
|
||||||
|
{
|
||||||
|
return m_strings[ localeName ];
|
||||||
|
}
|
||||||
|
int index = localeName.indexOf( '@' );
|
||||||
|
if ( index > 0 )
|
||||||
|
{
|
||||||
|
localeName.truncate( index );
|
||||||
|
if ( m_strings.contains( localeName ) )
|
||||||
|
{
|
||||||
|
return m_strings[ localeName ];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
index = localeName.indexOf( '_' );
|
||||||
|
if ( index > 0 )
|
||||||
|
{
|
||||||
|
localeName.truncate( index );
|
||||||
|
if ( m_strings.contains( localeName ) )
|
||||||
|
{
|
||||||
|
return m_strings[ localeName ];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return m_strings[ QString() ];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace Locale
|
||||||
|
} // namespace CalamaresUtils
|
@ -0,0 +1,63 @@
|
|||||||
|
/* === 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef LOCALE_TRANSLATABLECONFIGURATION_H
|
||||||
|
#define LOCALE_TRANSLATABLECONFIGURATION_H
|
||||||
|
|
||||||
|
#include "DllMacro.h"
|
||||||
|
|
||||||
|
#include <QLocale>
|
||||||
|
#include <QMap>
|
||||||
|
#include <QVariant>
|
||||||
|
|
||||||
|
namespace CalamaresUtils
|
||||||
|
{
|
||||||
|
namespace Locale
|
||||||
|
{
|
||||||
|
/** @brief A human-readable string from a configuration file
|
||||||
|
*
|
||||||
|
* The configuration files can contain human-readable strings,
|
||||||
|
* but those need their own translations and are not supported
|
||||||
|
* by QObject::tr or anything else.
|
||||||
|
*/
|
||||||
|
class DLLEXPORT TranslatedString
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/** @brief Get all the translations connected to @p key
|
||||||
|
*/
|
||||||
|
TranslatedString( const QVariantMap& map, const QString& key );
|
||||||
|
/** @brief Not-actually-translated string.
|
||||||
|
*/
|
||||||
|
TranslatedString( const QString& string );
|
||||||
|
|
||||||
|
int count() const { return m_strings.count(); }
|
||||||
|
|
||||||
|
/// @brief Gets the string in the current locale
|
||||||
|
QString get() const;
|
||||||
|
|
||||||
|
/// @brief Gets the string from the given locale
|
||||||
|
QString get( const QLocale& ) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
// Maps locale name to human-readable string, "" is English
|
||||||
|
QMap< QString, QString > m_strings;
|
||||||
|
};
|
||||||
|
} // namespace Locale
|
||||||
|
} // namespace CalamaresUtils
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue