-
Notifications
You must be signed in to change notification settings - Fork 18
/
gettext-compiler.js
151 lines (125 loc) · 3.95 KB
/
gettext-compiler.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
140
141
142
143
144
145
146
147
148
149
150
151
var extract = require('./lib/extract');
var fs = require('fs');
var path = require('path');
var glob = require('glob');
var async = require('async');
var po = require('pofile');
var _ = require('lodash');
var tsDir = './src';
var poDir = './l18n';
var outputPot = './l18n/template.pot';
var outputCompiled = './src/gettext/string.service.ts';
_.each(process.argv, function (arg, i) {
if (arg === '--ts-dir') {
tsDir = process.argv[i + 1] || './src';
} else if (arg === '--po-dir') {
poDir = process.argv[i + 1] || './l18n';
} else if (arg === '--output-pot') {
outputPot = process.argv[i + 1] || './po/template.pot';
} else if (arg === '--output-compiled') {
outputCompiled = process.argv[i + 1] || './po/template.pot';
}
});
var terms = [];
var q = async.queue(function (task, callback) {
fs.readFile(task.file, function (err, data) {
if (err) throw err;
terms.push({
file: task.file,
terms: extract(data.toString(), task.file.substr(task.file.lastIndexOf('.') + 1))
});
callback();
});
});
glob(tsDir + '/**/*.{ts,html}', function (err, files) {
if (err) throw err;
_.each(files, function (file) {
q.push({file: file});
});
});
q.drain = function () {
var catalog = new po();
_.each(terms, function (group) {
_.each(group.terms, function (term) {
var comment = group.file + ' [' + term.start.line + ':' + term.start.column + ']';
var existing = _.find(catalog.items, function (i) {
return i.msgid === term.term;
});
if (existing) {
existing.comments.push(comment);
} else {
var item = new po.Item();
item.msgid = term.term;
item.comments = [comment];
catalog.items.push(item);
}
});
});
catalog.headers = {
'Content-Type': 'text/plain; charset=UTF-8',
'Content-Transfer-Encoding': '8bit',
'Project-Id-Version': ''
};
catalog.items.sort(function (a, b) {
return a.msgid === b.msgid ? 0 : a.msgid > b.msgid ? 1 : -1;
});
mkdir(outputPot);
fs.writeFile(outputPot, "", function (err) {
if (err) throw err;
catalog.save(outputPot, function (err) {
if (err) throw err;
compile();
});
});
};
var compile = function () {
var strings = {};
var q = async.queue(function (task, callback) {
fs.readFile(task.file, function (err, data) {
if (err) throw err;
var catalog = po.parse(data.toString());
strings[catalog.headers.Language] = {};
_.each(catalog.items, function (term) {
strings[catalog.headers.Language][term.msgid] = term.msgstr.length === 1 ? term.msgstr[0] : term.msgstr;
});
callback();
});
});
glob(poDir + '/**/*.po', function (err, files) {
if (err) throw err;
_.each(files, function (file) {
q.push({
file: file
});
});
});
q.drain = function () {
var service = '/** This file is automatically generated, do not edit it! **/\n\n' +
'import {Injectable} from "@angular/core";\n\n' +
'@Injectable()\n' +
'export class GettextStrings {\n' +
' getStrings() {\n' +
' return ' + JSON.stringify(strings) + ';\n' +
' }\n' +
'}';
fs.writeFile(outputCompiled, service, function (err) {
if (err) throw err;
});
};
};
var directoryExists = function (path) {
try {
return fs.statSync(path).isDirectory();
}
catch (err) {
return false;
}
};
var mkdir = function (filePath) {
var dirname = path.dirname(filePath);
if (directoryExists(dirname)) {
return true;
}
mkdir(dirname);
fs.mkdirSync(dirname);
};