This repository has been archived by the owner on Mar 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
/
extract-pot
executable file
·97 lines (83 loc) · 3.24 KB
/
extract-pot
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
#!/usr/bin/env node
var async = require('async'),
exec = require('child_process').exec,
path = require('path'),
util = require('util');
// ./scripts/extract-pot --locale locale server static
// Given a locale directory to write out pot files and
// 1 or more directories to find sources in, run
// .js and template (.ejs) files through jsxgettext
var argv = require('optimist').demand(['l'])
.usage('Extract gettext strings from codebase. Usage: $0 --locale=DIRECTORY INPUT_DIR [INPUT_DIR]*')
.alias('h', 'help')
.describe('h', 'display this usage message')
.alias('l', 'locale')
.describe('l', 'locale directory where pot and po files will be written')
//.alias('e', 'exclude')
.describe('exclude', 'directory or filename pattern to exclude from jsxgettext\n\t\tmay be used multiple times')
.alias('t', 'template-engine')
.default('t', 'ejs')
.describe('template-engine', 'Template engine. Defaults to EJS, Jinja supported for nunjucks')
.alias('f', 'template-file-ext')
.default('f', 'ejs')
.describe('template-file-ext', 'File extension of templates. Defaults to ejs as in home.ejs, value can be valid file ext.')
.check(function (argv) {
// We have atleast one directory of source code to process
return argv._.length >= 1;
})
.argv;
var localeDir = path.resolve(process.cwd(), argv.locale);
var sourceDirs = [];
argv._.forEach(function (dir) {
sourceDirs.push(path.resolve(process.cwd(), dir));
});
// top of repo is our current working directory
process.chdir(path.dirname(__dirname));
var jsxGettextPath = path.join(__dirname, '../node_modules/.bin/jsxgettext');
var jsCmd = jsxGettextPath + ' %s --keyword=_ -L JS ' +
'--output-dir=%s/templates/LC_MESSAGES --from-code=utf-8 --output=messages.pot ' +
'`find %s -name \'*.js\' | grep -v node_modules | grep -v .git';
var templateCmd = jsxGettextPath + ' %s --keyword=_ -L ' + argv['template-engine'] +
' --output-dir=%s/templates/LC_MESSAGES --from-code=utf-8 --output=messages.pot ' +
'`find %s -name \'*.%s\' | grep -v node_modules | grep -v .git';
if (argv.exclude) {
var excludes = argv.exclude;
if ('string' === typeof argv.exclude) {
excludes = [argv.exclude];
}
excludes.forEach(function (exclude) {
jsCmd += ' | grep -v ' + exclude;
templateCmd += ' | grep -v ' + exclude;
});
}
templateExt = argv['template-file-ext'];
// Close out `find ...` backtick
jsCmd += '`';
templateCmd += '`';
// Create a new .pot file
var dashJ = '';
// Run commands
async.forEachSeries(sourceDirs, function (dir) {
exec(util.format(jsCmd, dashJ, localeDir, dir), function (err, stdout, stderr) {
// Join existing pot file
dashJ = ' --join-existing=true ';
// We expect an error if no gettext strings are used in client side code
if (err && stdout.indexOf('input argument is required') === -1) {
console.error(stdout);
console.error(stderr);
} else {
console.log(util.format(templateCmd, dashJ, localeDir, dir, templateExt));
exec(util.format(templateCmd, dashJ, localeDir, dir, templateExt), function (err, stdout, stderr) {
if (err) {
console.error(stdout);
console.error(stderr);
}
console.log('done');
});
}
});
}, function (err) {
if (err) {
console.error(err);
}
});