mirror of
https://github.com/telegramdesktop/tdesktop
synced 2025-10-13 13:58:04 +00:00
Move lang keys to td_lang library.
This commit is contained in:
@@ -8,6 +8,11 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||
#include "lang/lang_file_parser.h"
|
||||
|
||||
#include "base/parse_helper.h"
|
||||
#include "ui/integration.h"
|
||||
|
||||
#include <QtCore/QTextStream>
|
||||
#include <QtCore/QFile>
|
||||
#include <QtCore/QFileInfo>
|
||||
|
||||
namespace Lang {
|
||||
namespace {
|
||||
@@ -30,7 +35,7 @@ FileParser::FileParser(const QByteArray &content, Fn<void(QLatin1String key, con
|
||||
|
||||
void FileParser::parse() {
|
||||
if (_content.isEmpty()) {
|
||||
error(qsl("Got empty lang file content"));
|
||||
error(u"Got empty lang file content"_q);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -72,21 +77,21 @@ bool FileParser::readKeyValue(const char *&from, const char *end) {
|
||||
auto key = QLatin1String(nameStart, from - nameStart);
|
||||
|
||||
if (from == end || *from != '"') {
|
||||
return error(qsl("Expected quote after key name '%1'!").arg(key));
|
||||
return error(u"Expected quote after key name '%1'!"_q.arg(key));
|
||||
}
|
||||
++from;
|
||||
|
||||
if (!skipWhitespaces(from, end)) {
|
||||
return error(qsl("Unexpected end of file in key '%1'!").arg(key));
|
||||
return error(u"Unexpected end of file in key '%1'!"_q.arg(key));
|
||||
}
|
||||
if (*from != '=') {
|
||||
return error(qsl("'=' expected in key '%1'!").arg(key));
|
||||
return error(u"'=' expected in key '%1'!"_q.arg(key));
|
||||
}
|
||||
if (!skipWhitespaces(++from, end)) {
|
||||
return error(qsl("Unexpected end of file in key '%1'!").arg(key));
|
||||
return error(u"Unexpected end of file in key '%1'!"_q.arg(key));
|
||||
}
|
||||
if (*from != '"') {
|
||||
return error(qsl("Expected string after '=' in key '%1'!").arg(key));
|
||||
return error(u"Expected string after '=' in key '%1'!"_q.arg(key));
|
||||
}
|
||||
|
||||
auto skipping = false;
|
||||
@@ -105,11 +110,11 @@ bool FileParser::readKeyValue(const char *&from, const char *end) {
|
||||
const char *start = ++from;
|
||||
while (from < end && *from != '"') {
|
||||
if (*from == '\n') {
|
||||
return error(qsl("Unexpected end of string in key '%1'!").arg(key));
|
||||
return error(u"Unexpected end of string in key '%1'!"_q.arg(key));
|
||||
}
|
||||
if (*from == '\\') {
|
||||
if (from + 1 >= end) {
|
||||
return error(qsl("Unexpected end of file in key '%1'!").arg(key));
|
||||
return error(u"Unexpected end of file in key '%1'!"_q.arg(key));
|
||||
}
|
||||
if (*(from + 1) == '"' || *(from + 1) == '\\') {
|
||||
if (from > start) appendValue(start, from - start);
|
||||
@@ -123,17 +128,17 @@ bool FileParser::readKeyValue(const char *&from, const char *end) {
|
||||
++from;
|
||||
}
|
||||
if (from >= end) {
|
||||
return error(qsl("Unexpected end of file in key '%1'!").arg(key));
|
||||
return error(u"Unexpected end of file in key '%1'!"_q.arg(key));
|
||||
}
|
||||
if (from > start) {
|
||||
appendValue(start, from - start);
|
||||
}
|
||||
|
||||
if (!skipWhitespaces(++from, end)) {
|
||||
return error(qsl("Unexpected end of file in key '%1'!").arg(key));
|
||||
return error(u"Unexpected end of file in key '%1'!"_q.arg(key));
|
||||
}
|
||||
if (*from != ';') {
|
||||
return error(qsl("';' expected after \"value\" in key '%1'!").arg(key));
|
||||
return error(u"';' expected after \"value\" in key '%1'!"_q.arg(key));
|
||||
}
|
||||
|
||||
skipWhitespaces(++from, end);
|
||||
@@ -150,18 +155,18 @@ bool FileParser::readKeyValue(const char *&from, const char *end) {
|
||||
QByteArray FileParser::ReadFile(const QString &absolutePath, const QString &relativePath) {
|
||||
QFile file(QFileInfo(relativePath).exists() ? relativePath : absolutePath);
|
||||
if (!file.open(QIODevice::ReadOnly)) {
|
||||
LOG(("Lang Error: Could not open file at '%1' ('%2')").arg(relativePath).arg(absolutePath));
|
||||
Ui::Integration::Instance().writeLogEntry(u"Lang Error: Could not open file at '%1' ('%2')"_q.arg(relativePath).arg(absolutePath));
|
||||
return QByteArray();
|
||||
}
|
||||
if (file.size() > kLangFileLimit) {
|
||||
LOG(("Lang Error: File is too big: %1").arg(file.size()));
|
||||
Ui::Integration::Instance().writeLogEntry(u"Lang Error: File is too big: %1"_q.arg(file.size()));
|
||||
return QByteArray();
|
||||
}
|
||||
|
||||
constexpr auto kCodecMagicSize = 3;
|
||||
auto codecMagic = file.read(kCodecMagicSize);
|
||||
if (codecMagic.size() < kCodecMagicSize) {
|
||||
LOG(("Lang Error: Found bad file at '%1' ('%2')").arg(relativePath).arg(absolutePath));
|
||||
Ui::Integration::Instance().writeLogEntry(u"Lang Error: Found bad file at '%1' ('%2')"_q.arg(relativePath).arg(absolutePath));
|
||||
return QByteArray();
|
||||
}
|
||||
file.seek(0);
|
||||
@@ -172,11 +177,11 @@ QByteArray FileParser::ReadFile(const QString &absolutePath, const QString &rela
|
||||
stream.setCodec("UTF-16");
|
||||
auto string = stream.readAll();
|
||||
if (stream.status() != QTextStream::Ok) {
|
||||
LOG(("Lang Error: Could not read UTF-16 data from '%1' ('%2')").arg(relativePath).arg(absolutePath));
|
||||
Ui::Integration::Instance().writeLogEntry(u"Lang Error: Could not read UTF-16 data from '%1' ('%2')"_q.arg(relativePath).arg(absolutePath));
|
||||
return QByteArray();
|
||||
}
|
||||
if (string.isEmpty()) {
|
||||
LOG(("Lang Error: Empty UTF-16 content in '%1' ('%2')").arg(relativePath).arg(absolutePath));
|
||||
Ui::Integration::Instance().writeLogEntry(u"Lang Error: Empty UTF-16 content in '%1' ('%2')"_q.arg(relativePath).arg(absolutePath));
|
||||
return QByteArray();
|
||||
}
|
||||
return string.toUtf8();
|
||||
@@ -192,7 +197,7 @@ QByteArray FileParser::ReadFile(const QString &absolutePath, const QString &rela
|
||||
data = data.mid(3); // skip UTF-8 BOM
|
||||
}
|
||||
if (data.isEmpty()) {
|
||||
LOG(("Lang Error: Empty UTF-8 content in '%1' ('%2')").arg(relativePath).arg(absolutePath));
|
||||
Ui::Integration::Instance().writeLogEntry(u"Lang Error: Empty UTF-8 content in '%1' ('%2')"_q.arg(relativePath).arg(absolutePath));
|
||||
return QByteArray();
|
||||
}
|
||||
return data;
|
||||
|
Reference in New Issue
Block a user