translations: multi-line and special symbols support

This commit is contained in:
2024-11-03 18:12:32 +03:00
parent 1106cde3e4
commit 73ed51e3d4
4 changed files with 44 additions and 19 deletions

View File

@@ -19,13 +19,14 @@
#include "pisystemmonitor.h" #include "pisystemmonitor.h"
#include <unistd.h>
#include "pidir.h" #include "pidir.h"
#include "piliterals_string.h" #include "piliterals_string.h"
#include "piprocess.h" #include "piprocess.h"
#include "pisysteminfo.h" #include "pisysteminfo.h"
#include "pitime_win.h" #include "pitime_win.h"
#include "pitranslator.h"
#include <unistd.h>
#ifdef WINDOWS #ifdef WINDOWS
# include <psapi.h> # include <psapi.h>
# include <tlhelp32.h> # include <tlhelp32.h>
@@ -114,7 +115,7 @@ bool PISystemMonitor::startOnProcess(int pID, PISystemTime interval) {
# else # else
PRIVATE->hProc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pID_); PRIVATE->hProc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pID_);
if (PRIVATE->hProc == 0) { if (PRIVATE->hProc == 0) {
piCoutObj << "Can`t open process with ID = " << pID_ << "," << errorString(); piCoutObj << "Can`t open process with ID = "_tr << pID_ << "," << errorString();
return false; return false;
} }
PRIVATE->tm.reset(); PRIVATE->tm.reset();

View File

@@ -23,14 +23,10 @@ int main(int argc, char * argv[]) {
// piCout << PIString::readableSize(50_KiB); // piCout << PIString::readableSize(50_KiB);
// piCout << PIString::readableSize(1_GB); // piCout << PIString::readableSize(1_GB);
PITranslator::loadLang("ru"); PITranslator::loadLang("ru");
PITranslator::loadConfig("[C]\ntest string=\n"_u8); PITranslator::loadConfig("[]\ntest \\\nstring=привет\n"_u8);
piCout << "test string1"; piCout << "test\nstring"_tr;
piCout << "test string2"_tr;
piCout << piTr("test string", "C1");
PITranslator::clear(); PITranslator::clear();
piCout << "test string3"; piCout << "test\nstring"_tr;
piCout << "test string4"_tr;
piCout << piTr("test string", "C2");
// PICodeParser parser; // PICodeParser parser;
// parser.parseFile("cmg_test.h"); // parser.parseFile("cmg_test.h");
/*for (auto m: parser.macros) { /*for (auto m: parser.macros) {

View File

@@ -72,8 +72,25 @@ TSFile::Content TSFile::read(const PIString & path) {
Context * cc = nullptr; Context * cc = nullptr;
Message msg; Message msg;
int phase = 0; int phase = 0;
bool multi_source = false, multi_translation = false;
while (!ts.isEnd()) { while (!ts.isEnd()) {
auto line = ts.readLine().trim(); auto line = ts.readLine().trim();
if (multi_source) {
if (line.endsWith("</source>")) {
line.cutRight(9);
multi_source = false;
}
msg.source += "\n" + unmask(line);
continue;
}
if (multi_translation) {
if (line.endsWith("</translation>")) {
line.cutRight(14);
multi_translation = false;
}
msg.translation += "\n" + unmask(line);
continue;
}
switch (phase) { switch (phase) {
case 0: case 0:
if (line == "<context>") phase = 1; if (line == "<context>") phase = 1;
@@ -97,7 +114,11 @@ TSFile::Content TSFile::read(const PIString & path) {
if (cc) cc->messages[msg.source] = msg; if (cc) cc->messages[msg.source] = msg;
phase = 2; phase = 2;
} else if (line.startsWith("<source>")) { } else if (line.startsWith("<source>")) {
line.cutLeft(8).cutRight(9); line.cutLeft(8);
if (line.endsWith("</source>"))
line.cutRight(9);
else
multi_source = true;
msg.source = unmask(line); msg.source = unmask(line);
} else if (line.startsWith("<location")) { } else if (line.startsWith("<location")) {
PIString trs = line.takeRange('<', '>').cutLeft(8); PIString trs = line.takeRange('<', '>').cutLeft(8);
@@ -117,7 +138,10 @@ TSFile::Content TSFile::read(const PIString & path) {
PIString v = trs.takeRange('\"', '\"'); PIString v = trs.takeRange('\"', '\"');
if (t == "type") msg.type = v; if (t == "type") msg.type = v;
} }
if (line.endsWith("</translation>"))
line.cutRight(14); line.cutRight(14);
else
multi_translation = true;
msg.translation = unmask(line); msg.translation = unmask(line);
} }
break; break;

View File

@@ -68,6 +68,11 @@ void help() {
} }
PIString fromCode(const PIString & in) {
return in.replacedAll("\\n", '\n').replaceAll("\\r", '\r').replaceAll("\\t", '\t');
}
void gatherStrings(TSFile::Content & content, const PIString & file, const PIString & file_loc) { void gatherStrings(TSFile::Content & content, const PIString & file, const PIString & file_loc) {
PIString source, context; PIString source, context;
int pos = -1, ppos = 0, line = -1; int pos = -1, ppos = 0, line = -1;
@@ -129,14 +134,13 @@ void gatherStrings(TSFile::Content & content, const PIString & file, const PIStr
} }
} }
content[context].confirm(source, file_loc, line); content[context].confirm(fromCode(source), file_loc, line);
pos = ppos; pos = ppos;
piCout << "Context = \"" << context << "\", message = \"" << source << "\""; piCout << "Context = \"" << context << "\", message = \"" << source << "\"";
} }
} }
auto & ec(content[""]); auto & ec(content[""]);
content[context].confirm(source, file_loc, line);
pos = -1; pos = -1;
for (;;) { for (;;) {
source.clear(); source.clear();
@@ -156,7 +160,7 @@ void gatherStrings(TSFile::Content & content, const PIString & file, const PIStr
continue; continue;
} }
} }
ec.confirm(source, file_loc, file.lineNumber(pos)); ec.confirm(fromCode(source), file_loc, file.lineNumber(pos));
piCout << "_tr = \"" << source << "\""; piCout << "_tr = \"" << source << "\"";
} }
} }
@@ -186,7 +190,7 @@ int main(int argc, char * argv[]) {
PIString out_path = cli.argumentValue("output"); PIString out_path = cli.argumentValue("output");
PIStringList files; PIStringList files;
const static PIStringList ext({"*.h", "*.hpp", "*.cpp", "*.cxx"}); const static PIStringList ext({"h", "hpp", "cpp", "cxx"});
for (const PIString & a: cli.optionalArguments()) { for (const PIString & a: cli.optionalArguments()) {
if (PIDir::isExists(a)) { if (PIDir::isExists(a)) {
auto dl = PIDir(a).allEntries(); auto dl = PIDir(a).allEntries();