17 QFile iFile(inputFile);
18 iFile.open(QIODevice::ReadOnly);
20 QXmlStreamReader xmlReader(&iFile);
28 st_WaitForStartElement = 0x01,
29 st_WaitForText = 0x02,
30 st_WaitForEndElement = 0x04
33 int states = st_WaitForStartElement;
35 while(!xmlReader.atEnd())
37 QXmlStreamReader::TokenType tt = xmlReader.readNext();
40 case QXmlStreamReader::StartDocument:
42 root = std::make_shared<document_node>();
45 case QXmlStreamReader::DTD:
47 current->add_child(std::make_shared<DTD_node>(
"<!DOCTYPE TS>"));
49 case QXmlStreamReader::StartElement:
51 assert(states & st_WaitForStartElement);
53 QString name = xmlReader.name().toString();
54 QXmlStreamAttributes attrs = xmlReader.attributes();
60 else if(
"source" == name)
64 else if(
"translation" == name)
70 current = current->add_child(std::make_shared<TS_node>(name, attrs));
77 states = st_WaitForText|st_WaitForStartElement|st_WaitForEndElement;
79 case QXmlStreamReader::Characters:
81 if(states & st_WaitForText)
83 text = xmlReader.text().toString();
84 states = st_WaitForEndElement|st_WaitForStartElement;
87 case QXmlStreamReader::EndElement:
89 assert(states & st_WaitForEndElement);
93 reinterpret_cast<element_node*
>(current.get())->set_text(text);
96 states = st_WaitForStartElement|st_WaitForEndElement;
97 current = current->
parent();
99 case QXmlStreamReader::Invalid: {
break; }
100 case QXmlStreamReader::EndDocument: {
break; }
101 case QXmlStreamReader::Comment: {
break; }
102 case QXmlStreamReader::EntityReference: {
break; }
103 case QXmlStreamReader::ProcessingInstruction: {
break; }
104 case QXmlStreamReader::NoToken: {
break; }
116 QFile iFile(inputFile);
117 iFile.open(QFile::ReadOnly|QFile::Text);
118 QTextStream txts(&iFile);
119 txts.setCodec(
"UTF-8");
121 const QString rgxp(
"^(?<id>\\[\\[\\[[A-F0-9]{8}\\]\\]\\])\\s*[\\\",“,”](?<text>.*)[\\\",“,”]$");
122 QRegularExpression rxp(rgxp);
124 unsigned int line_counter = 0;
128 QString str = txts.readLine();
129 QRegularExpressionMatch rm = rxp.match(str);
131 QString
id = rm.captured(
"id");
132 QString text = rm.captured(
"text");
134 if(
id.isEmpty() || text.isEmpty())
136 std::cout <<
"Error in line: " << line_counter <<
" , file: " << inputFile.toUtf8().constData() <<
" , source line: " << str.toUtf8().constData() << std::endl;
140 strings.insert(visitors::map_QStringQString::value_type(
id, text));
150void MyTranlatorParser::toTXT(
const QString &inputFile,
const QString &outputDir,
bool with_unfinished,
bool with_vanished,
bool unfinished_only)
154 QFileInfo fiI(inputFile);
157 myLanguageModel->
mySetting->
showMessageBox(QObject::tr(
"Input does file not exist!").toLocal8Bit(), QString(
"%1: %2").arg(tr(
"Unable to find Input file"), inputFile).toLocal8Bit(), myLanguageModel->
mySetting->
Critical);
161 QFileInfo fiO(outputDir);
164 QDir().mkdir(outputDir);
167 QString outputXmlFileName = QDir(outputDir).path() + QDir::separator() + fiI.fileName();
168 QString outputTextFile = QDir(outputDir).path() + QDir::separator() + fiI.baseName() +
".txt";
170 unsigned int files_in_out_dir = QDir(outputDir).entryInfoList(QDir::NoDotAndDotDot|QDir::AllEntries).count();
172 if( !fiO.exists() || 2 < files_in_out_dir || (2 == files_in_out_dir && !QFileInfo::exists(outputXmlFileName) && !QFileInfo::exists(outputTextFile)) )
175 myLanguageModel->
mySetting->
showMessageBox(QObject::tr(
"Check output Folder").toLocal8Bit(), QString(
"%1!: %2").arg(tr(
"Can not create output directory OR directory is not empty"), inputFile).toLocal8Bit(), myLanguageModel->
mySetting->
Critical);
179 QFile oFile(outputXmlFileName);
180 oFile.open(QIODevice::WriteOnly);
191 QFile sFile(outputTextFile);
192 sFile.open(QIODevice::WriteOnly|QIODevice::Text);
193 QTextStream txts(&sFile);
194 txts.setCodec(
"UTF-8");
196 std::for_each(strings.begin(), strings.end(), [&txts](
const map_hashQString::value_type &vt){ txts << vt.second <<
"\n"; });
199 QXmlStreamWriter xmlWriter(&oFile);
200 xmlWriter.setAutoFormatting(
true);
213 QFileInfo fiI(inputDir);
216 myLanguageModel->
mySetting->
showMessageBox(QObject::tr(
"Input directory not exist!").toLocal8Bit(), QString(
"%1: %2").arg(tr(
"Unable to find Input folder"), inputDir).toLocal8Bit(), myLanguageModel->
mySetting->
Critical);
221 const QFileInfoList &fil = QDir(inputDir).entryInfoList(QDir::NoDotAndDotDot|QDir::AllEntries);
222 unsigned int files_in_input_dir = fil.count();
224 QString tsFile, txtFile;
226 if(2 == files_in_input_dir)
228 QFileInfo if0(QDir(inputDir).path() + QDir::separator() + fil[0].baseName() +
".ts");
229 QFileInfo if1(QDir(inputDir).path() + QDir::separator() + fil[0].baseName() +
".txt");
231 if(if0.isFile() && if1.isFile())
233 tsFile = if0.filePath();
234 txtFile = if1.filePath();
238 if(2 < files_in_input_dir || 0 == files_in_input_dir || tsFile.isEmpty() || txtFile.isEmpty())
240 myLanguageModel->
mySetting->
showMessageBox(QObject::tr(
"Check Input Folder").toLocal8Bit(), QString(
"%1!: %2").arg(tr(
"Input directory should contain only txt and ts file with same name"), tsFile).toLocal8Bit(), myLanguageModel->
mySetting->
Critical);
252 std::cout <<
"Parsing error: " << txtFile.toUtf8().constData() <<
" !" << std::endl;
253 myLanguageModel->
mySetting->
showMessageBox(QObject::tr(
"Parsing errorr").toLocal8Bit(), QString(
"%1: %2").arg(tr(
"Parsing errorr"), txtFile.toUtf8().constData()).toLocal8Bit(), myLanguageModel->
mySetting->
Critical);
263 QFile oFile(outputFile);
264 oFile.open(QIODevice::WriteOnly);
266 QXmlStreamWriter xmlWriter(&oFile);
267 xmlWriter.setAutoFormatting(
true);
268 xmlWriter.setCodec(
"UTF-8");
279 isDebugMessage = thisState;
287 return isDebugMessage;
297 qDebug() << thisMessage;
Abstact Model used for ComboBox. /class MyLanguageModel.
MyOrgSettings * mySetting
mySetting
QVariant showMessageBox(const QString &thisTitle, const QString &thisMessage, MyMessageTypes thisMessageType) const
show Message Box information, question, warning, critica
void toTS(const QString &inputDir, const QString &outputFile, const QString &langid)
toTS
void setDebugMessage(bool thisState)
set Debug Message
bool getDebugMessage()
get Debug Message
MyTranlatorParser(MyLanguageModel *thisLanguageModel=nullptr, QObject *parent=nullptr)
Translation Files Constructor. MyTranslationFiles.
bool parse_txt_file(const QString &inputFile, visitors::map_QStringQString &strings)
parse_txt_file
void setMessage(const QString &thisMessage)
set Message
base_node::base_node_ptr parse_ts_file(const QString &inputFile)
parse_ts_file
void toTXT(const QString &inputFile, const QString &outputDir, bool with_unfinished, bool with_vanished, bool unfinished_only)
toTXT
std::map< QString, QString > map_QStringQString
std::map< hash_t, QString > map_hashQString
map hashQString.
std::shared_ptr< base_node > base_node_ptr
base_node_ptr parent() const