@ -3,14 +3,12 @@
|
||||
#include <QDebug>
|
||||
#include "FileSystem.h"
|
||||
|
||||
struct POEntry
|
||||
{
|
||||
struct POEntry {
|
||||
QString text;
|
||||
bool fuzzy;
|
||||
};
|
||||
|
||||
struct POTranslatorPrivate
|
||||
{
|
||||
struct POTranslatorPrivate {
|
||||
QString filename;
|
||||
QHash<QByteArray, POEntry> mapping;
|
||||
QHash<QByteArray, POEntry> mapping_disambiguatrion;
|
||||
@ -19,47 +17,37 @@ struct POTranslatorPrivate
|
||||
void reload();
|
||||
};
|
||||
|
||||
class ParserArray : public QByteArray
|
||||
{
|
||||
public:
|
||||
ParserArray(const QByteArray &in) : QByteArray(in)
|
||||
class ParserArray : public QByteArray {
|
||||
public:
|
||||
ParserArray(const QByteArray& in) : QByteArray(in) {}
|
||||
bool chomp(const char* data, int length)
|
||||
{
|
||||
}
|
||||
bool chomp(const char * data, int length)
|
||||
{
|
||||
if(startsWith(data))
|
||||
{
|
||||
if (startsWith(data)) {
|
||||
remove(0, length);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool chompString(QByteArray & appendHere)
|
||||
bool chompString(QByteArray& appendHere)
|
||||
{
|
||||
QByteArray msg;
|
||||
bool escape = false;
|
||||
if(size() < 2)
|
||||
{
|
||||
if (size() < 2) {
|
||||
qDebug() << "String fragment is too short";
|
||||
return false;
|
||||
}
|
||||
if(!startsWith('"'))
|
||||
{
|
||||
if (!startsWith('"')) {
|
||||
qDebug() << "String fragment does not start with \"";
|
||||
return false;
|
||||
}
|
||||
if(!endsWith('"'))
|
||||
{
|
||||
if (!endsWith('"')) {
|
||||
qDebug() << "String fragment does not end with \", instead, there is" << at(size() - 1);
|
||||
return false;
|
||||
}
|
||||
for(int i = 1; i < size() - 1; i++)
|
||||
{
|
||||
for (int i = 1; i < size() - 1; i++) {
|
||||
char c = operator[](i);
|
||||
if(escape)
|
||||
{
|
||||
switch(c)
|
||||
{
|
||||
if (escape) {
|
||||
switch (c) {
|
||||
case 'r':
|
||||
msg += '\r';
|
||||
break;
|
||||
@ -94,14 +82,11 @@ public:
|
||||
case '4':
|
||||
case '5':
|
||||
case '6':
|
||||
case '7':
|
||||
{
|
||||
case '7': {
|
||||
int octal_start = i;
|
||||
while ((c = operator[](i)) >= '0' && c <= '7')
|
||||
{
|
||||
while ((c = operator[](i)) >= '0' && c <= '7') {
|
||||
i++;
|
||||
if (i == length() - 1)
|
||||
{
|
||||
if (i == length() - 1) {
|
||||
qDebug() << "Something went bad while parsing an octal escape string...";
|
||||
return false;
|
||||
}
|
||||
@ -109,16 +94,13 @@ public:
|
||||
msg += mid(octal_start, i - octal_start).toUInt(0, 8);
|
||||
break;
|
||||
}
|
||||
case 'x':
|
||||
{
|
||||
case 'x': {
|
||||
// chomp the 'x'
|
||||
i++;
|
||||
int hex_start = i;
|
||||
while (isxdigit(operator[](i)))
|
||||
{
|
||||
while (isxdigit(operator[](i))) {
|
||||
i++;
|
||||
if (i == length() - 1)
|
||||
{
|
||||
if (i == length() - 1) {
|
||||
qDebug() << "Something went bad while parsing a hex escape string...";
|
||||
return false;
|
||||
}
|
||||
@ -126,25 +108,19 @@ public:
|
||||
msg += mid(hex_start, i - hex_start).toUInt(0, 16);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
default: {
|
||||
qDebug() << "Invalid escape sequence character:" << c;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
escape = false;
|
||||
}
|
||||
else if(c == '\\')
|
||||
{
|
||||
} else if (c == '\\') {
|
||||
escape = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
msg += c;
|
||||
}
|
||||
}
|
||||
if(escape)
|
||||
{
|
||||
if (escape) {
|
||||
qDebug() << "Unterminated escape sequence...";
|
||||
return false;
|
||||
}
|
||||
@ -156,8 +132,7 @@ public:
|
||||
void POTranslatorPrivate::reload()
|
||||
{
|
||||
QFile file(filename);
|
||||
if(!file.open(QFile::OpenMode::enum_type::ReadOnly | QFile::OpenMode::enum_type::Text))
|
||||
{
|
||||
if (!file.open(QFile::OpenMode::enum_type::ReadOnly | QFile::OpenMode::enum_type::Text)) {
|
||||
qDebug() << "Failed to open PO file:" << filename;
|
||||
return;
|
||||
}
|
||||
@ -169,13 +144,7 @@ void POTranslatorPrivate::reload()
|
||||
bool fuzzy = false;
|
||||
bool nextFuzzy = false;
|
||||
|
||||
enum class Mode
|
||||
{
|
||||
First,
|
||||
MessageContext,
|
||||
MessageId,
|
||||
MessageString
|
||||
} mode = Mode::First;
|
||||
enum class Mode { First, MessageContext, MessageId, MessageString } mode = Mode::First;
|
||||
|
||||
int lineNumber = 0;
|
||||
QHash<QByteArray, POEntry> newMapping;
|
||||
@ -183,14 +152,12 @@ void POTranslatorPrivate::reload()
|
||||
auto endEntry = [&]() {
|
||||
auto strStr = QString::fromUtf8(str);
|
||||
// NOTE: PO header has empty id. We skip it.
|
||||
if(!id.isEmpty())
|
||||
{
|
||||
if (!id.isEmpty()) {
|
||||
auto normalKey = context + "|" + id;
|
||||
newMapping.insert(normalKey, {strStr, fuzzy});
|
||||
if(!disambiguation.isEmpty())
|
||||
{
|
||||
newMapping.insert(normalKey, { strStr, fuzzy });
|
||||
if (!disambiguation.isEmpty()) {
|
||||
auto disambiguationKey = context + "|" + id + "@" + disambiguation;
|
||||
newMapping_disambiguation.insert(disambiguationKey, {strStr, fuzzy});
|
||||
newMapping_disambiguation.insert(disambiguationKey, { strStr, fuzzy });
|
||||
}
|
||||
}
|
||||
context.clear();
|
||||
@ -200,36 +167,26 @@ void POTranslatorPrivate::reload()
|
||||
fuzzy = nextFuzzy;
|
||||
nextFuzzy = false;
|
||||
};
|
||||
while (!file.atEnd())
|
||||
{
|
||||
while (!file.atEnd()) {
|
||||
ParserArray line = file.readLine();
|
||||
if(line.endsWith('\n'))
|
||||
{
|
||||
if (line.endsWith('\n')) {
|
||||
line.resize(line.size() - 1);
|
||||
}
|
||||
if(line.endsWith('\r'))
|
||||
{
|
||||
if (line.endsWith('\r')) {
|
||||
line.resize(line.size() - 1);
|
||||
}
|
||||
|
||||
if(!line.size())
|
||||
{
|
||||
if (!line.size()) {
|
||||
// NIL
|
||||
}
|
||||
else if(line[0] == '#')
|
||||
{
|
||||
if(line.contains(", fuzzy"))
|
||||
{
|
||||
} else if (line[0] == '#') {
|
||||
if (line.contains(", fuzzy")) {
|
||||
nextFuzzy = true;
|
||||
}
|
||||
}
|
||||
else if(line.startsWith('"'))
|
||||
{
|
||||
} else if (line.startsWith('"')) {
|
||||
QByteArray temp;
|
||||
QByteArray *out = &temp;
|
||||
QByteArray* out = &temp;
|
||||
|
||||
switch(mode)
|
||||
{
|
||||
switch (mode) {
|
||||
case Mode::First:
|
||||
qDebug() << "Unexpected escaped string during initial state... line:" << lineNumber;
|
||||
return;
|
||||
@ -243,16 +200,12 @@ void POTranslatorPrivate::reload()
|
||||
out = &id;
|
||||
break;
|
||||
}
|
||||
if(!line.chompString(*out))
|
||||
{
|
||||
if (!line.chompString(*out)) {
|
||||
qDebug() << "Badly formatted string on line:" << lineNumber;
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if(line.chomp("msgctxt ", 8))
|
||||
{
|
||||
switch(mode)
|
||||
{
|
||||
} else if (line.chomp("msgctxt ", 8)) {
|
||||
switch (mode) {
|
||||
case Mode::First:
|
||||
break;
|
||||
case Mode::MessageString:
|
||||
@ -263,21 +216,16 @@ void POTranslatorPrivate::reload()
|
||||
qDebug() << "Unexpected msgctxt line:" << lineNumber;
|
||||
return;
|
||||
}
|
||||
if(line.chompString(context))
|
||||
{
|
||||
if (line.chompString(context)) {
|
||||
auto parts = context.split('|');
|
||||
context = parts[0];
|
||||
if(parts.size() > 1 && !parts[1].isEmpty())
|
||||
{
|
||||
if (parts.size() > 1 && !parts[1].isEmpty()) {
|
||||
disambiguation = parts[1];
|
||||
}
|
||||
mode = Mode::MessageContext;
|
||||
}
|
||||
}
|
||||
else if (line.chomp("msgid ", 6))
|
||||
{
|
||||
switch(mode)
|
||||
{
|
||||
} else if (line.chomp("msgid ", 6)) {
|
||||
switch (mode) {
|
||||
case Mode::MessageContext:
|
||||
case Mode::First:
|
||||
break;
|
||||
@ -288,15 +236,11 @@ void POTranslatorPrivate::reload()
|
||||
qDebug() << "Unexpected msgid line:" << lineNumber;
|
||||
return;
|
||||
}
|
||||
if(line.chompString(id))
|
||||
{
|
||||
if (line.chompString(id)) {
|
||||
mode = Mode::MessageId;
|
||||
}
|
||||
}
|
||||
else if (line.chomp("msgstr ", 7))
|
||||
{
|
||||
switch(mode)
|
||||
{
|
||||
} else if (line.chomp("msgstr ", 7)) {
|
||||
switch (mode) {
|
||||
case Mode::First:
|
||||
case Mode::MessageString:
|
||||
case Mode::MessageContext:
|
||||
@ -305,13 +249,10 @@ void POTranslatorPrivate::reload()
|
||||
case Mode::MessageId:
|
||||
break;
|
||||
}
|
||||
if(line.chompString(str))
|
||||
{
|
||||
if (line.chompString(str)) {
|
||||
mode = Mode::MessageString;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
qDebug() << "I did not understand line: " << lineNumber << ":" << QString::fromUtf8(line);
|
||||
}
|
||||
lineNumber++;
|
||||
@ -336,19 +277,15 @@ POTranslator::~POTranslator()
|
||||
|
||||
QString POTranslator::translate(const char* context, const char* sourceText, const char* disambiguation, [[maybe_unused]] int n) const
|
||||
{
|
||||
if(disambiguation)
|
||||
{
|
||||
if (disambiguation) {
|
||||
auto disambiguationKey = QByteArray(context) + "|" + QByteArray(sourceText) + "@" + QByteArray(disambiguation);
|
||||
auto iter = d->mapping_disambiguatrion.find(disambiguationKey);
|
||||
if(iter != d->mapping_disambiguatrion.end())
|
||||
{
|
||||
auto & entry = *iter;
|
||||
if(entry.text.isEmpty())
|
||||
{
|
||||
if (iter != d->mapping_disambiguatrion.end()) {
|
||||
auto& entry = *iter;
|
||||
if (entry.text.isEmpty()) {
|
||||
qDebug() << "Translation entry has no content:" << disambiguationKey;
|
||||
}
|
||||
if(entry.fuzzy)
|
||||
{
|
||||
if (entry.fuzzy) {
|
||||
qDebug() << "Translation entry is fuzzy:" << disambiguationKey << "->" << entry.text;
|
||||
}
|
||||
return entry.text;
|
||||
@ -356,15 +293,12 @@ QString POTranslator::translate(const char* context, const char* sourceText, con
|
||||
}
|
||||
auto key = QByteArray(context) + "|" + QByteArray(sourceText);
|
||||
auto iter = d->mapping.find(key);
|
||||
if(iter != d->mapping.end())
|
||||
{
|
||||
auto & entry = *iter;
|
||||
if(entry.text.isEmpty())
|
||||
{
|
||||
if (iter != d->mapping.end()) {
|
||||
auto& entry = *iter;
|
||||
if (entry.text.isEmpty()) {
|
||||
qDebug() << "Translation entry has no content:" << key;
|
||||
}
|
||||
if(entry.fuzzy)
|
||||
{
|
||||
if (entry.fuzzy) {
|
||||
qDebug() << "Translation entry is fuzzy:" << key << "->" << entry.text;
|
||||
}
|
||||
return entry.text;
|
||||
|
Reference in New Issue
Block a user