This repository has been archived by the owner on Nov 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwp-webpack-translation-plugin.js
181 lines (140 loc) · 5.79 KB
/
wp-webpack-translation-plugin.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
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
const chokidar = require('chokidar');
const {exec, execSync, spawnSync } = require('child_process');
const webpack = require('webpack');
const wpPot = require('wp-pot');
const path = require('path');
const fs = require('fs');
const getTextParser = require('gettext-parser');
const lodash = require('lodash');
const colors = require('colors');
const commandExists = require('command-exists').sync
module.exports = class WpPotPlugin {
constructor (options) {
this.options = options;
this.potWatcher = {};
this.poWatcher = {};
this.phpWatcher = {};
this.initialized = false;
this.logPrefix = "[wp-webpack-translation-plugin]"
if(!commandExists("msgmerge")) {
throw `${this.logPrefix} 'msgmerge' command doesn't exist. Ensure, that you have gettext installed.`
}
}
recursiveEmptyTranslationFill (object, currentTranslatableString) {
for(let e in object) {
if(e == "msgstr") {
let has_translations = false;
for(let a in object[e]) {
if(object[e][a].length > 0) {
has_translations = true;
break;
}
}
if(!has_translations) {
object[e] = [
currentTranslatableString
];
}
} else if(typeof(object[e]) == "object") {
object[e] = this.recursiveEmptyTranslationFill(object[e], e);
}
}
return object;
}
generatePotFiles () {
process.stdout.write(`${this.logPrefix} Generating pot files: `.white);
let potFile = this.options.languagesDirectory + '/' + this.options.domain + '.pot';
try {
wpPot({
destFile: potFile,
domain : this.options.domain,
src : this.options.phpDirectories
});
process.stdout.write("[Done]\n".green);
} catch (error) {
process.stdout.write("[ERROR]\n".red);
console.log(("" + error).red);
}
}
mergePotToPoFiles () {
process.stdout.write(`${this.logPrefix} Merging pot files to po files: `.white);
var files = fs.readdirSync(this.options.languagesDirectory);
for(var f in files) {
let file = files[f];
let ext = path.extname(file);
let filename = path.basename(file, ext);
let poFile = this.options.languagesDirectory + '/' + file;
let potFile = this.options.languagesDirectory + '/' + this.options.domain + '.pot';
if (ext != ".po") {
continue;
}
exec('msgmerge --force-po ' + poFile + ' ' + potFile + " -o " + poFile);
}
process.stdout.write("[Done]\n".green);
}
generatePoMoFiles () {
process.stdout.write(`${this.logPrefix} Compiling po to mo files: `.white);
var files = fs.readdirSync(this.options.languagesDirectory);
for(var f in files) {
let file = files[f];
let ext = path.extname(file);
let filename = path.basename(file, ext);
let poFile = this.options.languagesDirectory + '/' + file;
let moFile = this.options.languagesDirectory + '/' + filename + '.mo';
if(ext != ".po") {
continue;
}
let poFileContent = getTextParser.po.parse(fs.readFileSync(poFile));
poFileContent = this.recursiveEmptyTranslationFill(poFileContent);
let moFileContent = getTextParser.mo.compile(poFileContent);
fs.writeFileSync(moFile, moFileContent);
}
process.stdout.write("[Done]\n".green);
}
runTranslationsGenerations () {
this.generatePotFiles();
this.mergePotToPoFiles();
this.generatePoMoFiles();
}
apply (compiler) {
if(lodash.isEmpty(this.options.phpDirectories)) {
throw new Error("Options need a parameter 'phpDirectories', which contains the locations to watch php files.");
return;
}
if(lodash.isEmpty(this.options.languagesDirectory)) {
throw new Error("Options need a parameter 'languagesDirectory', which contains the location of the pot, po and mo files.");
return;
}
if(lodash.isEmpty(this.options.domain)) {
throw new Error("Options need a parameter 'domain', which is the domain name of the translations (and also .pot file name etc).");
return;
}
if(compiler.options.watch) {
this.runTranslationsGenerations();
this.phpWatcher = chokidar.watch(this.options.phpDirectories);
this.phpWatcher.on('change', (p) => {
console.log(`\n${this.logPrefix} PHP file: ${path.basename(p)} changed.`.white);
compiler.run(() => {
this.generatePotFiles();
});
});
this.potWatcher = chokidar.watch(this.options.languagesDirectory + '/*.pot');
this.potWatcher.on('change', (p) => {
console.log(`\n${this.logPrefix} POT file: ${path.basename(p)} changed.`.white);
compiler.run(() => {
this.mergePotToPoFiles();
});
});
this.poWatcher = chokidar.watch(this.options.languagesDirectory + '/*.po');
this.poWatcher.on('change', (p) => {
console.log(`\n${this.logPrefix} POT file: ${path.basename(p)} changed.`.white);
compiler.run(() => {
this.generatePoMoFiles();
});
});
this.initialized = true;
} else {
this.runTranslationsGenerations();
}
}
};