File indexing completed on 2024-11-03 13:13:06
0001 /* 0002 SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 #include "messagetranslation.h" 0007 #include <QJsonArray> 0008 #include <QJsonObject> 0009 0010 MessageTranslation::MessageTranslation() = default; 0011 0012 QDebug operator<<(QDebug d, const MessageTranslation &t) 0013 { 0014 d.space() << "translate string" << t.translatedString(); 0015 return d; 0016 } 0017 0018 void MessageTranslation::parse(const QJsonObject &obj) 0019 { 0020 mTranslatedString.clear(); 0021 const QJsonObject languageObject = obj[QLatin1String("translations")].toObject(); 0022 const QStringList keys = languageObject.keys(); 0023 for (const QString &lang : keys) { 0024 mTranslatedString.insert(lang, languageObject.value(lang).toString()); 0025 } 0026 // qDebug() << " void MessageTranslation::parse(const QJsonObject &obj)"<<mTranslatedString; 0027 } 0028 0029 bool MessageTranslation::operator==(const MessageTranslation &other) const 0030 { 0031 return mTranslatedString == other.translatedString(); 0032 } 0033 0034 QMap<QString, QString> MessageTranslation::translatedString() const 0035 { 0036 return mTranslatedString; 0037 } 0038 0039 void MessageTranslation::setTranslatedString(const QMap<QString, QString> &translatedString) 0040 { 0041 mTranslatedString = translatedString; 0042 } 0043 0044 QString MessageTranslation::translatedStringFromLanguage(const QString &lang) 0045 { 0046 return mTranslatedString.value(lang); 0047 } 0048 0049 QJsonArray MessageTranslation::serialize(const MessageTranslation &translation) 0050 { 0051 QJsonArray array; 0052 QMapIterator<QString, QString> i(translation.mTranslatedString); 0053 while (i.hasNext()) { 0054 i.next(); 0055 QJsonObject obj; 0056 obj.insert(i.key(), i.value()); 0057 array.append(obj); 0058 } 0059 return array; 0060 } 0061 0062 MessageTranslation MessageTranslation::deserialize(const QJsonArray &array) 0063 { 0064 MessageTranslation translationMessage; 0065 QMap<QString, QString> translationStrings; 0066 for (int i = 0, total = array.count(); i < total; ++i) { 0067 QJsonObject o = array.at(i).toObject(); 0068 if (o.count() == 1) { 0069 translationStrings.insert(o.keys().at(0), o.value(o.keys().at(0)).toString()); 0070 } 0071 } 0072 translationMessage.setTranslatedString(translationStrings); 0073 return translationMessage; 0074 } 0075 0076 bool MessageTranslation::isEmpty() const 0077 { 0078 return mTranslatedString.isEmpty(); 0079 }