2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-08-31 14:45:14 +00:00

Start cloud langpack support.

Change the way langpacks are stored.
Support custom langpacks in the new storage.
This commit is contained in:
John Preston
2017-04-13 20:59:05 +03:00
parent 2334ba1fe1
commit 139d4e72b5
43 changed files with 1264 additions and 741 deletions

View File

@@ -118,14 +118,6 @@ QRect computeSourceRect(const QImage &image) {
return result;
}
QString computeId(Id id) {
auto idAsParams = QStringList();
for (auto i = 0, size = id.size(); i != size; ++i) {
idAsParams.push_back("0x" + QString::number(id[i].unicode(), 16));
}
return "internal::ComputeId(" + idAsParams.join(", ") + ")";
}
} // namespace
Generator::Generator(const Options &options) : project_(Project)
@@ -305,20 +297,11 @@ EmojiPtr Find(const QChar *start, const QChar *end, int *outLength) {\n\
return index ? &Items[index - 1] : nullptr;\n\
}\n\
\n\
inline QString ComputeId(gsl::span<ushort> utf16) {\n\
auto result = QString();\n\
result.reserve(utf16.size());\n\
for (auto ch : utf16) {\n\
result.append(QChar(ch));\n\
}\n\
return result;\n\
}\n\
\n\
void Init() {\n\
auto id = IdData;\n\
Items.reserve(base::array_size(Data));\n\
for (auto &data : Data) {\n\
Items.emplace_back(ComputeId(gsl::make_span(id, data.idSize)), data.column, data.row, data.postfixed, data.variated, data.original ? &Items[data.original - 1] : nullptr, One::CreationTag());\n\
Items.emplace_back(QString::fromRawData(id, data.idSize), data.column, data.row, data.postfixed, data.variated, data.original ? &Items[data.original - 1] : nullptr, One::CreationTag());\n\
id += data.idSize;\n\
}\n\
}\n\
@@ -429,7 +412,7 @@ struct DataStruct {\n\
bool variated;\n\
};\n\
\n\
ushort IdData[] = {";
QChar IdData[] = {";
auto count = 0;
auto fulllength = 0;
if (!enumerateWholeList([this, &count, &fulllength](Id id, int column, int row, bool isPostfixed, bool isVariated, bool isColored, int original) {

View File

@@ -24,7 +24,6 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include <functional>
#include <QtCore/QDir>
#include <QtCore/QSet>
#include <QtCore/QBuffer>
#include <QtGui/QImage>
#include <QtGui/QPainter>
@@ -109,7 +108,7 @@ QString stringToBinaryArray(const std::string &str) {
} // namespace
Generator::Generator(const Langpack &langpack, const QString &destBasePath, const common::ProjectInfo &project)
Generator::Generator(const LangPack &langpack, const QString &destBasePath, const common::ProjectInfo &project)
: langpack_(langpack)
, basePath_(destBasePath)
, baseName_(QFileInfo(basePath_).baseName())
@@ -141,8 +140,6 @@ enum LangKey {\n";
};\n\
\n\
QString lang(LangKey key);\n\
\n\
QString langOriginal(LangKey key);\n\
\n";
for (auto &entry : langpack_.entries) {
if (!entry.tags.empty()) {
@@ -172,7 +169,7 @@ ushort GetTagIndex(QLatin1String tag);\n\
LangKey GetKeyIndex(QLatin1String key);\n\
LangKey GetSubkeyIndex(LangKey key, ushort tag, ushort index);\n\
bool IsTagReplaced(LangKey key, ushort tag);\n\
void FeedKeyValue(LangKey key, const QString &value);\n\
QString GetOriginalValue(LangKey key);\n\
\n";
return header_->finalize();
@@ -191,26 +188,47 @@ const char *KeyNames[kLangKeysCount] = {\n\
\n\
};\n\
\n\
QString Values[kLangKeysCount], OriginalValues[kLangKeysCount];\n\
\n\
void set(LangKey key, const QString &val) {\n\
Values[key] = val;\n\
}\n\
\n\
class Initializer {\n\
public:\n\
Initializer() {\n";
QChar DefaultData[] = {";
auto count = 0;
auto fulllength = 0;
for (auto &entry : langpack_.entries) {
source_->stream() << "\t\tset(" << getFullKey(entry) << ", QString::fromUtf8(" << stringToEncodedString(entry.value) << "));\n";
for (auto ch : entry.value) {
if (fulllength > 0) source_->stream() << ",";
if (!count++) {
source_->stream() << "\n";
} else {
if (count == 12) {
count = 0;
}
source_->stream() << " ";
}
source_->stream() << "0x" << QString::number(ch.unicode(), 16);
++fulllength;
}
}
source_->stream() << "\
}\n\
source_->stream() << " };\n\
\n\
};\n\
\n\
Initializer Instance;\n\
\n";
int Offsets[] = {";
count = 0;
auto offset = 0;
auto writeOffset = [this, &count, &offset] {
if (offset > 0) source_->stream() << ",";
if (!count++) {
source_->stream() << "\n";
} else {
if (count == 12) {
count = 0;
}
source_->stream() << " ";
}
source_->stream() << offset;
};
for (auto &entry : langpack_.entries) {
writeOffset();
offset += entry.value.size();
}
writeOffset();
source_->stream() << " };\n";
source_->popNamespace().stream() << "\
\n\
const char *GetKeyName(LangKey key) {\n\
@@ -314,25 +332,13 @@ bool IsTagReplaced(LangKey key, ushort tag) {\n\
return false;\n\
}\n\
\n\
void FeedKeyValue(LangKey key, const QString &value) {\n\
QString GetOriginalValue(LangKey key) {\n\
Expects(key >= 0 && key < kLangKeysCount);\n\
if (OriginalValues[key].isEmpty()) {\n\
OriginalValues[key] = Values[key].isEmpty() ? qsl(\"{}\") : Values[key];\n\
}\n\
Values[key] = value;\n\
auto offset = Offsets[key];\n\
return QString::fromRawData(DefaultData + offset, Offsets[key + 1] - offset);\n\
}\n\
\n";
source_->popNamespace().stream() << "\
\n\
QString lang(LangKey key) {\n\
return (key < 0 || key >= kLangKeysCount) ? QString() : Lang::Values[key];\n\
}\n\
\n\
QString langOriginal(LangKey key) {\n\
return (key < 0 || key >= kLangKeysCount || Lang::OriginalValues[key] == qsl(\"{}\")) ? QString() : (Lang::OriginalValues[key].isEmpty() ? Lang::Values[key] : Lang::OriginalValues[key]);\n\
}\n";
return source_->finalize();
}
@@ -473,7 +479,7 @@ void Generator::writeSetSearch(const std::set<QString, std::greater<QString>> &s
return " << invalidResult << ";\n";
}
QString Generator::getFullKey(const Langpack::Entry &entry) {
QString Generator::getFullKey(const LangPack::Entry &entry) {
if (entry.tags.empty()) {
return entry.key;
}

View File

@@ -34,7 +34,7 @@ namespace lang {
class Generator {
public:
Generator(const Langpack &langpack, const QString &destBasePath, const common::ProjectInfo &project);
Generator(const LangPack &langpack, const QString &destBasePath, const common::ProjectInfo &project);
Generator(const Generator &other) = delete;
Generator &operator=(const Generator &other) = delete;
@@ -42,13 +42,13 @@ public:
bool writeSource();
private:
QString getFullKey(const Langpack::Entry &entry);
QString getFullKey(const LangPack::Entry &entry);
bool isTagPlural(const QString &key, const QString &tag) const;
template <typename ComputeResult>
void writeSetSearch(const std::set<QString, std::greater<QString>> &set, ComputeResult computeResult, const QString &invalidResult);
const Langpack &langpack_;
const LangPack &langpack_;
QString basePath_, baseName_;
const common::ProjectInfo &project_;
std::unique_ptr<common::CppFile> source_, header_;

View File

@@ -128,7 +128,7 @@ common::LogStream ParsedFile::logErrorBadString() {
return logError(kErrorBadString);
}
QString ParsedFile::extractTagsData(const QString &value, Langpack *to) {
QString ParsedFile::extractTagsData(const QString &value, LangPack *to) {
auto tagStart = value.indexOf('{');
if (tagStart < 0) {
return value;
@@ -157,7 +157,7 @@ QString ParsedFile::extractTagsData(const QString &value, Langpack *to) {
return finalValue;
}
QString ParsedFile::extractTagData(const QString &tagText, Langpack *to) {
QString ParsedFile::extractTagData(const QString &tagText, LangPack *to) {
auto numericPart = tagText.indexOf(':');
auto tag = (numericPart > 0) ? tagText.mid(0, numericPart) : tagText;
if (!ValidateTag(tag)) {
@@ -190,7 +190,7 @@ QString ParsedFile::extractTagData(const QString &tagText, Langpack *to) {
}
auto index = 0;
for (auto &part : numericParts) {
auto numericPartEntry = Langpack::Entry();
auto numericPartEntry = LangPack::Entry();
numericPartEntry.key = tag + QString::number(index++);
if (part.indexOf('#') != part.lastIndexOf('#')) {
logErrorBadString() << "bad option for plural key part in tag: '" << tagText.toStdString() << "', too many '#'.";
@@ -211,14 +211,14 @@ void ParsedFile::addEntity(const QString &key, const QString &value) {
return;
}
}
auto tagsData = Langpack();
auto entry = Langpack::Entry();
auto tagsData = LangPack();
auto entry = LangPack::Entry();
entry.key = key;
entry.value = extractTagsData(value, &tagsData);
entry.tags = tagsData.tags;
result_.entries.push_back(entry);
for (auto &pluralEntry : tagsData.entries) {
auto taggedEntry = Langpack::Entry();
auto taggedEntry = LangPack::Entry();
taggedEntry.key = key + "__" + pluralEntry.key;
taggedEntry.value = pluralEntry.value;
result_.entries.push_back(taggedEntry);

View File

@@ -30,7 +30,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
namespace codegen {
namespace lang {
struct Langpack {
struct LangPack {
struct Tag {
QString tag;
};
@@ -53,7 +53,7 @@ public:
bool read();
Langpack getResult() {
LangPack getResult() {
return result_;
}
@@ -84,14 +84,14 @@ private:
BasicToken assertNextToken(BasicToken::Type type);
void addEntity(const QString &key, const QString &value);
QString extractTagsData(const QString &value, Langpack *to);
QString extractTagData(const QString &tag, Langpack *to);
QString extractTagsData(const QString &value, LangPack *to);
QString extractTagData(const QString &tag, LangPack *to);
QString filePath_;
common::BasicTokenizedFile file_;
Options options_;
bool failed_ = false;
Langpack result_;
LangPack result_;
};

View File

@@ -51,7 +51,7 @@ int Processor::launch() {
return 0;
}
bool Processor::write(const Langpack &langpack) const {
bool Processor::write(const LangPack &langpack) const {
bool forceReGenerate = false;
QDir dir(options_.outputPath);
if (!dir.mkpath(".")) {

View File

@@ -27,7 +27,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
namespace codegen {
namespace lang {
class ParsedFile;
struct Langpack;
struct LangPack;
// Walks through a file, parses it and generates the output.
class Processor {
@@ -42,7 +42,7 @@ public:
~Processor();
private:
bool write(const Langpack &langpack) const;
bool write(const LangPack &langpack) const;
std::unique_ptr<ParsedFile> parser_;
const Options &options_;