mirror of
https://github.com/parchlinux/calamares.git
synced 2025-02-24 10:55:46 -05:00
[libcalamares] Translatable config strings use tr()-infrastructure
- Allow TranslatedString to get a context parameter; if it has one, it will try to use the regular tr()-infrastructure **as fallback** for the translations from the config file itself. - This makes it possible to offer -- and translate -- some "standard" phrases in the module, while allowing the config file the knob to change strings. Using one of the standard strings gets translations for "free", while introducing something entirely new means sourcing translations for it as well.
This commit is contained in:
parent
25e3f91754
commit
0ef28f6a50
2 changed files with 17 additions and 3 deletions
|
@ -23,6 +23,7 @@
|
|||
#include "utils/Logger.h"
|
||||
#include "utils/Variant.h"
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QRegularExpression>
|
||||
#include <QRegularExpressionMatch>
|
||||
|
||||
|
@ -34,7 +35,9 @@ TranslatedString::TranslatedString( const QString& string )
|
|||
{
|
||||
m_strings[ QString() ] = string;
|
||||
}
|
||||
TranslatedString::TranslatedString( const QVariantMap& map, const QString& key )
|
||||
|
||||
TranslatedString::TranslatedString( const QVariantMap& map, const QString& key, const char* context )
|
||||
: m_context( context )
|
||||
{
|
||||
// Get the un-decorated value for the key
|
||||
QString value = CalamaresUtils::getString( map, key );
|
||||
|
@ -99,7 +102,17 @@ TranslatedString::get( const QLocale& locale ) const
|
|||
}
|
||||
}
|
||||
|
||||
return m_strings[ QString() ];
|
||||
// If we're given a context to work with, also try the same string in
|
||||
// the regular translation framework.
|
||||
const QString& s = m_strings[ QString() ];
|
||||
if ( m_context )
|
||||
{
|
||||
return QCoreApplication::translate( m_context, s.toLatin1().constData() );
|
||||
}
|
||||
else
|
||||
{
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -40,7 +40,7 @@ class DLLEXPORT TranslatedString
|
|||
public:
|
||||
/** @brief Get all the translations connected to @p key
|
||||
*/
|
||||
TranslatedString( const QVariantMap& map, const QString& key );
|
||||
TranslatedString( const QVariantMap& map, const QString& key, const char* context = nullptr );
|
||||
/** @brief Not-actually-translated string.
|
||||
*/
|
||||
TranslatedString( const QString& string );
|
||||
|
@ -73,6 +73,7 @@ public:
|
|||
private:
|
||||
// Maps locale name to human-readable string, "" is English
|
||||
QMap< QString, QString > m_strings;
|
||||
const char* m_context = nullptr;
|
||||
};
|
||||
} // namespace Locale
|
||||
} // namespace CalamaresUtils
|
||||
|
|
Loading…
Add table
Reference in a new issue