-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
408 lines (322 loc) · 11.2 KB
/
main.cpp
File metadata and controls
408 lines (322 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <regex>
using namespace std;
void processTsFileLine(string& line, vector<string>& translationStrings)
{
if (line.find("<source>") == string::npos) {
return;
}
size_t start = line.find("<source>") + 8;
size_t end = line.find("</source>");
string str = line.substr(start, end - start);
translationStrings.push_back(str);
}
void processCsvFileLine(string& line, vector<string>& row, vector<vector<string>>& csvContent)
{
row.clear();
stringstream ss(line);
string field;
bool inQuotes = false;
char prevChar = '\0';
for (size_t i = 0; i < line.length(); i++) {
char c = line[i];
if (c == '"' && prevChar != '\\') {
inQuotes = !inQuotes;
} else if (c == ',' && !inQuotes) {
row.push_back(field);
field.clear();
} else {
field += c;
}
prevChar = c;
}
if (!field.empty()) {
row.push_back(field);
}
csvContent.push_back(row);
}
void showInstructions()
{
cout << "ts2csv project.ts"
<< "\n\nCSV to TS mode:"
<< "\nts2csv project.ts output.csv"
<< endl;
}
map<string, map<string, string>> generateReplacementsByLanguageMap(vector<vector<string>>& csvContent, vector<string>& languages)
{
map<string, map<string, string>> replacementsByLanguage;
for (string& language : languages) {
replacementsByLanguage[language] = map<string, string>();
}
bool isHeader = true;
for (vector<string>& content : csvContent) {
if (isHeader) {
isHeader = false;
continue;
}
if (content.empty()) continue;
string sourceString = content[0];
for (int i = 1; i <= languages.size(); i++) {
string lang = languages[i - 1];
if (lang.empty()) {
continue;
}
map<string, string>& langMap = replacementsByLanguage[lang];
if (i < content.size()) {
langMap[sourceString] = content[i];
} else {
langMap[sourceString] = string();
}
}
}
return replacementsByLanguage;
}
vector<string> getLanguagesFromHeaders(vector<string>& headers)
{
vector<string> languages;
for (size_t i = 1; i < headers.size(); i++) {
languages.push_back(headers[i]);
}
return languages;
}
string unescapeString(const string& input) {
string output;
output.reserve(input.size());
for (size_t i = 0; i < input.size(); i++) {
if (input[i] == '\\' && i + 1 < input.size()) {
switch (input[i + 1]) {
case 'n': output += '\n'; i++; break;
case 't': output += '\t'; i++; break;
case 'r': output += '\r'; i++; break;
case '\\': output += '\\'; i++; break;
case '"': output += '"'; i++; break;
default: output += input[i]; break;
}
} else {
output += input[i];
}
}
return output;
}
string escapeXml(const string& input) {
string output;
output.reserve(input.size());
for (char c : input) {
switch (c) {
case '&': output += "&"; break;
case '<': output += "<"; break;
case '>': output += ">"; break;
case '"': output += """; break;
case '\'': output += "'"; break;
default: output += c; break;
}
}
return output;
}
string escapeForCsv(const string& input) {
string output;
output.reserve(input.size());
for (char c : input) {
switch (c) {
case '\n': output += "\\n"; break;
case '\t': output += "\\t"; break;
case '\r': output += "\\r"; break;
case '\\': output += "\\\\"; break;
case '"': output += "\\\""; break;
default: output += c; break;
}
}
return output;
}
void generateTsFiles(vector<vector<string>>& csvContent, string& filename, string& csvFilename)
{
vector<string> headers = csvContent[0];
// Clean headers (remove quotes if present)
for (auto& header : headers) {
if (!header.empty() && header.front() == '"' && header.back() == '"') {
header = header.substr(1, header.size() - 2);
}
}
vector<string> languages = getLanguagesFromHeaders(headers);
ifstream t(filename);
stringstream buffer;
buffer << t.rdbuf();
string tsFileContents = buffer.str();
t.close();
const string TRANSLATION_OPEN_TAG = "<translation";
const string TRANSLATION_CLOSE_TAG = "</translation>";
// Create a map for quick lookup of translations
map<string, map<string, string>> translationsByLang;
// First, populate the map from CSV data
bool isHeader = true;
for (vector<string>& row : csvContent) {
if (isHeader) {
isHeader = false;
continue;
}
if (row.empty()) continue;
string source = row[0];
if (!source.empty() && source.front() == '"' && source.back() == '"') {
source = source.substr(1, source.size() - 2);
}
for (size_t i = 1; i <= languages.size(); i++) {
if (i - 1 >= languages.size()) continue;
string lang = languages[i - 1];
if (lang.empty()) continue;
string translation;
if (i < row.size()) {
translation = row[i];
if (!translation.empty() && translation.front() == '"' && translation.back() == '"') {
translation = translation.substr(1, translation.size() - 2);
}
translation = unescapeString(translation);
}
translationsByLang[lang][source] = translation;
}
}
// Generate TS file for each language
for (string &language: languages) {
if (language.empty()) continue;
string langTsFileContents = tsFileContents;
size_t pos = 0;
// Find and replace each translation
while (pos < langTsFileContents.length()) {
// Find source tag
size_t sourceStart = langTsFileContents.find("<source>", pos);
if (sourceStart == string::npos) break;
size_t sourceEnd = langTsFileContents.find("</source>", sourceStart);
if (sourceEnd == string::npos) break;
string source = langTsFileContents.substr(sourceStart + 8, sourceEnd - sourceStart - 8);
// Find translation tag
size_t transStart = langTsFileContents.find(TRANSLATION_OPEN_TAG, sourceEnd);
if (transStart == string::npos) {
pos = sourceEnd + 9;
continue;
}
size_t transEnd = langTsFileContents.find(TRANSLATION_CLOSE_TAG, transStart);
if (transEnd == string::npos) {
pos = sourceEnd + 9;
continue;
}
transEnd += TRANSLATION_CLOSE_TAG.length();
// Get translation from map
string translation;
auto langIt = translationsByLang.find(language);
if (langIt != translationsByLang.end()) {
auto transIt = langIt->second.find(source);
if (transIt != langIt->second.end()) {
translation = escapeXml(transIt->second);
}
}
// Build new translation string
string translationString = "<translation>";
if (!translation.empty()) {
translationString += translation;
}
translationString += "</translation>";
// Replace in content
langTsFileContents.replace(transStart, transEnd - transStart, translationString);
// Move position after the replaced translation
pos = transStart + translationString.length();
}
// Write to file
string outputFilename = language + ".ts";
ofstream file(outputFilename, ios::out | ios::binary);
if (!file.is_open()) {
cout << "File could not be created: " << outputFilename << endl;
continue;
}
file << langTsFileContents;
file.close();
cout << "Created: " << outputFilename << endl;
}
}
void generateCsv(vector<string>& translationStrings)
{
// Remove duplicates
map<string, bool> seen;
vector<string> uniqueStrings;
for (auto& str : translationStrings) {
if (!seen[str]) {
uniqueStrings.push_back(str);
seen[str] = true;
}
}
ofstream csvFile("output.csv", ios::out | ios::trunc);
// Write header
csvFile << "\"Source\",\"English\",\"Spanish\",\"French\",\"German\",\"Russian\"\n";
if (!csvFile.is_open()) {
cout << "Could not create file: output.csv" << endl;
exit(1);
}
// Write data rows
for (auto& translationString : uniqueStrings) {
// Escape the source string for CSV
string escapedSource = translationString;
// Replace quotes with escaped quotes
size_t quotePos = escapedSource.find('"');
while (quotePos != string::npos) {
escapedSource.replace(quotePos, 1, "\"\"");
quotePos = escapedSource.find('"', quotePos + 2);
}
csvFile << "\"" << escapedSource << "\"";
// Add empty columns for translations
for (int i = 0; i < 5; i++) {
csvFile << ",\"\"";
}
csvFile << endl;
}
csvFile.close();
cout << "Created: output.csv" << endl;
}
int main(int argc, char *argv[])
{
if (argc == 1) {
showInstructions();
return 0;
}
string filename = string(argv[1]);
bool isCsv = argc == 3;
string csvFilename;
if (argc == 3) {
csvFilename = argv[2];
}
string line;
string openFile = csvFilename.empty() ? filename : csvFilename;
ifstream file(openFile);
vector<vector<string>> csvContent;
vector<string> row;
vector<string> translationStrings;
if (!file.is_open()) {
cout << "File could not be opened: " << openFile << endl;
return 1;
}
if (isCsv) {
// Read CSV file
while (getline(file, line)) {
processCsvFileLine(line, row, csvContent);
}
if (csvContent.empty()) {
cout << "No content found in CSV!" << endl;
return 0;
}
generateTsFiles(csvContent, filename, csvFilename);
} else {
// Read TS file
while (getline(file, line)) {
processTsFileLine(line, translationStrings);
}
if (translationStrings.empty()) {
cout << "No translation strings found in TS file!" << endl;
return 0;
}
generateCsv(translationStrings);
}
file.close();
return 0;
}