diff --git a/src/plugins/coreplugin/externaltool.cpp b/src/plugins/coreplugin/externaltool.cpp index e93d397ffcb..8d417da6904 100644 --- a/src/plugins/coreplugin/externaltool.cpp +++ b/src/plugins/coreplugin/externaltool.cpp @@ -99,37 +99,67 @@ ExternalTool::OutputHandling ExternalTool::outputHandling() const return m_outputHandling; } -ExternalTool * ExternalTool::createFromXml(const QString &xml, QString *errorMessage) +static QStringList splitLocale(const QString &locale) { + QString value = locale; + QStringList values; + if (!value.isEmpty()) + values << value; + int index = value.indexOf(QLatin1Char('.')); + if (index >= 0) { + value = value.left(index); + if (!value.isEmpty()) + values << value; + } + index = value.indexOf(QLatin1Char('_')); + if (index >= 0) { + value = value.left(index); + if (!value.isEmpty()) + values << value; + } + return values; +} + +static void localizedText(const QStringList &locales, QXmlStreamReader *reader, int *currentLocale, QString *currentText) +{ + Q_ASSERT(reader); + Q_ASSERT(currentLocale); + Q_ASSERT(currentText); + if (reader->attributes().hasAttribute(QLatin1String(kXmlLang))) { + int index = locales.indexOf(reader->attributes().value(QLatin1String(kXmlLang)).toString()); + if (index >= 0 && (index < *currentLocale || *currentLocale < 0)) { + *currentText = reader->readElementText(); + *currentLocale = index; + } else { + reader->skipCurrentElement(); + } + } else { + if (*currentLocale < 0 && currentText->isEmpty()) { + *currentText = reader->readElementText(); + } else { + reader->skipCurrentElement(); + } + } +} + +ExternalTool * ExternalTool::createFromXml(const QString &xml, QString *errorMessage, const QString &locale) +{ + int descriptionLocale = -1; + int nameLocale = -1; + int categoryLocale = -1; + const QStringList &locales = splitLocale(locale); ExternalTool *tool = new ExternalTool; QXmlStreamReader reader(xml); + if (!reader.readNextStartElement() || reader.name() != QLatin1String(kExternalTool)) reader.raiseError(QLatin1String("Missing start element ")); while (reader.readNextStartElement()) { if (reader.name() == QLatin1String(kDescription)) { - // TODO locale check - if (!reader.attributes().hasAttribute(QLatin1String(kXmlLang)) - && tool->m_description.isEmpty()) { - tool->m_description = reader.readElementText(); - } else { - reader.skipCurrentElement(); - } + localizedText(locales, &reader, &descriptionLocale, &tool->m_description); } else if (reader.name() == QLatin1String(kDisplayName)) { - // TODO locale check - if (!reader.attributes().hasAttribute(QLatin1String(kXmlLang)) - && tool->m_displayName.isEmpty()) { - tool->m_displayName = reader.readElementText(); - } else { - reader.skipCurrentElement(); - } + localizedText(locales, &reader, &nameLocale, &tool->m_displayName); } else if (reader.name() == QLatin1String(kCategory)) { - // TODO locale check - if (!reader.attributes().hasAttribute(QLatin1String(kXmlLang)) - && tool->m_displayCategory.isEmpty()) { - tool->m_displayCategory = reader.readElementText(); - } else { - reader.skipCurrentElement(); - } + localizedText(locales, &reader, &categoryLocale, &tool->m_displayCategory); } else if (reader.name() == QLatin1String(kOrder)) { if (tool->m_order >= 0) { reader.raiseError(QLatin1String("only one element allowed")); diff --git a/src/plugins/coreplugin/externaltool.h b/src/plugins/coreplugin/externaltool.h index af573b574b0..bb45d47291a 100644 --- a/src/plugins/coreplugin/externaltool.h +++ b/src/plugins/coreplugin/externaltool.h @@ -57,7 +57,7 @@ public: QString arguments() const; QString workingDirectory() const; - static ExternalTool *createFromXml(const QString &xml, QString *errorMessage = 0); + static ExternalTool *createFromXml(const QString &xml, QString *errorMessage = 0, const QString &locale = QString()); private: QString m_description; diff --git a/tests/auto/externaltool/tst_externaltooltest.cpp b/tests/auto/externaltool/tst_externaltooltest.cpp index b509f1c66a0..33f24faad2c 100644 --- a/tests/auto/externaltool/tst_externaltooltest.cpp +++ b/tests/auto/externaltool/tst_externaltooltest.cpp @@ -53,6 +53,22 @@ static const char * const TEST_XML3 = " " ""; +static const char * const TEST_XML_LANG = +"" +" Hi" +" Hallo" +" Grüezi" +" Hallo" +" Hi" +" Grüezi" +" Grüezi" +" Hi" +" Hallo" +" " +" foo" +" " +""; + class ExternaltoolTest : public QObject { Q_OBJECT @@ -61,6 +77,7 @@ private Q_SLOTS: void testRead1(); void testRead2(); void testRead3(); + void testReadLocale(); }; void ExternaltoolTest::testRead1() @@ -79,6 +96,7 @@ void ExternaltoolTest::testRead1() QCOMPARE(tool->arguments(), QString::fromLatin1("%{CurrentProjectFilePath}")); QCOMPARE(tool->workingDirectory(), QString::fromLatin1("%{CurrentProjectPath}")); QCOMPARE(tool->outputHandling(), ExternalTool::ShowInPane); + delete tool; } void ExternaltoolTest::testRead2() @@ -96,6 +114,7 @@ void ExternaltoolTest::testRead2() QCOMPARE(tool->arguments(), QString::fromLatin1("%{CurrentSelectionFilePath}")); QCOMPARE(tool->workingDirectory(), QString::fromLatin1("%{CurrentPath}")); QCOMPARE(tool->outputHandling(), ExternalTool::ReplaceSelection); + delete tool; } void ExternaltoolTest::testRead3() @@ -113,8 +132,46 @@ void ExternaltoolTest::testRead3() QVERIFY(tool->arguments().startsWith(QLatin1String("-geom %{"))); QCOMPARE(tool->workingDirectory(), QString::fromLatin1("%{CurrentPath}")); QCOMPARE(tool->outputHandling(), ExternalTool::ReloadDocument); + delete tool; } +void ExternaltoolTest::testReadLocale() +{ + QString error; + ExternalTool *tool; + + tool = ExternalTool::createFromXml(QLatin1String(TEST_XML_LANG), &error); + QVERIFY(tool != 0); + QVERIFY(error.isEmpty()); + QCOMPARE(tool->description(), QString::fromLatin1("Hi")); + QCOMPARE(tool->displayName(), QString::fromLatin1("Hi")); + QCOMPARE(tool->displayCategory(), QString::fromLatin1("Hi")); + delete tool; + + tool = ExternalTool::createFromXml(QLatin1String(TEST_XML_LANG), &error, QLatin1String("uk")); + QVERIFY(tool != 0); + QVERIFY(error.isEmpty()); + QCOMPARE(tool->description(), QString::fromLatin1("Hi")); + QCOMPARE(tool->displayName(), QString::fromLatin1("Hi")); + QCOMPARE(tool->displayCategory(), QString::fromLatin1("Hi")); + delete tool; + + tool = ExternalTool::createFromXml(QLatin1String(TEST_XML_LANG), &error, QLatin1String("de_DE.UTF-8")); + QVERIFY(tool != 0); + QVERIFY(error.isEmpty()); + QCOMPARE(tool->description(), QString::fromLatin1("Hallo")); + QCOMPARE(tool->displayName(), QString::fromLatin1("Hallo")); + QCOMPARE(tool->displayCategory(), QString::fromLatin1("Hallo")); + delete tool; + + tool = ExternalTool::createFromXml(QLatin1String(TEST_XML_LANG), &error, QLatin1String("de_CH")); + QVERIFY(tool != 0); + QVERIFY(error.isEmpty()); + QCOMPARE(tool->description(), QString::fromLatin1("Grüezi")); + QCOMPARE(tool->displayName(), QString::fromLatin1("Grüezi")); + QCOMPARE(tool->displayCategory(), QString::fromLatin1("Grüezi")); + delete tool;} + QTEST_APPLESS_MAIN(ExternaltoolTest); #include "tst_externaltooltest.moc"