-
Notifications
You must be signed in to change notification settings - Fork 0
/
ini2po.js
executable file
·139 lines (129 loc) · 3.68 KB
/
ini2po.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env node
// webL10n JavaScript ini file to GNU gettext PO file converter
// Template
var starLang = '*';
var enLang = 'en';
var templateFileName = 'template.pot';
var crlf = '\n';
var constHeader = '#. extracted from ';
var constSub1 = [
'#, fuzzy',
'msgid ""',
'msgstr ""',
'"Project-Id-Version: PACKAGE VERSION\\n"',
'"Report-Msgid-Bugs-To: \\n"'
];
var constSub2 = '"POT-Creation-Date: ';
var constSub2b = '\\n"';
var constSub3 = [
'"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n"',
'"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n"',
'"Language-Team: LANGUAGE <[email protected]>\\n"',
'"MIME-Version: 1.0\\n"',
'"Content-Type: text/plain; charset=UTF-8\\n"',
'"Content-Transfer-Encoding: 8bit\\n"',
'"X-Generator: ini2po 0.0.1\\n"'
];
var constComment = '#. Do not translate';
// Generate function
function addContent(content, from) {
var to = from;
if (!content) {
return to;
}
if (content instanceof Array) {
for (var i = 0 ; i < content.length ; i++) {
var subcontent = content[i];
if (subcontent) {
to += subcontent + crlf;
}
}
} else {
to += content + crlf;
}
return to;
}
function addCrLf(content) {
return content + crlf;
}
// Require
var fs = require('fs'),
ini = require('js-ini');
// Get INI filename
var filename = null;
if (process.argv.length != 3) {
console.log("Usage: ini2po <inifile>");
}
filename = process.argv[2];
// Generate all PO files
if (filename) {
// Load file
fs.readFile(filename, 'utf-8', function(err, read) {
// Parse content
if (err) throw err;
var sections = ini.parse(read, {comment: ";;;", autoTyping: false});
var keys = Object.keys(sections);
// Find default
var defaultSectionIndex = null;
for (var i = 0 ; i < keys.length ; i++) {
var language = keys[i];
if (language == starLang) {
defaultSectionIndex = language;
break;
} else if (language == enLang && defaultSectionIndex == null) {
defaultSectionIndex = language;
}
}
if (defaultSectionIndex == null) {
console.log("no default language");
return;
}
var defaultSection = sections[defaultSectionIndex];
// Iterate on each section
for (var i = 0 ; i < keys.length ; i++) {
// Generate header
var language = keys[i];
var content = '';
content = addContent(constHeader + filename, content);
content = addContent(constSub1, content);
content = addContent(constSub2 + new Date().toString() + constSub2b, content);
content = addContent(constSub3, content);
content = addCrLf(content);
// Prepare output file
var outputname = language + '.po';
if (language == starLang) {
outputname = templateFileName;
}
// Iterate on each string
var section = sections[language];
var items = Object.keys(section);
for (var j = 0 ; j < items.length ; j++) {
// Generate lines
var msgid = defaultSection[items[j]];
if (msgid == null) {
console.warn("WARNING: string '"+items[j]+"' in '"+language+"' not in default language");
continue;
}
var currentTranslation = section[items[j]];
var res = msgid.match(/{{[^}}]+}}/g);
if (res && res.length) {
var comment = constComment;
for (var k = 0 ; k < res.length ; k++) {
comment += ' ' + res[k];
}
content = addContent(comment, content);
}
content = addContent('#: '+items[j], content);
content = addContent('msgctxt "'+items[j]+'"', content);
content = addContent('msgid "'+msgid.replace(/"/g,'\\"')+'"', content);
content = addContent('msgstr "'+currentTranslation.replace(/"/g,'\\"')+'"', content);
content = addCrLf(content);
}
// Write file
console.log(outputname + ' generated');
fs.writeFile(outputname, content, 'utf8', function(err) {
if (err) throw err;
});
}
});
}