forked from qt-creator/qt-creator
projectexplorer: add availableQmlPreviewTranslations()
Preparation for a test translations feature. Change-Id: I1a7ccecab803f5838cd765b7dca99bcf5bb9e8a1 Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
@@ -983,6 +983,21 @@ QVariant Project::extraData(const QString &key) const
|
|||||||
return d->m_extraData.value(key);
|
return d->m_extraData.value(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QStringList Project::availableQmlPreviewTranslations(QString *errorMessage)
|
||||||
|
{
|
||||||
|
const auto projectDirectory = rootProjectDirectory().toFileInfo().absoluteFilePath();
|
||||||
|
const QDir languageDirectory(projectDirectory + "/i18n");
|
||||||
|
const auto qmFiles = languageDirectory.entryList({"qml_*.qm"});
|
||||||
|
if (qmFiles.isEmpty() && errorMessage)
|
||||||
|
errorMessage->append(tr("Could not find any qml_*.qm file at '%1'").arg(languageDirectory.absolutePath()));
|
||||||
|
return Utils::transform(qmFiles, [](const QString &qmFile) {
|
||||||
|
const int localeStartPosition = qmFile.lastIndexOf("_") + 1;
|
||||||
|
const int localeEndPosition = qmFile.size() - QString(".qm").size();
|
||||||
|
const QString locale = qmFile.left(localeEndPosition).mid(localeStartPosition);
|
||||||
|
return locale;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
#if defined(WITH_TESTS)
|
#if defined(WITH_TESTS)
|
||||||
|
|
||||||
} // namespace ProjectExplorer
|
} // namespace ProjectExplorer
|
||||||
|
@@ -173,6 +173,8 @@ public:
|
|||||||
void setExtraData(const QString &key, const QVariant &data);
|
void setExtraData(const QString &key, const QVariant &data);
|
||||||
QVariant extraData(const QString &key) const;
|
QVariant extraData(const QString &key) const;
|
||||||
|
|
||||||
|
QStringList availableQmlPreviewTranslations(QString *errorMessage);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void projectFileIsDirty(const Utils::FilePath &path);
|
void projectFileIsDirty(const Utils::FilePath &path);
|
||||||
|
|
||||||
|
@@ -221,15 +221,21 @@ SwitchLanguageComboboxAction::SwitchLanguageComboboxAction(QObject *parent)
|
|||||||
QWidget *SwitchLanguageComboboxAction::createWidget(QWidget *parent)
|
QWidget *SwitchLanguageComboboxAction::createWidget(QWidget *parent)
|
||||||
{
|
{
|
||||||
QPointer<QComboBox> comboBox = new QComboBox(parent);
|
QPointer<QComboBox> comboBox = new QComboBox(parent);
|
||||||
comboBox->setToolTip(tr("Switch the language used by preview."));
|
const QString toolTip(tr("Switch the language used by preview."));
|
||||||
|
comboBox->setToolTip(toolTip);
|
||||||
comboBox->addItem(tr("Default"));
|
comboBox->addItem(tr("Default"));
|
||||||
|
|
||||||
auto refreshComboBoxFunction = [this, comboBox] (ProjectExplorer::Project *project) {
|
auto refreshComboBoxFunction = [this, comboBox, toolTip] (ProjectExplorer::Project *project) {
|
||||||
if (comboBox) {
|
if (comboBox) {
|
||||||
if (updateProjectLocales(project)) {
|
QString errorMessage;
|
||||||
|
auto locales = project->availableQmlPreviewTranslations(&errorMessage);
|
||||||
|
if (!errorMessage.isEmpty())
|
||||||
|
comboBox->setToolTip(QString("%1<br/>(%2)").arg(toolTip, errorMessage));
|
||||||
|
if (m_previousLocales != locales) {
|
||||||
comboBox->clear();
|
comboBox->clear();
|
||||||
comboBox->addItem(tr("Default"));
|
comboBox->addItem(tr("Default"));
|
||||||
comboBox->addItems(m_localeStrings);
|
comboBox->addItems(locales);
|
||||||
|
m_previousLocales = locales;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -250,24 +256,6 @@ QWidget *SwitchLanguageComboboxAction::createWidget(QWidget *parent)
|
|||||||
return comboBox;
|
return comboBox;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SwitchLanguageComboboxAction::updateProjectLocales(Project *project)
|
|
||||||
{
|
|
||||||
if (!project)
|
|
||||||
return false;
|
|
||||||
auto previousLocales = m_localeStrings;
|
|
||||||
m_localeStrings.clear();
|
|
||||||
const auto projectDirectory = project->rootProjectDirectory().toFileInfo().absoluteFilePath();
|
|
||||||
const QDir languageDirectory(projectDirectory + "/i18n");
|
|
||||||
const auto qmFiles = languageDirectory.entryList({"qml_*.qm"});
|
|
||||||
m_localeStrings = Utils::transform(qmFiles, [](const QString &qmFile) {
|
|
||||||
const int localeStartPosition = qmFile.lastIndexOf("_") + 1;
|
|
||||||
const int localeEndPosition = qmFile.size() - QString(".qm").size();
|
|
||||||
const QString locale = qmFile.left(localeEndPosition).mid(localeStartPosition);
|
|
||||||
return locale;
|
|
||||||
});
|
|
||||||
return previousLocales != m_localeStrings;
|
|
||||||
}
|
|
||||||
|
|
||||||
SwitchLanguageAction::SwitchLanguageAction()
|
SwitchLanguageAction::SwitchLanguageAction()
|
||||||
: m_switchLanguageAction(new SwitchLanguageComboboxAction(nullptr))
|
: m_switchLanguageAction(new SwitchLanguageComboboxAction(nullptr))
|
||||||
{
|
{
|
||||||
|
@@ -113,8 +113,7 @@ signals:
|
|||||||
protected:
|
protected:
|
||||||
QWidget *createWidget(QWidget *parent) override;
|
QWidget *createWidget(QWidget *parent) override;
|
||||||
private:
|
private:
|
||||||
bool updateProjectLocales(ProjectExplorer::Project *project);
|
QStringList m_previousLocales;
|
||||||
QStringList m_localeStrings;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class SwitchLanguageAction : public ActionInterface
|
class SwitchLanguageAction : public ActionInterface
|
||||||
|
Reference in New Issue
Block a user