2017-04-13 17:59:05 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2017-04-13 17:59:05 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2017-04-13 17:59:05 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2017-09-13 16:57:44 +00:00
|
|
|
#include <rpl/producer.h>
|
2017-04-13 17:59:05 +00:00
|
|
|
#include "lang_auto.h"
|
2017-11-30 17:33:27 +00:00
|
|
|
#include "base/weak_ptr.h"
|
2017-04-13 17:59:05 +00:00
|
|
|
|
|
|
|
namespace Lang {
|
|
|
|
|
|
|
|
constexpr auto kLegacyLanguageNone = -2;
|
|
|
|
constexpr auto kLegacyCustomLanguage = -1;
|
|
|
|
constexpr auto kLegacyDefaultLanguage = 0;
|
|
|
|
|
2017-06-04 13:09:53 +00:00
|
|
|
constexpr str_const kLegacyLanguages[] = {
|
|
|
|
"en",
|
|
|
|
"it",
|
|
|
|
"es",
|
|
|
|
"de",
|
|
|
|
"nl",
|
|
|
|
"pt_BR",
|
|
|
|
"ko",
|
|
|
|
};
|
|
|
|
|
|
|
|
inline QString ConvertLegacyLanguageId(const QString &languageId) {
|
|
|
|
return languageId.toLower().replace('_', '-');
|
|
|
|
}
|
|
|
|
|
2017-04-18 17:37:14 +00:00
|
|
|
QString DefaultLanguageId();
|
|
|
|
|
|
|
|
class Instance;
|
|
|
|
Instance &Current();
|
|
|
|
|
2017-09-13 16:57:44 +00:00
|
|
|
rpl::producer<QString> Viewer(LangKey key);
|
|
|
|
|
2017-05-30 17:58:25 +00:00
|
|
|
class Instance {
|
2017-04-13 17:59:05 +00:00
|
|
|
public:
|
|
|
|
Instance() {
|
|
|
|
fillDefaults();
|
|
|
|
}
|
2017-04-18 15:21:03 +00:00
|
|
|
void switchToId(const QString &id);
|
|
|
|
void switchToCustomFile(const QString &filePath);
|
|
|
|
|
2017-04-13 17:59:05 +00:00
|
|
|
Instance(const Instance &other) = delete;
|
|
|
|
Instance &operator=(const Instance &other) = delete;
|
|
|
|
Instance(Instance &&other) = default;
|
|
|
|
Instance &operator=(Instance &&other) = default;
|
|
|
|
|
2017-05-30 09:31:32 +00:00
|
|
|
QString systemLangCode() const;
|
2017-04-18 15:21:03 +00:00
|
|
|
QString cloudLangCode() const;
|
|
|
|
|
2017-04-13 17:59:05 +00:00
|
|
|
QString id() const {
|
|
|
|
return _id;
|
|
|
|
}
|
|
|
|
bool isCustom() const {
|
2017-05-30 13:54:05 +00:00
|
|
|
return (_id == qstr("custom") || _id == qstr("TEST_X") || _id == qstr("TEST_0"));
|
2017-04-13 17:59:05 +00:00
|
|
|
}
|
|
|
|
int version() const {
|
|
|
|
return _version;
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray serialize() const;
|
|
|
|
void fillFromSerialized(const QByteArray &data);
|
|
|
|
void fillFromLegacy(int legacyId, const QString &legacyPath);
|
|
|
|
|
|
|
|
void applyDifference(const MTPDlangPackDifference &difference);
|
2017-05-30 09:31:40 +00:00
|
|
|
static std::map<LangKey, QString> ParseStrings(const MTPVector<MTPLangPackString> &strings);
|
2017-04-18 15:21:03 +00:00
|
|
|
base::Observable<void> &updated() {
|
|
|
|
return _updated;
|
|
|
|
}
|
2017-04-13 17:59:05 +00:00
|
|
|
|
2017-09-13 16:57:44 +00:00
|
|
|
QString getValue(LangKey key) const {
|
2017-04-13 17:59:05 +00:00
|
|
|
Expects(key >= 0 && key < kLangKeysCount);
|
|
|
|
Expects(_values.size() == kLangKeysCount);
|
|
|
|
return _values[key];
|
|
|
|
}
|
2017-09-13 16:57:44 +00:00
|
|
|
bool isNonDefaultPlural(LangKey key) const {
|
2017-06-02 11:46:02 +00:00
|
|
|
Expects(key >= 0 && key < kLangKeysCount);
|
|
|
|
Expects(_nonDefaultSet.size() == kLangKeysCount);
|
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];
|
2017-06-02 11:46:02 +00:00
|
|
|
}
|
2017-04-13 17:59:05 +00:00
|
|
|
|
|
|
|
private:
|
2017-05-30 09:31:40 +00:00
|
|
|
// SetCallback takes two QByteArrays: key, value.
|
|
|
|
// It is called for all key-value pairs in string.
|
|
|
|
// ResetCallback takes one QByteArray: key.
|
|
|
|
template <typename SetCallback, typename ResetCallback>
|
|
|
|
static void HandleString(const MTPLangPackString &mtpString, SetCallback setCallback, ResetCallback resetCallback);
|
|
|
|
|
|
|
|
// Writes each key-value pair in the result container.
|
|
|
|
template <typename Result>
|
2017-06-02 11:46:02 +00:00
|
|
|
static LangKey ParseKeyValue(const QByteArray &key, const QByteArray &value, Result &result);
|
2017-05-30 09:31:40 +00:00
|
|
|
|
2017-04-13 17:59:05 +00:00
|
|
|
void applyValue(const QByteArray &key, const QByteArray &value);
|
|
|
|
void resetValue(const QByteArray &key);
|
2017-04-18 15:21:03 +00:00
|
|
|
void reset();
|
2017-04-13 17:59:05 +00:00
|
|
|
void fillDefaults();
|
|
|
|
void fillFromCustomFile(const QString &filePath);
|
|
|
|
void loadFromContent(const QByteArray &content);
|
|
|
|
void loadFromCustomContent(const QString &absolutePath, const QString &relativePath, const QByteArray &content);
|
2017-06-02 11:46:02 +00:00
|
|
|
void updatePluralRules();
|
2017-04-13 17:59:05 +00:00
|
|
|
|
|
|
|
QString _id;
|
|
|
|
int _legacyId = kLegacyLanguageNone;
|
|
|
|
QString _customFilePathAbsolute;
|
|
|
|
QString _customFilePathRelative;
|
|
|
|
QByteArray _customFileContent;
|
|
|
|
int _version = 0;
|
2017-04-18 15:21:03 +00:00
|
|
|
base::Observable<void> _updated;
|
|
|
|
|
2017-04-13 17:59:05 +00:00
|
|
|
mutable QString _systemLanguage;
|
|
|
|
|
|
|
|
std::vector<QString> _values;
|
2017-06-02 11:46:02 +00:00
|
|
|
std::vector<uchar> _nonDefaultSet;
|
2017-04-13 17:59:05 +00:00
|
|
|
std::map<QByteArray, QByteArray> _nonDefaultValues;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Lang
|