-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpo2ini.js
executable file
·124 lines (111 loc) · 2.93 KB
/
po2ini.js
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
#!/usr/bin/env node
// GNU gettext PO to webL10n JavaScript ini file converter
// Require
var fs = require('fs'),
readline = require('line-reader'),
path = require('path'),
minimist = require('minimist');
// constant
var defaultOutputfile = "locale.ini";
var defaultTemplateFile = "template.pot";
// Get arguments
var argv = minimist(process.argv.slice(2), {string: 'o'});
if (argv._.length == 0) {
console.log("Usage: po2ini [-o <outputfile>] <pofile1> [<pofile2> ...]");
return;
}
var outputFile = defaultOutputfile;
if (argv.o) {
outputFile = argv.o;
}
var content = "";
// Processing function
function processFiles(filenames, index) {
var basename = path.basename(filenames[index]);
var sectioname = (basename == defaultTemplateFile ? "*" : basename.substr(0,basename.indexOf(".")));
// Load all lines
var state = -1;
var msgctxt = "";
var msgid = "";
var msgstr = "";
readline.eachLine(fs.createReadStream(filenames[index], {options: 'utf-8'}), function(line, last) {
// Write setHeader
if (state == -1) {
console.log("Process "+filenames[index]+"...")
content += crlf+"["+sectioname+"]"+crlf;
state = 0;
}
// Get line type
var isMsgctxt = line.match(/msgctxt\s*"([^"]*)"/);
var isMsgid = line.match(/msgid\s*"([^"]*)"/);
var isMsgstr = line.match(/msgstr\s*"(.*)"$/);
var isString = line.match(/^"(.*)"$/);
var isOther = (!isMsgctxt && !isMsgid && !isMsgstr && !isString);
// Look for msgctxt and strings
if (state == 0) {
if (isMsgctxt) {
msgctxt = (isMsgctxt.length > 1) ? isMsgctxt[1] : "";
state = 1;
}
} else if (state == 1) {
if (isString) {
msgctxt += (isString.length > 1) ? isString[1] : "";
} else {
state = 2;
}
}
// Look for msgid and strings
if (state == 2) {
if (isMsgid) {
msgid = (isMsgid.length > 1) ? isMsgid[1] : "";
state = 3;
}
} else if (state == 3) {
if (isString) {
msgid += (isString.length > 1) ? isString[1] : "";
} else {
state = 4;
}
}
// Look for msgstr and stings
if (state == 4) {
if (isMsgstr) {
msgstr = (isMsgstr.length > 1) ? isMsgstr[1] : "";
if (last) {
content += msgctxt + "=" + msgstr.replace("\\\"","\"") + crlf;
}
state = 5;
}
} else if (state == 5) {
if (isString) {
msgstr += (isString.length > 1) ? isString[1] : "";
if (last) {
content += msgctxt + "=" + msgstr.replace(/\\\"/g,"\"") + crlf;
}
} else {
content += msgctxt + "=" + msgstr.replace(/\\\"/g,"\"") + crlf;
msgid = msgctxt = msgstr = "";
state = 0;
}
}
// Process next file
if (last) {
if ((index+1) < filenames.length) {
processFiles(filenames, index+1);
} else {
fs.writeFile(outputFile, content, 'utf8', function(err) {
if (err) throw err;
});
}
}
});
}
// Load all files
var crlf = '\n';
var filenames = [];
for (var i = 0 ; i < argv._.length ; i++) {
var name = argv._[i];
filenames.push(name);
}
// Process files
processFiles(filenames, 0);