Remove LangKey.

This commit is contained in:
John Preston 2019-06-19 17:42:16 +02:00
parent 87fc066e67
commit edcd09c29f
16 changed files with 97 additions and 100 deletions

View File

@ -11,8 +11,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "mtproto/sender.h"
#include "apiwrap.h"
enum LangKey : int;
namespace Ui {
class VerticalLayout;
class FlatLabel;

View File

@ -15,8 +15,6 @@ class RoundButton;
class VerticalLayout;
} // namespace Ui
enum LangKey : int;
class EditPeerPermissionsBox : public BoxContent {
public:
EditPeerPermissionsBox(QWidget*, not_null<PeerData*> peer);

View File

@ -10,8 +10,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "boxes/abstract_box.h"
#include "base/timer.h"
enum LangKey : int;
namespace style {
struct InfoProfileCountButton;
} // namespace style

View File

@ -10,8 +10,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "boxes/abstract_box.h"
#include <vector>
enum LangKey : int;
namespace Ui {
class Radiobutton;
} // namespace Ui

View File

@ -106,7 +106,6 @@ bool Generator::writeHeader() {
writeHeaderForwardDeclarations();
writeHeaderTagTypes();
writeHeaderKeyType();
writeHeaderInterface();
writeHeaderReactiveInterface();
writeHeaderTaggedMethods();
@ -117,11 +116,10 @@ bool Generator::writeHeader() {
void Generator::writeHeaderForwardDeclarations() {
header_->pushNamespace("Lang").stream() << "\
\n\
inline constexpr auto kTagsCount = " << langpack_.tags.size() << ";\n\
inline constexpr ushort kTagsCount = " << langpack_.tags.size() << ";\n\
inline constexpr ushort kKeysCount = " << langpack_.entries.size() << ";\n\
\n";
header_->popNamespace().newline().stream() << "\
enum LangKey : int;\n\n";
header_->popNamespace().newline();
}
void Generator::writeHeaderTagTypes() {
@ -143,19 +141,6 @@ void Generator::writeHeaderTagTypes() {
header_->newline();
}
void Generator::writeHeaderKeyType() {
header_->stream() << "\
enum LangKey : int {\n";
for (auto &entry : langpack_.entries) {
header_->stream() << "\t" << getFullKey(entry) << ",\n";
}
header_->stream() << "\
\n\
kLangKeysCount,\n\
};\n\
\n";
}
void Generator::writeHeaderTaggedMethods() {
for (auto &entry : langpack_.entries) {
auto isPlural = !entry.keyBase.isEmpty();
@ -181,9 +166,9 @@ void Generator::writeHeaderInterface() {
header_->pushNamespace("Lang").stream() << "\
\n\
ushort GetTagIndex(QLatin1String tag);\n\
LangKey GetKeyIndex(QLatin1String key);\n\
bool IsTagReplaced(LangKey key, ushort tag);\n\
QString GetOriginalValue(LangKey key);\n\
ushort GetKeyIndex(QLatin1String key);\n\
bool IsTagReplaced(ushort key, ushort tag);\n\
QString GetOriginalValue(ushort key);\n\
\n";
writeHeaderTagValueLookup();
header_->popNamespace().newline();
@ -286,7 +271,7 @@ struct phrase<" << tags.join(", ") << "> {\n\
return ::Lang::details::Producer<" << tags.join(", ") << ">::template Current(" << values.join(", ") << ");\n\
}\n\
\n\
LangKey base;\n\
ushort base;\n\
};\n\
\n";
}
@ -304,17 +289,29 @@ void Generator::writeHeaderProducersInstances() {
}
if (!isPlural || key == ComputePluralKey(entry.keyBase, 0)) {
header_->stream() << "\
inline constexpr phrase<" << tags.join(", ") << "> " << (isPlural ? entry.keyBase : key) << "{ LangKey(" << index << ") };\n";
inline constexpr phrase<" << tags.join(", ") << "> " << (isPlural ? entry.keyBase : key) << "{ ushort(" << index << ") };\n";
}
++index;
}
header_->newline();
}
void Generator::writeSourceLangKeyConstants() {
auto index = 0;
for (auto &entry : langpack_.entries) {
source_->stream() << "constexpr auto " << getFullKey(entry) << " = ushort(" << (index++) << ");\n";
}
source_->newline();
}
bool Generator::writeSource() {
source_ = std::make_unique<common::CppFile>(basePath_ + ".cpp", project_);
source_->include("lang/lang_keys.h").pushNamespace("Lang").pushNamespace().stream() << "\
source_->include("lang/lang_keys.h").pushNamespace("Lang").pushNamespace();
writeSourceLangKeyConstants();
source_->stream() << "\
\n\
QChar DefaultData[] = {";
auto count = 0;
@ -375,7 +372,7 @@ ushort GetTagIndex(QLatin1String tag) {\n\
source_->stream() << "\
}\n\
\n\
LangKey GetKeyIndex(QLatin1String key) {\n\
ushort GetKeyIndex(QLatin1String key) {\n\
auto size = key.size();\n\
auto data = key.data();\n";
@ -397,15 +394,16 @@ LangKey GetKeyIndex(QLatin1String key) {\n\
}
}
writeSetSearch(keysSet, [&taggedKeys](const QString &key) {
writeSetSearch(keysSet, [&](const QString &key) {
auto it = taggedKeys.find(key);
return (it != taggedKeys.end()) ? it->second : key;
}, "kLangKeysCount");
}, "kKeysCount");
header_->popNamespace().newline();
source_->stream() << "\
}\n\
\n\
bool IsTagReplaced(LangKey key, ushort tag) {\n\
bool IsTagReplaced(ushort key, ushort tag) {\n\
switch (key) {\n";
auto lastWrittenPluralEntry = QString();
@ -444,9 +442,10 @@ bool IsTagReplaced(LangKey key, ushort tag) {\n\
return false;\n\
}\n\
\n\
QString GetOriginalValue(LangKey key) {\n\
Expects(key >= 0 && key < kLangKeysCount);\n\
auto offset = Offsets[key];\n\
QString GetOriginalValue(ushort key) {\n\
Expects(key >= 0 && key < kKeysCount);\n\
\n\
const auto offset = Offsets[key];\n\
return QString::fromRawData(DefaultData + offset, Offsets[key + 1] - offset);\n\
}\n\
\n";

View File

@ -31,7 +31,6 @@ public:
private:
void writeHeaderForwardDeclarations();
void writeHeaderTagTypes();
void writeHeaderKeyType();
void writeHeaderTaggedMethods();
void writeHeaderInterface();
void writeHeaderTagValueLookup();
@ -39,6 +38,8 @@ private:
void writeHeaderProducersInterface();
void writeHeaderProducersInstances();
void writeSourceLangKeyConstants();
QString getFullKey(const LangPack::Entry &entry);
template <typename ComputeResult>

View File

@ -11,8 +11,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "data/data_flags.h"
#include "data/data_notify_settings.h"
enum LangKey : int;
namespace Ui {
class EmptyUserpic;
} // namespace Ui

View File

@ -23,7 +23,6 @@ struct SendingAlbum;
enum class SendMediaType;
enum class CompressConfirm;
class MessageLinksParser;
enum LangKey : int;
namespace InlineBots {
namespace Layout {

View File

@ -149,17 +149,19 @@ void Widget::createLanguageLink() {
const auto defaultId = Lang::DefaultLanguageId();
const auto suggested = Lang::CurrentCloudManager().suggestedLanguage();
if (currentId != defaultId) {
createLink(Lang::GetOriginalValue(lng_switch_to_this), defaultId);
createLink(
Lang::GetOriginalValue(tr::lng_switch_to_this.base),
defaultId);
} else if (!suggested.isEmpty() && suggested != currentId) {
request(MTPlangpack_GetStrings(
MTP_string(Lang::CloudLangPackName()),
MTP_string(suggested),
MTP_vector<MTPstring>(1, MTP_string("lng_switch_to_this"))
)).done([=](const MTPVector<MTPLangPackString> &result) {
auto strings = Lang::Instance::ParseStrings(result);
auto it = strings.find(lng_switch_to_this);
if (it != strings.end()) {
createLink(it->second, suggested);
const auto strings = Lang::Instance::ParseStrings(result);
const auto i = strings.find(tr::lng_switch_to_this.base);
if (i != strings.end()) {
createLink(i->second, suggested);
}
}).send();
}

View File

@ -468,7 +468,7 @@ void CloudManager::switchToLanguage(const Language &data) {
)).done([=](const MTPVector<MTPLangPackString> &result) {
_switchingToLanguageRequest = 0;
const auto values = Instance::ParseStrings(result);
const auto getValue = [&](LangKey key) {
const auto getValue = [&](ushort key) {
auto it = values.find(key);
return (it == values.cend())
? GetOriginalValue(key)
@ -476,7 +476,7 @@ void CloudManager::switchToLanguage(const Language &data) {
};
const auto text = tr::lng_sure_save_language(tr::now)
+ "\n\n"
+ getValue(lng_sure_save_language);
+ getValue(tr::lng_sure_save_language.base);
Ui::show(
Box<ConfirmBox>(
text,
@ -498,15 +498,17 @@ void CloudManager::performSwitchToCustom() {
return;
}
auto filePath = result.paths.front();
Lang::FileParser loader(filePath, { lng_sure_save_language });
const auto filePath = result.paths.front();
auto loader = Lang::FileParser(
filePath,
{ tr::lng_sure_save_language.base });
if (loader.errors().isEmpty()) {
weak->request(weak->_switchingToLanguageRequest).cancel();
if (weak->canApplyWithoutRestart(qsl("#custom"))) {
weak->_langpack.switchToCustomFile(filePath);
} else {
const auto values = loader.found();
const auto getValue = [&](LangKey key) {
const auto getValue = [&](ushort key) {
const auto it = values.find(key);
return (it == values.cend())
? GetOriginalValue(key)
@ -514,7 +516,7 @@ void CloudManager::performSwitchToCustom() {
};
const auto text = tr::lng_sure_save_language(tr::now)
+ "\n\n"
+ getValue(lng_sure_save_language);
+ getValue(tr::lng_sure_save_language.base);
const auto change = [=] {
weak->_langpack.switchToCustomFile(filePath);
App::restart();

View File

@ -16,7 +16,7 @@ constexpr auto kLangFileLimit = 1024 * 1024;
} // namespace
FileParser::FileParser(const QString &file, const std::set<LangKey> &request)
FileParser::FileParser(const QString &file, const std::set<ushort> &request)
: _content(base::parse::stripComments(ReadFile(file, file)))
, _request(request) {
parse();
@ -90,7 +90,7 @@ bool FileParser::readKeyValue(const char *&from, const char *end) {
}
auto skipping = false;
auto keyIndex = kLangKeysCount;
auto keyIndex = kKeysCount;
if (!_callback) {
keyIndex = GetKeyIndex(key);
skipping = (_request.find(keyIndex) == _request.end());

View File

@ -13,9 +13,9 @@ namespace Lang {
class FileParser {
public:
using Result = QMap<LangKey, QString>;
using Result = QMap<ushort, QString>;
FileParser(const QString &file, const std::set<LangKey> &request);
FileParser(const QString &file, const std::set<ushort> &request);
FileParser(const QByteArray &content, Fn<void(QLatin1String key, const QByteArray &value)> callback);
static QByteArray ReadFile(const QString &absolutePath, const QString &relativePath);
@ -43,7 +43,7 @@ private:
mutable QString _errors, _warnings;
const QByteArray _content;
const std::set<LangKey> _request;
const std::set<ushort> _request;
const Fn<void(QLatin1String key, const QByteArray &value)> _callback;
Result _result;

View File

@ -27,9 +27,9 @@ constexpr auto kLangValuesLimit = 20000;
std::vector<QString> PrepareDefaultValues() {
auto result = std::vector<QString>();
result.reserve(kLangKeysCount);
for (auto i = 0; i != kLangKeysCount; ++i) {
result.emplace_back(GetOriginalValue(LangKey(i)));
result.reserve(kKeysCount);
for (auto i = 0; i != kKeysCount; ++i) {
result.emplace_back(GetOriginalValue(ushort(i)));
}
return result;
}
@ -38,7 +38,7 @@ class ValueParser {
public:
ValueParser(
const QByteArray &key,
LangKey keyIndex,
ushort keyIndex,
const QByteArray &value);
QString takeResult() {
@ -55,7 +55,7 @@ private:
bool readTag();
const QByteArray &_key;
LangKey _keyIndex = kLangKeysCount;
ushort _keyIndex = kKeysCount;
QLatin1String _currentTag;
ushort _currentTagIndex = 0;
@ -72,7 +72,10 @@ private:
};
ValueParser::ValueParser(const QByteArray &key, LangKey keyIndex, const QByteArray &value)
ValueParser::ValueParser(
const QByteArray &key,
ushort keyIndex,
const QByteArray &value)
: _key(key)
, _keyIndex(keyIndex)
, _currentTag("")
@ -196,7 +199,7 @@ void ParseKeyValue(
const QByteArray &value,
Save &&save) {
const auto index = GetKeyIndex(QLatin1String(key));
if (index != kLangKeysCount) {
if (index != kKeysCount) {
ValueParser parser(key, index, value);
if (parser.parse()) {
save(index, parser.takeResult());
@ -240,12 +243,12 @@ struct Instance::PrivateTag {
Instance::Instance()
: _values(PrepareDefaultValues())
, _nonDefaultSet(kLangKeysCount, 0) {
, _nonDefaultSet(kKeysCount, 0) {
}
Instance::Instance(not_null<Instance*> derived, const PrivateTag &)
: _derived(derived)
, _nonDefaultSet(kLangKeysCount, 0) {
, _nonDefaultSet(kKeysCount, 0) {
}
void Instance::switchToId(const Language &data) {
@ -298,7 +301,7 @@ void Instance::reset(const Language &data) {
_version = 0;
_nonDefaultValues.clear();
for (auto i = 0, count = int(_values.size()); i != count; ++i) {
_values[i] = GetOriginalValue(LangKey(i));
_values[i] = GetOriginalValue(ushort(i));
}
ranges::fill(_nonDefaultSet, 0);
@ -340,12 +343,14 @@ QString Instance::baseId() const {
}
QString Instance::name() const {
return _name.isEmpty() ? getValue(lng_language_name) : _name;
return _name.isEmpty()
? getValue(tr::lng_language_name.base)
: _name;
}
QString Instance::nativeName() const {
return _nativeName.isEmpty()
? getValue(lng_language_name)
? getValue(tr::lng_language_name.base)
: _nativeName;
}
@ -719,17 +724,17 @@ void Instance::applyDifferenceToMe(
}
}
std::map<LangKey, QString> Instance::ParseStrings(
std::map<ushort, QString> Instance::ParseStrings(
const MTPVector<MTPLangPackString> &strings) {
auto result = std::map<LangKey, QString>();
auto result = std::map<ushort, QString>();
for (const auto &string : strings.v) {
HandleString(string, [&](auto &&key, auto &&value) {
ParseKeyValue(key, value, [&](LangKey key, QString &&value) {
ParseKeyValue(key, value, [&](ushort key, QString &&value) {
result[key] = std::move(value);
});
}, [&](auto &&key) {
auto keyIndex = GetKeyIndex(QLatin1String(key));
if (keyIndex != kLangKeysCount) {
if (keyIndex != kKeysCount) {
result.erase(keyIndex);
}
});
@ -748,7 +753,7 @@ QString Instance::getNonDefaultValue(const QByteArray &key) const {
void Instance::applyValue(const QByteArray &key, const QByteArray &value) {
_nonDefaultValues[key] = value;
ParseKeyValue(key, value, [&](LangKey key, QString &&value) {
ParseKeyValue(key, value, [&](ushort key, QString &&value) {
_nonDefaultSet[key] = 1;
if (!_derived) {
_values[key] = std::move(value);
@ -773,7 +778,7 @@ void Instance::resetValue(const QByteArray &key) {
_nonDefaultValues.erase(key);
const auto keyIndex = GetKeyIndex(QLatin1String(key));
if (keyIndex != kLangKeysCount) {
if (keyIndex != kKeysCount) {
_nonDefaultSet[keyIndex] = 0;
if (!_derived) {
const auto base = _base
@ -794,11 +799,11 @@ Instance &Current() {
namespace details {
QString Current(LangKey key) {
QString Current(ushort key) {
return Lang::Current().getValue(key);
}
rpl::producer<QString> Viewer(LangKey key) {
rpl::producer<QString> Viewer(ushort key) {
return rpl::single(
Lang::Current().getValue(key)
) | then(base::ObservableViewer(

View File

@ -96,19 +96,19 @@ public:
void applyDifference(
Pack pack,
const MTPDlangPackDifference &difference);
static std::map<LangKey, QString> ParseStrings(
static std::map<ushort, QString> ParseStrings(
const MTPVector<MTPLangPackString> &strings);
base::Observable<void> &updated() {
return _updated;
}
QString getValue(LangKey key) const {
QString getValue(ushort key) const {
Expects(key >= 0 && key < _values.size());
return _values[key];
}
QString getNonDefaultValue(const QByteArray &key) const;
bool isNonDefaultPlural(LangKey key) const {
bool isNonDefaultPlural(ushort key) const {
Expects(key >= 0 && key + 5 < _nonDefaultSet.size());
return _nonDefaultSet[key]
@ -163,8 +163,8 @@ private:
namespace details {
QString Current(LangKey key);
rpl::producer<QString> Viewer(LangKey key);
QString Current(ushort key);
rpl::producer<QString> Viewer(ushort key);
} // namespace details
} // namespace Lang

View File

@ -963,7 +963,7 @@ PluralResult Plural(
const auto t = f;
const auto useNonDefaultPlural = (ChoosePlural != ChoosePluralDefault)
&& Lang::Current().isNonDefaultPlural(LangKey(keyBase));
&& Lang::Current().isNonDefaultPlural(keyBase);
const auto shift = (useNonDefaultPlural ? ChoosePlural : ChoosePluralDefault)(
(integer ? i : -1),
i,

View File

@ -9,7 +9,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "lang/lang_tag.h"
enum LangKey : int;
enum lngtag_count : int;
namespace Lang {
@ -20,8 +19,8 @@ inline constexpr auto kPluralCount = 6;
template <typename Tag>
inline constexpr ushort TagValue();
QString Current(LangKey key);
rpl::producer<QString> Viewer(LangKey key);
QString Current(ushort key);
rpl::producer<QString> Viewer(ushort key);
template <int Index, typename Type, typename Tuple>
Type ReplaceUnwrapTuple(Type accumulated, const Tuple &tuple) {
@ -76,7 +75,7 @@ struct Producer {
typename P,
typename T = decltype(std::declval<P>()(QString())),
typename ...Values>
static rpl::producer<T> Combine(LangKey base, P p, Values &...values) {
static rpl::producer<T> Combine(ushort base, P p, Values &...values) {
return rpl::combine(
Viewer(base),
std::move(values)...
@ -89,7 +88,7 @@ struct Producer {
typename P,
typename T = decltype(std::declval<P>()(QString())),
typename ...Values>
static T Current(LangKey base, P p, const Values &...values) {
static T Current(ushort base, P p, const Values &...values) {
return ReplaceUnwrap<Tags...>::template Call(
p(Lang::details::Current(base)),
values...);
@ -101,14 +100,14 @@ struct Producer<> {
template <
typename P,
typename T = decltype(std::declval<P>()(QString()))>
static rpl::producer<T> Combine(LangKey base, P p) {
static rpl::producer<T> Combine(ushort base, P p) {
return Viewer(base) | rpl::map(std::move(p));
}
template <
typename P,
typename T = decltype(std::declval<P>()(QString()))>
static T Current(LangKey base, P p) {
static T Current(ushort base, P p) {
return p(Lang::details::Current(base));
}
};
@ -120,18 +119,18 @@ struct Producer<lngtag_count, Tags...> {
typename T = decltype(std::declval<P>()(QString())),
typename ...Values>
static rpl::producer<T> Combine(
LangKey base,
ushort base,
P p,
lngtag_count type,
rpl::producer<float64> &count,
Values &...values) {
return rpl::combine(
Viewer(base),
Viewer(LangKey(base + 1)),
Viewer(LangKey(base + 2)),
Viewer(LangKey(base + 3)),
Viewer(LangKey(base + 4)),
Viewer(LangKey(base + 5)),
Viewer(base + 1),
Viewer(base + 2),
Viewer(base + 3),
Viewer(base + 4),
Viewer(base + 5),
std::move(count),
std::move(values)...
) | rpl::map([base, type, p = std::move(p)](auto tuple) {
@ -163,7 +162,7 @@ struct Producer<lngtag_count, Tags...> {
typename T = decltype(std::declval<P>()(QString())),
typename ...Values>
static T Current(
LangKey base,
ushort base,
P p,
lngtag_count type,
float64 count,
@ -171,7 +170,7 @@ struct Producer<lngtag_count, Tags...> {
auto plural = Plural(base, count, type);
return ReplaceUnwrap<Tags...>::template Call(
ReplaceTag<T>::Call(
p(Lang::details::Current(LangKey(base + plural.keyShift))),
p(Lang::details::Current(base + plural.keyShift)),
TagValue<lngtag_count>(),
StartReplacements<T>::Call(
std::move(plural.replacement))),