NOISSUE granular model updates for language model
This commit is contained in:
parent
ec2732ccd1
commit
4474d269cc
@ -54,6 +54,42 @@ struct Language
|
|||||||
total = translated + untranslated + fuzzy;
|
total = translated + untranslated + fuzzy;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool isOfSameNameAs(const Language& other) const
|
||||||
|
{
|
||||||
|
return key == other.key;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isIdenticalTo(const Language& other) const
|
||||||
|
{
|
||||||
|
return
|
||||||
|
(
|
||||||
|
key == other.key &&
|
||||||
|
file_name == other.file_name &&
|
||||||
|
file_size == other.file_size &&
|
||||||
|
file_sha1 == other.file_sha1 &&
|
||||||
|
translated == other.translated &&
|
||||||
|
fuzzy == other.fuzzy &&
|
||||||
|
total == other.fuzzy &&
|
||||||
|
localFileType == other.localFileType
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Language & apply(Language & other)
|
||||||
|
{
|
||||||
|
if(!isOfSameNameAs(other))
|
||||||
|
{
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
file_name = other.file_name;
|
||||||
|
file_size = other.file_size;
|
||||||
|
file_sha1 = other.file_sha1;
|
||||||
|
translated = other.translated;
|
||||||
|
fuzzy = other.fuzzy;
|
||||||
|
total = other.fuzzy;
|
||||||
|
localFileType = other.localFileType;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
QString key;
|
QString key;
|
||||||
QLocale locale;
|
QLocale locale;
|
||||||
bool updated;
|
bool updated;
|
||||||
@ -70,13 +106,14 @@ struct Language
|
|||||||
FileType localFileType = FileType::NONE;
|
FileType localFileType = FileType::NONE;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
struct TranslationsModel::Private
|
struct TranslationsModel::Private
|
||||||
{
|
{
|
||||||
QDir m_dir;
|
QDir m_dir;
|
||||||
|
|
||||||
// initial state is just english
|
// initial state is just english
|
||||||
QVector<Language> m_languages = {Language (defaultLangCode)};
|
QVector<Language> m_languages = {Language (defaultLangCode)};
|
||||||
QMap<QString, int> m_languageLookup = {{defaultLangCode, 0}};
|
|
||||||
|
|
||||||
QString m_selectedLanguage = defaultLangCode;
|
QString m_selectedLanguage = defaultLangCode;
|
||||||
std::unique_ptr<QTranslator> m_qt_translator;
|
std::unique_ptr<QTranslator> m_qt_translator;
|
||||||
@ -125,7 +162,7 @@ void TranslationsModel::indexRecieved()
|
|||||||
}
|
}
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
void readIndex(const QString & path, QVector<Language>& languages, QMap<QString, int>& languagesLookup)
|
void readIndex(const QString & path, QMap<QString, Language>& languages)
|
||||||
{
|
{
|
||||||
QByteArray data;
|
QByteArray data;
|
||||||
try
|
try
|
||||||
@ -169,8 +206,7 @@ void readIndex(const QString & path, QVector<Language>& languages, QMap<QString,
|
|||||||
lang.file_sha1 = Json::requireString(langObj, "sha1");
|
lang.file_sha1 = Json::requireString(langObj, "sha1");
|
||||||
lang.file_size = Json::requireInteger(langObj, "size");
|
lang.file_size = Json::requireInteger(langObj, "size");
|
||||||
|
|
||||||
languages.append(std::move(lang));
|
languages.insert(lang.key, lang);
|
||||||
languagesLookup[iter.key()] = index;
|
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -183,23 +219,9 @@ void readIndex(const QString & path, QVector<Language>& languages, QMap<QString,
|
|||||||
|
|
||||||
void TranslationsModel::reloadLocalFiles()
|
void TranslationsModel::reloadLocalFiles()
|
||||||
{
|
{
|
||||||
QVector<Language> languages = {Language (defaultLangCode)};
|
QMap<QString, Language> languages = {{defaultLangCode, Language(defaultLangCode)}};
|
||||||
QMap<QString, int> languageLookup = {{defaultLangCode, 0}};
|
|
||||||
|
|
||||||
readIndex(d->m_dir.absoluteFilePath("index_v2.json"), languages, languageLookup);
|
readIndex(d->m_dir.absoluteFilePath("index_v2.json"), languages);
|
||||||
auto fileTypeToString = [](FileType ft) -> QString
|
|
||||||
{
|
|
||||||
switch(ft)
|
|
||||||
{
|
|
||||||
case FileType::NONE:
|
|
||||||
return QString();
|
|
||||||
case FileType::QM:
|
|
||||||
return "QM";
|
|
||||||
case FileType::PO:
|
|
||||||
return "PO";
|
|
||||||
}
|
|
||||||
return QString();
|
|
||||||
};
|
|
||||||
auto entries = d->m_dir.entryInfoList({"mmc_*.qm", "*.po"}, QDir::Files | QDir::NoDotAndDotDot);
|
auto entries = d->m_dir.entryInfoList({"mmc_*.qm", "*.po"}, QDir::Files | QDir::NoDotAndDotDot);
|
||||||
for(auto & entry: entries)
|
for(auto & entry: entries)
|
||||||
{
|
{
|
||||||
@ -221,13 +243,12 @@ void TranslationsModel::reloadLocalFiles()
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto langIter = languageLookup.find(langCode);
|
auto langIter = languages.find(langCode);
|
||||||
if(langIter != languageLookup.end())
|
if(langIter != languages.end())
|
||||||
{
|
{
|
||||||
auto & language = languages[*langIter];
|
auto & language = *langIter;
|
||||||
if(int(fileType) > int(language.localFileType))
|
if(int(fileType) > int(language.localFileType))
|
||||||
{
|
{
|
||||||
qDebug() << "Found" << fileTypeToString(fileType) << "local file for language" << langCode;
|
|
||||||
language.localFileType = fileType;
|
language.localFileType = fileType;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -237,16 +258,50 @@ void TranslationsModel::reloadLocalFiles()
|
|||||||
{
|
{
|
||||||
Language localFound(langCode);
|
Language localFound(langCode);
|
||||||
localFound.localFileType = FileType::PO;
|
localFound.localFileType = FileType::PO;
|
||||||
languages.append(localFound);
|
languages.insert(langCode, localFound);
|
||||||
qDebug() << "Found standalone translation PO file: " << langCode;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
beginResetModel();
|
// changed and removed languages
|
||||||
d->m_languages.swap(languages);
|
for(auto iter = d->m_languages.begin(); iter != d->m_languages.end();)
|
||||||
endResetModel();
|
{
|
||||||
|
auto &language = *iter;
|
||||||
|
auto row = iter - d->m_languages.begin();
|
||||||
|
|
||||||
|
auto updatedLanguageIter = languages.find(language.key);
|
||||||
|
if(updatedLanguageIter != languages.end())
|
||||||
|
{
|
||||||
|
if(language.isIdenticalTo(*updatedLanguageIter))
|
||||||
|
{
|
||||||
|
languages.remove(language.key);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
language.apply(*updatedLanguageIter);
|
||||||
|
emit dataChanged(index(row), index(row));
|
||||||
|
languages.remove(language.key);
|
||||||
|
}
|
||||||
|
iter++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
beginRemoveRows(QModelIndex(), row, row);
|
||||||
|
iter = d->m_languages.erase(iter);
|
||||||
|
endRemoveRows();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// added languages
|
||||||
|
if(languages.isEmpty())
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
beginInsertRows(QModelIndex(), d->m_languages.size(), d->m_languages.size() + languages.size() - 1);
|
||||||
|
for(auto & language: languages)
|
||||||
|
{
|
||||||
|
d->m_languages.append(language);
|
||||||
|
}
|
||||||
|
endInsertRows();
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariant TranslationsModel::data(const QModelIndex& index, int role) const
|
QVariant TranslationsModel::data(const QModelIndex& index, int role) const
|
||||||
|
@ -237,7 +237,8 @@ void MultiMCPage::applySettings()
|
|||||||
auto s = MMC->settings();
|
auto s = MMC->settings();
|
||||||
|
|
||||||
// Language
|
// Language
|
||||||
s->set("Language", ui->languageBox->itemData(ui->languageBox->currentIndex()).toString());
|
auto langCode = ui->languageBox->itemData(ui->languageBox->currentIndex()).toString();
|
||||||
|
s->set("Language", langCode.isEmpty() ? "en" : langCode);
|
||||||
|
|
||||||
if (ui->resetNotificationsBtn->isChecked())
|
if (ui->resetNotificationsBtn->isChecked())
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user