Fix in-wizard translation support

We in principle support a map of locale->string for keys starting with
"tr" like "trDisplayName". This didn't work everywhere.

We may not cast the value for these keys to QString before passing it to
`localizedString`, since that would result in an empty string if the
value is such a map.

Also fix the documentation since we remove all parts from '_' (to also
get rid of encoding parts).

Fixes: QTCREATORBUG-23575
Change-Id: I2be795053e645c8bf81417d0db69cd7e63eff022
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Eike Ziller
2024-07-17 14:15:40 +02:00
parent a5b2c648bc
commit 8a060b285a
3 changed files with 12 additions and 12 deletions

View File

@@ -52,13 +52,13 @@
translations in the .json file using the following syntax: translations in the .json file using the following syntax:
\code \code
"trDisplayName": { "C": "default", "en_US": "english", "de_DE": "deutsch" } "trDisplayName": { "C": "default", "en": "english", "de": "deutsch" }
\endcode \endcode
For example: For example:
\code \code
"trDisplayName": { "C": "Project Location", "en_US": "Project Location", "de_DE": "Projekt Verzeichnis" } "trDisplayName": { "C": "Project Location", "en": "Project Location", "de": "Projektverzeichnis" }
\endcode \endcode
\section1 Creating Wizards \section1 Creating Wizards

View File

@@ -85,7 +85,7 @@ static QString translatedOrUntranslatedText(QVariantMap &map, const QString &key
{ {
if (key.size() >= 1) { if (key.size() >= 1) {
const QString trKey = "tr" + key.at(0).toUpper() + key.mid(1); // "text" -> "trText" const QString trKey = "tr" + key.at(0).toUpper() + key.mid(1); // "text" -> "trText"
const QString trValue = JsonWizardFactory::localizedString(consumeValue(map, trKey).toString()); const QString trValue = JsonWizardFactory::localizedString(consumeValue(map, trKey));
if (!trValue.isEmpty()) if (!trValue.isEmpty())
return trValue; return trValue;
} }
@@ -188,9 +188,10 @@ JsonFieldPage::Field *JsonFieldPage::Field::parse(const QVariant &input, QString
*errorMessage = Tr::tr("Field \"%1\" has unsupported type \"%2\".").arg(name).arg(type); *errorMessage = Tr::tr("Field \"%1\" has unsupported type \"%2\".").arg(name).arg(type);
return nullptr; return nullptr;
} }
data->setTexts(name, data->setTexts(
JsonWizardFactory::localizedString(consumeValue(tmp, DISPLAY_NAME_KEY).toString()), name,
JsonWizardFactory::localizedString(consumeValue(tmp, TOOLTIP_KEY).toString())); JsonWizardFactory::localizedString(consumeValue(tmp, DISPLAY_NAME_KEY)),
JsonWizardFactory::localizedString(consumeValue(tmp, TOOLTIP_KEY)));
data->setVisibleExpression(consumeValue(tmp, VISIBLE_KEY, true)); data->setVisibleExpression(consumeValue(tmp, VISIBLE_KEY, true));
data->setEnabledExpression(consumeValue(tmp, ENABLED_KEY, true)); data->setEnabledExpression(consumeValue(tmp, ENABLED_KEY, true));
@@ -974,7 +975,7 @@ std::unique_ptr<QStandardItem> createStandardItemFromListItem(const QVariant &it
auto standardItem = std::make_unique<QStandardItem>(); auto standardItem = std::make_unique<QStandardItem>();
if (item.typeId() == QMetaType::QVariantMap) { if (item.typeId() == QMetaType::QVariantMap) {
QVariantMap tmp = item.toMap(); QVariantMap tmp = item.toMap();
const QString key = JsonWizardFactory::localizedString(consumeValue(tmp, "trKey", QString()).toString()); const QString key = JsonWizardFactory::localizedString(consumeValue(tmp, "trKey"));
const QVariant value = consumeValue(tmp, "value", key); const QVariant value = consumeValue(tmp, "value", key);
if (key.isNull() || key.isEmpty()) { if (key.isNull() || key.isEmpty()) {
@@ -985,7 +986,7 @@ std::unique_ptr<QStandardItem> createStandardItemFromListItem(const QVariant &it
standardItem->setData(value, ListField::ValueRole); standardItem->setData(value, ListField::ValueRole);
standardItem->setData(consumeValue(tmp, "condition", true), ListField::ConditionRole); standardItem->setData(consumeValue(tmp, "condition", true), ListField::ConditionRole);
standardItem->setData(consumeValue(tmp, "icon"), ListField::IconStringRole); standardItem->setData(consumeValue(tmp, "icon"), ListField::IconStringRole);
standardItem->setToolTip(JsonWizardFactory::localizedString(consumeValue(tmp, "trToolTip", QString()).toString())); standardItem->setToolTip(JsonWizardFactory::localizedString(consumeValue(tmp, "trToolTip")));
warnAboutUnsupportedKeys(tmp, QString(), "List"); warnAboutUnsupportedKeys(tmp, QString(), "List");
} else { } else {
const QString keyvalue = item.toString(); const QString keyvalue = item.toString();

View File

@@ -763,10 +763,9 @@ QString JsonWizardFactory::localizedString(const QVariant &value)
return {}; return {};
if (value.typeId() == QMetaType::QVariantMap) { if (value.typeId() == QMetaType::QVariantMap) {
QVariantMap tmp = value.toMap(); QVariantMap tmp = value.toMap();
const QString locale = languageSetting().toLower(); const QString currentLocale = languageSetting().toLower();
QStringList locales; const QStringList locales{currentLocale, "en", "C"};
locales << locale << QLatin1String("en") << QLatin1String("C") << tmp.keys(); for (const QString &locale : locales) {
for (const QString &locale : std::as_const(locales)) {
QString result = tmp.value(locale, QString()).toString(); QString result = tmp.value(locale, QString()).toString();
if (!result.isEmpty()) if (!result.isEmpty())
return result; return result;