tdesktop/Telegram/SourceFiles/lang/lang_instance.h

151 lines
3.6 KiB
C
Raw Normal View History

/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#pragma once
2017-09-13 16:57:44 +00:00
#include <rpl/producer.h>
#include "lang_auto.h"
#include "base/weak_ptr.h"
namespace Lang {
struct Language {
QString id;
QString pluralId;
QString baseId;
QString name;
QString nativeName;
};
inline bool operator==(const Language &a, const Language &b) {
return (a.id == b.id) && (a.name == b.name);
}
inline bool operator!=(const Language &a, const Language &b) {
return !(a == b);
}
QString DefaultLanguageId();
QString LanguageIdOrDefault(const QString &id);
2018-08-20 11:31:40 +00:00
QString CloudLangPackName();
QString CustomLanguageId();
Language DefaultLanguage();
class Instance;
Instance &Current();
enum class Pack {
None,
Current,
Base,
};
class Instance {
struct PrivateTag;
public:
Instance();
Instance(not_null<Instance*> derived, const PrivateTag &);
void switchToId(const Language &language);
void switchToCustomFile(const QString &filePath);
Instance(const Instance &other) = delete;
Instance &operator=(const Instance &other) = delete;
Instance(Instance &&other) = default;
Instance &operator=(Instance &&other) = default;
QString systemLangCode() const;
2018-08-20 11:31:40 +00:00
QString langPackName() const;
QString cloudLangCode(Pack pack) const;
QString id() const;
2019-06-19 11:22:25 +00:00
rpl::producer<QString> idChanges() const;
QString baseId() const;
QString name() const;
QString nativeName() const;
QString id(Pack pack) const;
bool isCustom() const;
int version(Pack pack) const;
QByteArray serialize() const;
void fillFromSerialized(const QByteArray &data, int dataAppVersion);
void applyDifference(
Pack pack,
const MTPDlangPackDifference &difference);
2019-06-19 15:42:16 +00:00
static std::map<ushort, QString> ParseStrings(
2018-08-20 11:31:40 +00:00
const MTPVector<MTPLangPackString> &strings);
base::Observable<void> &updated() {
return _updated;
}
2019-06-19 15:42:16 +00:00
QString getValue(ushort key) const {
2019-06-21 08:58:31 +00:00
Expects(key < _values.size());
2018-08-20 11:31:40 +00:00
return _values[key];
}
QString getNonDefaultValue(const QByteArray &key) const;
2019-06-19 15:42:16 +00:00
bool isNonDefaultPlural(ushort key) const {
2019-06-21 08:58:31 +00:00
Expects(key + 5 < _nonDefaultSet.size());
2018-08-20 11:31:40 +00:00
2017-09-13 16:57:44 +00:00
return _nonDefaultSet[key]
|| _nonDefaultSet[key + 1]
|| _nonDefaultSet[key + 2]
|| _nonDefaultSet[key + 3]
|| _nonDefaultSet[key + 4]
|| _nonDefaultSet[key + 5]
|| (_base && _base->isNonDefaultPlural(key));
}
private:
void setBaseId(const QString &baseId, const QString &pluralId);
2017-05-30 09:31:40 +00:00
void applyDifferenceToMe(const MTPDlangPackDifference &difference);
void applyValue(const QByteArray &key, const QByteArray &value);
void resetValue(const QByteArray &key);
void reset(const Language &language);
void fillFromCustomContent(
const QString &absolutePath,
const QString &relativePath,
const QByteArray &content);
bool loadFromCustomFile(const QString &filePath);
void loadFromContent(const QByteArray &content);
2018-08-20 11:31:40 +00:00
void loadFromCustomContent(
const QString &absolutePath,
const QString &relativePath,
const QByteArray &content);
void updatePluralRules();
Instance *_derived = nullptr;
QString _id, _pluralId;
2019-06-19 11:22:25 +00:00
rpl::event_stream<QString> _idChanges;
QString _name, _nativeName;
QString _customFilePathAbsolute;
QString _customFilePathRelative;
QByteArray _customFileContent;
int _version = 0;
base::Observable<void> _updated;
mutable QString _systemLanguage;
std::vector<QString> _values;
std::vector<uchar> _nonDefaultSet;
std::map<QByteArray, QByteArray> _nonDefaultValues;
std::unique_ptr<Instance> _base;
};
2019-06-18 12:16:43 +00:00
namespace details {
2019-06-19 15:42:16 +00:00
QString Current(ushort key);
rpl::producer<QString> Viewer(ushort key);
2019-06-18 12:16:43 +00:00
} // namespace details
} // namespace Lang