/
l10n-extract.js
92 lines (82 loc) · 2.66 KB
/
l10n-extract.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
/* eslint-disable no-useless-catch */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// grunt task to extract strings.
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var extract = require('jsxgettext-recursive-next');
var execSync = require('child_process').execSync;
// where to place the pot files.
var messagesOutputPath = path.join(
__dirname,
'..',
'locale',
'templates',
'LC_MESSAGES'
);
var babelCmd =
'NODE_OPTIONS=--openssl-legacy-provider yarn babel --plugins=@babel/plugin-syntax-dynamic-import,dynamic-import-webpack,@babel/plugin-proposal-class-properties --presets @babel/preset-react,@babel/env,@babel/preset-typescript app/scripts --out-dir .es5';
var templateCmd = 'cp -r app/scripts/templates .es5/templates/';
module.exports = function (grunt) {
grunt.registerTask(
'jsxextract',
'Do not call directly, see l10n-extract.',
function () {
var done = this.async();
if (!fs.existsSync(messagesOutputPath)) {
mkdirp.sync(messagesOutputPath);
}
// jsxgettext does not support ES2015, only ES5. Run babel to convert
// then run the extractor on the ES5 files.
try {
execSync(babelCmd);
} catch (e) {
throw e;
}
try {
// babel only converts a subset of files, copy all other files
// where there may be strings to extract.
execSync(templateCmd);
} catch (e) {
throw e;
}
var clientWalker = extract({
'input-dir': path.join(__dirname, '..', '.es5'),
joinExisting: true,
keyword: ['t', 'unsafeTranslate'],
output: 'client.pot',
outputDir: messagesOutputPath,
parsers: {
'.js': 'javascript',
'.mustache': 'handlebars',
},
});
clientWalker.on('end', function () {
var authWalker = extract({
exclude: /pages\/dist/,
'input-dir': path.join(__dirname, '..', 'server'),
joinExisting: true,
keyword: ['t', 'unsafeTranslate'],
output: 'server.pot',
outputDir: messagesOutputPath,
parsers: {
'.html': 'handlebars',
'.js': 'javascript',
'.txt': 'handlebars',
},
parserOptions: '{"ecmaVersion":"2018"}',
});
authWalker.on('end', function () {
done();
});
});
}
);
grunt.registerTask(
'l10n-extract',
'Extract strings from templates for localization.',
['clean', 'jsxextract']
);
};