Merge branch 'translate-configs'

Use regular translation machinery to support and help out translations
from the config files. This reduces the need to do all the translation
in those files -- some of it can be shared with the regular TX workflow.
This commit is contained in:
Adriaan de Groot 2020-02-19 14:46:58 +01:00
commit 790fbb96d5
4 changed files with 56 additions and 8 deletions

View File

@ -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;
}
}

View File

@ -39,8 +39,19 @@ class DLLEXPORT TranslatedString
{
public:
/** @brief Get all the translations connected to @p key
*
* Gets map[key] as the "untranslated" form, and then all the
* keys of the form <key>[lang] are taken as the translation
* for <lang> of the untranslated form.
*
* If @p context is not a nullptr, then that is taken as an
* indication to **also** use the regular QObject::tr() translation
* mechanism for these strings. It is recommended to pass in
* metaObject()->className() as context (from a QObject based class)
* to give the TranslatedString the same context as other calls
* to tr() within that class.
*/
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 +84,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

View File

@ -55,6 +55,17 @@ QString
NetInstallViewStep::prettyName() const
{
return m_sidebarLabel ? m_sidebarLabel->get() : tr( "Package selection" );
// This is a table of "standard" labels for this module. If you use them
// in the label: sidebar: section of the config file, the existing
// translations can be used.
NOTREACHED
tr( "Package selection" );
tr( "Office software" );
tr( "Office package" );
tr( "Browser software" );
tr( "Browser package" );
tr( "Web browser" );
}
@ -199,10 +210,11 @@ NetInstallViewStep::setConfigurationMap( const QVariantMap& configurationMap )
if ( label.contains( "sidebar" ) )
{
m_sidebarLabel = new CalamaresUtils::Locale::TranslatedString( label, "sidebar" );
m_sidebarLabel = new CalamaresUtils::Locale::TranslatedString( label, "sidebar", metaObject()->className() );
}
if ( label.contains( "title" ) )
{
m_widget->setPageTitle( new CalamaresUtils::Locale::TranslatedString( label, "title" ) );
m_widget->setPageTitle(
new CalamaresUtils::Locale::TranslatedString( label, "title", metaObject()->className() ) );
}
}

View File

@ -30,8 +30,19 @@ required: false
# If no *sidebar* values are provided, defaults to "Package selection"
# and existing translations. If no *title* values are provided, no string
# is displayed.
#
# The following strings are already known to Calamares and can be
# listed here in *untranslated* form (e.g. as value of *sidebar*)
# without bothering with the translations: they are picked up from
# the regular translation framework:
# - "Package selection"
# - "Office software"
# - "Office package"
# - "Browser software"
# - "Browser package"
# - "Web browser"
label:
sidebar: "Package selection"
sidebar[nl]: "Pakketkeuze"
title: "Office Package"
title[nl]: "Kantoorsoftware"
# sidebar[nl]: "Pakketkeuze"
# title: "Office Package"
# title[nl]: "Kantoorsoftware"